RECODE in nested LOOP

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

aUser
Posts: 4
Joined: Tue Oct 20, 2015 8:44 am

RECODE in nested LOOP

Postby aUser » Tue Oct 20, 2015 8:57 am

Hi folks,

I would be happy if you could help me with my following problem:

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:

Code: Select all

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

Code: Select all

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.
    }
}
I think everyone who knows a little bit about coding gets what I want to achieve. :)

Now my question for you :

How do I do that in SPSS syntax ?

I tried with nested LOOPs, but it didn't really work, so I'm hoping for your wisdom and experience to solve this problem. :)
JonPedersen
Posts: 119
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: RECODE in nested LOOP

Postby JonPedersen » Wed Oct 21, 2015 11:20 am

Hi,
If I understand correctly, which, I am sorry to say, is not always the case: No need to use recode,
simply use

Code: Select all

IF (W0_TDM_1AP=1) AMS=W0_TDM_1Erg.

and then repeat for the other two. (You can also use a do repeat loop, but that uses more lines of code when you have just three assignents.
best
jon
aUser
Posts: 4
Joined: Tue Oct 20, 2015 8:44 am

Re: RECODE in nested LOOP

Postby aUser » Wed Oct 21, 2015 12:55 pm

JonPedersen wrote:Hi,
...No need to use recode,
simply use

Code: Select all

IF (W0_TDM_1AP=1) AMS=W0_TDM_1Erg.

and then repeat for the other two. (You can also use a do repeat loop, but that uses more lines of code when you have just three assignents.
Hi,

interesting idea, but if I do a

Code: Select all

IF (W0_TDM_1AP=1) AMS=W0_TDM_1Erg.
followed by a

Code: Select all

IF (W0_TDM_2AP=1) AMS=W0_TDM_2Erg.
then the value of AMS would be assigned according to the second line, and not being appended as another value, my first assignment would be lost, right ?

I don't know about SPSS syntax "in a nutshell" :), but if I do

Code: Select all

$AMS = 3.
$AMS = 4.
then the value of $AMS is 4 and not a set of (3,4).

I need to have all values which match $AP=1 out of all measurements over 20 weeks, that's why I use recode. :)
JonPedersen
Posts: 119
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: RECODE in nested LOOP

Postby JonPedersen » Wed Oct 21, 2015 1:42 pm

Hi,
yes right. This depends on what your data structure actually look like, I have to admit I am a bit unclear on that. The most obvious solution would be to use AMS1, AMS2 and AMS3 as new variables, corresponding to your three cases, but I guess this solution is more or less like renaming your original variables. SPSS cannot handle Python or PHP like list type structures. So the alternative would be to use the varstocases command to put each of the observations on a separate row, which might be easier to deal with. (The varstocases syntax is kind of horrible and not something spss should be particulary proud of, but it works).
best
Jon
aUser
Posts: 4
Joined: Tue Oct 20, 2015 8:44 am

Re: RECODE in nested LOOP

Postby aUser » Wed Oct 21, 2015 2:10 pm

JonPedersen wrote:Hi,
... The most obvious solution would be to use AMS1, AMS2 and AMS3 as new variables, corresponding to your three cases, but I guess this solution is more or less like renaming your original variables.
Exactly :)
JonPedersen wrote: SPSS cannot handle Python or PHP like list type structures. So the alternative would be to use the varstocases command to put each of the observations on a separate row, which might be easier to deal with. (The varstocases syntax is kind of horrible and not something spss should be particulary proud of, but it works).
:)

One thought: What does SPSS actually do, if I do a recode via GUI for the first three measurements, all into AMS ?
This works, I already tested it, but I don't want to do this whole clicking scenario 60 times. :)

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

Code: Select all

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 . 
 
As you can see, 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 pseudo code. :)
JonPedersen
Posts: 119
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: RECODE in nested LOOP

Postby JonPedersen » Thu Oct 22, 2015 9:06 am

Hi,
When you use the click-approach, exactly the same thing happens as when you are using syntax.
Your pseudo-code cannot be easily translated into spss, because the var1,var2 is basically storing a variable name on the fly.
Also, what you are doiung is also9 coding everythinmg into one variable, AMS, which will recieve the value of whatever comes last.
You could do it throughthe Python extension, perhaps. But, could you post example of your data file - a bit easier to undertand what you want to achieve then.
Best
Jon
aUser
Posts: 4
Joined: Tue Oct 20, 2015 8:44 am

Re: RECODE in nested LOOP

Postby aUser » Thu Oct 22, 2015 11:40 am

JonPedersen wrote:...
Also, what you are doiung is also9 coding everythinmg into one variable, AMS, which will recieve the value of whatever comes last.
Hi,

I don't think so, because all matching values are copied into the new variable:
When I do the recodes via GUI I get all matching results showing in the boxplots (you can see the number of cases matched).
According to your logic I would only have the number of cases matching the last criteria called, but this isn't the case. :)

Code: Select all

(ELSE=Copy)  INTO  AMS .
The more I deal with this problem, the more I get the impression, that SPSS syntax is not sophisticated enough for such basic operations, which are no problem in other programming languages, I'm afraid.
I'd be happy if someone could prove me wrong. :)

Who is online

Users browsing this forum: No registered users and 1 guest

cron