Home > database >  Repeating syntax for a list of variables in SPSS
Repeating syntax for a list of variables in SPSS

Time:01-12

I have a dataset with 134 different variables, I have now one variable for where the syntax is working and this runs fine on my SPSS. However, I don't want to have to repeat this step by hand 133 times more, as it is a lot of work. I want to know the string term present in AE_term1 and create a new variable where it is classified as a number, called AE_term1num. However, I don't know how to write a syntax that automatically does this for all 134 AE_term. Could someone please help me?

The data looks like this:

AE_term1 AE_dur1 AE_term2 AE_dur2 etc
Cystitis 14 Heamaturia 3
uti 15 Sepsis 8
etc etc etc etc

To identify the string variable, i have written the following syntax:

`compute AE_term1num=0.
do if (char.index(lower(AE_term1), "cystitis")).
compute AE_term1num=1.
else if (char.index(lower(AE_term1), "uti")).
compute AE_term1num = 2.
else.
compute AE_term1num=0.
end if.
execute.`

This syntax works for finding the correct values and returns what i want, however I do not know how to loop it over the remaining 133 AE_term variables, if this is even possible.

CodePudding user response:

To start with, your original syntax can be streamlined like this:

compute AE_term1num=0.
if char.index(lower(AE_term1), "cystitis") AE_term1num=1.
if char.index(lower(AE_term1), "uti") AE_term1num = 2.

Now if the same conditions exactly apply for all the rest of the variables, you can use do repeat to automate the process:

do repeat AE_term = AE_term1 to AE_term134 / AE_termN = AE_termN1 to AE_termN134.
    compute AE_termN=0.
    if char.index(lower(AE_term), "cystitis") AE_termN=1.
    if char.index(lower(AE_term), "uti") AE_termN= 2.
end repeat.

NOTE - using AE_term1 to AE_term134 - existing variables - requires that they be consecutive in the dataset. Using AE_termN1 to AE_termN134 to create new variables requires that the numbers be at the end of the name, so AE_term1num to AE_term134num wouldn't work.

  • Related