4

I need to run the below in SPSS:

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 a macro that "If event_duration = X, Mean the Activity variables up to Activity_X".

I have to do this over 15 variables, so although I could type them all out I assume there is some code that will simplify it?

ZygD
  • 22,092
  • 39
  • 79
  • 102
Dan
  • 41
  • 1

3 Answers3

4

Here's a demo how to wrap this into a SPSS DEFINE-!ENDDEFINE macro, I've parametrized N=120 but you could just as well parametrize for the 15 different variables you need to run this for also. I'll leave that to you to adapt.

define !RunJob(n=!cmdend)

!do !i = 0 !to !n
    !if (!i=0) !then
        do  if (event_duration = !i).
            compute Acti_Activity =Acti_Activity.
    !ifend
    !if (!i>0) !then
        else if (event_duration = !i).
            compute Acti_Activity =MEAN(Acti_Activity to !concat("Acti_Activity_",!i)).
    !ifend

!doend
end if.      

!enddefine.

set mprint on.
!RunJob n=120.

If for whatever reason your variables are not in contiguous order, or say Activity_15 doesn't exist in the data, then this approach shall fail and stop when an error is encountered. If so, then you may want to approach this using Python Programmability, which is much more dynamic and flexible in such instances.

Jignesh Sutar
  • 2,909
  • 10
  • 13
  • I've updated my post to use DO IF rather than IF for testing the logical condition as this will be faster than repeatedly testing the IF conditional across all cases each time. – Jignesh Sutar Apr 28 '15 at 10:58
  • It looks like to me you are missing an "ELSE IF" statement nested below the "!else" macro statement. Also might as well mention when giving answers to people in [multiple places](http://spssx-discussion.1045642.n5.nabble.com/Simplifying-syntax-td5729393.html), so no one duplicates efforts already made. – Andy W Apr 28 '15 at 15:13
  • Thanks Andy. I had it coded such that it was repeatedly evaluating DO IF / END IF, rather than in the first iteration a DO IF followed by ELSE IF thereafter. I've updated the post. – Jignesh Sutar Apr 28 '15 at 15:39
0

I was wondering. When event_duration equals 0 means that variables Activity_1 through Activity_119 are all zero, and when event_duration equals 1 it means that Activity_2 through Activity_119 are zero, and so on, you could also use something simpler like this.

compute Acti_Activity = SUM(Activity to Activity119) / (event_duration + 1).
execute.
MA53QXR
  • 82
  • 2
  • 11
  • Close! Very close! But that wouldn't work because, say for example, when Event_Duration=2 you want to sum only Activity1 and Activity2 and NOT as in your case Activity1 to Activity119? Your method would work if when Event_Duration=2 then only Activity1 and Activity2 have valid data and missing for all other activity variables so is dependent on the quality and structure of the data. – Jignesh Sutar Sep 28 '16 at 09:30
0

DEFINE !SPGrid (ID=!Default(respid) !TOKENS(1)/ Codes = !TOKENS(1000)/ N = !TOKENS(1)/ Question = !TOKENS(1000)) COUNT QCount = !Question (!Codes). EXECUTE. TEMPORARY. SELECT IF (QCount<>!N). LIST !ID !Question. !ENDDEFINE.

!SPGrid Codes=1,2,3,4 N=15 Question=Q1r1 Q1r2 Q1r3 Q1r4 Q1r5 Q1r6 Q1r7 Q1r8 Q1r9 Q1r10 Q1r11 Q1r12 Q1r13 Q1r14 Q1r15.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 16 '22 at 18:04