1

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. :-)

4
  • 1
    Why would you want to split this out into a set of variables? One for each drug? Wouldn't it be prefereable to have in long format as it is (as far as I can tell - despite any explicit mention). May be worth describing the file structure and your final goal exactly. Commented Oct 22, 2015 at 9:25
  • No, I need to pool / collect all measurements (3 per week for 20 weeks) for one substance and then create boxplots based on the collected data of this new variable. I can do this by recoding via GUI, works like a charm, but for each measurement I have to do all the clicking again (60 times for one substance), so this would be the perfect job for a nested loop, which counts through the weeks and measurements recoding into the same new variable (here called AMS). My final goal is to have all matching measurements recoded into one new variable. :) Commented Oct 22, 2015 at 9:37
  • 1
    Couldn't SPLIT FILE be of use to you here? Commented Oct 22, 2015 at 9:51
  • Nice idea, but after splitting I would be at the same point as now, because I still have to recode all measurements for the boxplots. I would have all related data for one substance, but it's spread over all measurements over 20 weeks. Commented Oct 22, 2015 at 10:35

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.