|
Hi
I am trying to create a macro that will take a number of numerator variables and a number of denominator variables and for each set divide the numerator by the corresponding value in the denominator set. The macros I wrote is as follows:
Define !meanpercent (!positional !charend('/')/ !positional !cmdend) !do !vars !in (!1) !do !vars2 !in (!2) compute !concat(!1,!2,"_mp") = !1/!2. fre !1!2 /sta mean/format notable. !doend !doend !end define.
!meanpercent s4 s5/ s3 s3.
What I would like is based on this get 2 new variables s4s3_mp = s4/s3 and S5s3_mp=s5/s3.
But when I run this it does not work. I don't want to fix the number of numerators entered or denominators entered. I want it to be flexible. Any help will be appreciated.
Thanks,
Ratna
Ratna Wynn
|
|
Administrator
|
You've got a few problems here. One is that it is !enddefine, not !end define. Another is that with your two lines,
!do !vars !in (!1) !do !vars2 !in (!2) you get a nested loop. I.e., s4/s3 and s5/s3 will each get computed twice. I doubt that's what you want. I suspect you want to walk through the two lists in parallel. So...try this: Define !meanpercent (!positional !charend('/')/ !positional !cmdend) *** Process the two lists in parallel *** . !LET !YCOPY=!2 !DO !X !IN (!1) !LET !Y = !HEAD(!YCOPY) !LET !YCOPY=!TAIL(!YCOPY) compute !concat(!X,!Y,"_mp") = !X/!Y. format !concat(!X,!Y,"_mp") (f5.2). fre !X !Y /sta mean/format notable. !doend !enddefine. !meanpercent s4 s5/ s3 s3. HTH.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
|
In reply to this post by Ratna Wynn
Dear Ratna,
Here's an written out example with some test data and a classic macro solution as well as a very basic Python solution. HTH, Ruben van den Berg Consultant Models & Methods TNS NIPO Email: [hidden email] Mobiel: +31 6 24641435 Telefoon: +31 20 522 5738 Internet: www.tns-nipo.com *Testdata. data list free/id. begin data 1 2 3 4 5 6 7 8 9 10 end data. do repeat v= bli bla blo vli vla vlo. compute v=rnd(rv.uni(.5,2.5)). end repeat. *Classic macro definition. define !meanpercent(dlist=!enclose("(",")")/nlist=!enclose("(",")")) !let !cnlist=!nlist !do !dvar !in (!dlist) !let !nvar=!head(!cnlist) !let !cnlist=!tail(!cnlist) compute !concat(!dvar,!nvar,"_mp")=!dvar/!nvar. frequencies variables= !concat(!dvar,!nvar,"_mp"). !doend !enddefine. *Macro call. !meanpercent dlist (bli bla blo) nlist(vli vla vlo). *Python solution, I added an extra underscore to the new variable names in order not to overwrite their previous counterparts. begin program. dlist=['bli','bla','blo'] nlist=['vli','vla','vlo'] for i in range(len(nlist)): spss.Submit("compute "+dlist[i]+nlist[i]+"__mp ="+dlist[i]+"/"+nlist[i]+".") spss.Submit("frequencies variables ="+dlist[i]+nlist[i]+"__mp.") end program. Date: Fri, 18 Jun 2010 09:47:46 -0400 From: [hidden email] Subject: Help with an SPSS Macro To: [hidden email] Hi
I am trying to create a macro that will take a number of numerator variables and a number of denominator variables and for each set divide the numerator by the corresponding value in the denominator set. The macros I wrote is as follows:
Define !meanpercent (!positional !charend('/')/ !positional !cmdend) !do !vars !in (!1) !do !vars2 !in (!2) compute !concat(!1,!2,"_mp") = !1/!2. fre !1!2 /sta mean/format notable. !doend !doend !end define.
!meanpercent s4 s5/ s3 s3.
What I would like is based on this get 2 new variables s4s3_mp = s4/s3 and S5s3_mp=s5/s3.
But when I run this it does not work. I don't want to fix the number of numerators entered or denominators entered. I want it to be flexible. Any help will be appreciated.
Thanks,
Ratna
Ratna Wynn
New Windows 7: Find the right PC for you. Learn more. |
|
Administrator
|
In reply to this post by Ratna Wynn
Yeah, that good old !head !tail parser I first posted about 15 years ago;-)
--- How about within the macro body after applying the !ENDDEFINE fix and adding a 3rd argument list. I typically avoid positionals but whatever floats your boat. DO REPEAT Top=!1 / Bot=!2 /DIV=!3. COMPUTE DIV=TOP/BOT. END REPEAT. On Fri, 18 Jun 2010 07:10:14 -0700, Bruce Weaver <[hidden email]> wrote: >You've got a few problems here. One is that it is !enddefine, not !end >define. Another is that with your two lines, > >!do !vars !in (!1) >!do !vars2 !in (!2) > >you get a nested loop. I.e., s4/s3 and s5/s3 will each get computed twice. >I doubt that's what you want. I suspect you want to walk through the two >lists in parallel. So...try this: > > >Define !meanpercent (!positional !charend('/')/ !positional !cmdend) > >*** Process the two lists in parallel *** . >!LET !YCOPY=!2 >!DO !X !IN (!1) >!LET !Y = !HEAD(!YCOPY) >!LET !YCOPY=!TAIL(!YCOPY) > >compute !concat(!X,!Y,"_mp") = !X/!Y. >format !concat(!X,!Y,"_mp") (f5.2). >fre !X !Y /sta mean/format notable. > >!doend >!enddefine. > >!meanpercent s4 s5/ s3 s3. > > >HTH. > > > >Ratna Wynn wrote: >> >> Hi >> >> >> >> I am trying to create a macro that will take a number of numerator >> variables >> and a number of denominator variables and for each set divide the >> numerator >> by the corresponding value in the denominator set. The macros I wrote is >> as >> follows: >> >> >> >> Define !meanpercent (!positional !charend('/')/ !positional !cmdend) >> >> !do !vars !in (!1) >> >> !do !vars2 !in (!2) >> >> compute !concat(!1,!2,"_mp") = !1/!2. >> >> fre !1!2 /sta mean/format notable. >> >> !doend >> >> !doend >> >> !end define. >> >> >> >> !meanpercent s4 s5/ s3 s3. >> >> >> >> >> >> What I would like is based on this get 2 new variables s4s3_mp = s4/s3 and >> S5s3_mp=s5/s3. >> >> >> >> But when I run this it does not work. I don't want to fix the number of >> numerators entered or denominators entered. I want it to be flexible. Any >> help will be appreciated. >> >> >> >> Thanks, >> >> >> >> Ratna >> >> >> >> >> >> >> >> Ratna Wynn >> >> >> >> >> > > >----- >-- >Bruce Weaver >[hidden email] >http://sites.google.com/a/lakeheadu.ca/bweaver/ >"When all else fails, RTFM." > >NOTE: My Hotmail account is not monitored regularly. >To send me an e-mail, please use the address shown above. >-- >View this message in context: >Sent from the SPSSX Discussion mailing list archive at Nabble.com. > >===================== >To manage your subscription to SPSSX-L, send a message to >[hidden email] (not to SPSSX-L), with no body text except the >command. To leave the list, send the command >SIGNOFF SPSSX-L >For a list of commands to manage subscriptions, send the command >INFO REFCARD ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
| Free forum by Nabble | Edit this page |
