dacoda
dacoda

Reputation: 167

SPSS syntax: recode in nested loop

I hope you can help me with my problem regarding a recode in a nested loop (SPSS syntax). :)

In my SPSS database I've got blood level measurements (3 per week over 20 weeks) for different drugs. I want to create boxplots for each Substance, using recode to generate a new variable for each substance.

The following process describes the process for ONE substance. I can abstract the rest having the solution for one substance.

So there would be the way by clicking via GUI, which is very exhausting for so many measurements - and error-prone -, so I would like to do this with SPSS syntax.

Just as a reminder: 3 measurements per week (1-3) over 20 weeks (0-19);

The RECODE for the first measurement of the first week looks like this:

DO IF (W0_TDM_1AP=1).
RECODE
 W0_TDM_1Erg
 (ELSE=Copy) INTO AMS.
END IF.
EXECUTE.

If I wanted to implement this with Python or PHP, it would look like this (pseudo-code):

for($i=0; $i<19; $i++)
{
  for($j=1; $j<3; $j++)
  {
    DO IF (W{$i}_TDM_{$j}AP=1).
    RECODE
     W{$i}_TDM_{$j}Erg
     (ELSE=Copy) INTO AMS.
    END IF.
    EXECUTE.
  }
} 

My basic idea was: Nested LOOP and on the inner loop execute the recode. As $i and $j would be integers, I would have to do a cast to string and then check for the condition: (pseudo-code)

LOOP #i=0 TO 19 .
 LOOP #j=1 TO 3 .
  var1 = CONCAT("W",STRING(#i),"_TDM_",STRING(#j),"AP") .
  var2 = CONCAT("W",STRING(#i),"_TDM_",STRING(#j),"Erg") .
  DO IF (var1=1) .
   RECODE
    var2
    (ELSE=Copy)  INTO  AMS .
  END IF .
  EXECUTE .
 END LOOP .
END LOOP .

I'm not very familiar with SPSS syntax, but this would be my basic idea how it could work. What I need is the actual working syntax code for my PHP/Pythonish pseudo code. :-)

Upvotes: 1

Views: 1233

Answers (1)

Jignesh Sutar
Jignesh Sutar

Reputation: 2929

To translate the snippet of code you provided, this would be the equivalent in SPSS, which uses SPSS's Macro language facility :

define !RunJob ()
!do !i = 0 !to 19
  !do !j = 1 !to 3
     do if (!concat("W", !i, "_TDM_",!j, "AP"))=1.
         recode !concat("W", !i, "_TDM_",!j, "ERG") (else=copy) into AMS.
     end if.
  !doend
!doend
!enddefine.


set mprint on.
!RunJob.
set mprint off.

Upvotes: 1

Related Questions