I have an incredibly large syntax, mainly due to my amateurish coding. If someone could help me with the problem below it would be greatly appreciated:
I have syntax for the below: if (event_duration = 0) Acti_Activity = Activity. if (event_duration = 1) Acti_Activity = MEAN (Activity, Activity_1). if (event_duration = 2) Acti_Activity = MEAN (Activity, Activity_1, Activity_2). …all the way up to… if (event_duration = 120) Acti_Activity = MEAN (Activity, Activity_1, Activity_2, Activity_3, Activity_4, Activity_5, Activity_6, Activity_7, Activity_8, Activity_9, Activity_10, Activity_11, Activity_12, Activity_13, Activity_14, Activity_15, Activity_16, Activity_17, Activity_18, Activity_19, .... Activity_120). So, essentially, I need some code that says "If event_duration = X, compute the mean for all 'Activity' variables up to and including Activity_X)". Any help would be greatly appreciated. It's probably a simple macro I'm overlooking! Cheers Dan |
Administrator
|
Untested:
Assuming vars are contiguous. ---------------------------------------------------- VECTOR v_Activity=Activity TO Activity_120. COMPUTE Acti_Activity =0. LOOP #=1 TO event_duration+1 . COMPUTE Acti_Activity =SUM(Acti_Activity ,v_Activity(#). END LOOP. EXECUTE. If you have a lot more of this sort of thing I do contract SPSS programming and training.
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?" |
In reply to this post by dp1g08
Oh my, if nothing the TO convention really should be used rather than explicitly listing out all the variables. And if the various are not in contiguous order then that is whole another matter and bad data management too. Try below. define !RunJob(n=!cmdend) !do !i = 0 !to !n !if (!i=0) !then if (event_duration = !i) Acti_Activity =Acti_Activity. !else if (event_duration = !i) Acti_Activity =MEAN(Acti_Activity to !concat("Acti_Activity_",!i). !ifend !doend !enddefine. set mprint on. !RunJob n=6. On 28 April 2015 at 10:43, dp1g08 <[hidden email]> wrote: I have an incredibly large syntax, mainly due to my amateurish coding. If |
Administrator
|
But the test value will not be a constant across cases so the Macro !DO is not the way to go.
The VECTOR/LOOP is very fast and fairly general. --
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?" |
Administrator
|
In reply to this post by David Marso
Here are a couple tweaks (also untested) to David's VECTOR-LOOP method. The main change reflects the fact that the OP wants Act_Activity to equal a mean, not a sum. The other change--the final bracket was missing on the COMPUTE line inside the loop.
VECTOR v_Activity=Activity TO Activity_120. COMPUTE #sum = 0. . LOOP # = 1 TO event_duration + 1 . . COMPUTE #sum =SUM(#sum ,v_Activity(#) ). END LOOP. COMPUTE Acti_Activity = #sum / (event_duration+1). EXECUTE.
--
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/). |
Thanks everyone... success! :-)
|
Administrator
|
In reply to this post by Bruce Weaver
Good catch Bruce.
I shouldn't post code before I wake up ;-) --
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?" |
In reply to this post by Bruce Weaver
Just to be contrarian here is a one-liner using Python and SPSSINC TRANS. (Much slower, and not as simple as the loop IMO, but hey always good to have options!)
DATA LIST FREE /N X1 TO X4. BEGIN DATA 1 1 1 1 1 2 1 1 1 1 3 1 1 1 1 4 1 1 1 1 4 2 1 2 1 4 1 1 3 1 END DATA. DATASET NAME Test. SPSSINC TRANS RESULT=B TYPE=0 /VARIABLES X1 TO X4 /FORMULA "sum([<>][0:int(N)])/N". |
Administrator
|
That my friend is what I categorize as PFTSOP.
Waiting to see what creative unpacking of that acronym comes out of the peanut gallery ;-) --
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?" |
Administrator
|
Unfortunately, it came to me almost immediately. And I agree. ;-)
--
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/). |
I guess because PFTSOP has long meant "Protective Force Training Standard Operating Procedure" to me I miss what David meant. Feel free to send the translation to me by direct email.
Art Kendall
Social Research Consultants |
In reply to this post by Bruce Weaver
Posted by the two most determined Python
resisters who post on this list...
Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: Bruce Weaver <[hidden email]> To: [hidden email] Date: 04/28/2015 02:38 PM Subject: Re: [SPSSX-L] Simplifying syntax Sent by: "SPSSX(r) Discussion" <[hidden email]> Unfortunately, it came to me almost immediately. And I agree. ;-) David Marso wrote > That my friend is what I categorize as PFTSOP. > Waiting to see what creative unpacking of that acronym comes out of the > peanut gallery ;-) > > -- > Andy W wrote >> Just to be contrarian here is a one-liner using Python and SPSSINC TRANS. >> (Much slower, and not as simple as the loop IMO, but hey always good to >> have options!) >> >> DATA LIST FREE /N X1 TO X4. >> BEGIN DATA >> 1 1 1 1 1 >> 2 1 1 1 1 >> 3 1 1 1 1 >> 4 1 1 1 1 >> 4 2 1 2 1 >> 4 1 1 3 1 >> END DATA. >> DATASET NAME Test. >> >> SPSSINC TRANS RESULT=B TYPE=0 /VARIABLES X1 TO X4 /FORMULA >> "sum([<>][0:int(N)])/N". ----- -- 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: http://spssx-discussion.1045642.n5.nabble.com/Simplifying-syntax-tp5729393p5729411.html 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 |
Administrator
|
So Jon got it too. ;-)
--
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/). |
Administrator
|
In reply to this post by Jon K Peck
Jon, I use python when it is necessary. Not just python for the sake of python. Do you have an issue with my solution? Can you provide anything better? PFTSOP LOL. KISS......
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 |