Search code examples
spss

SPSS Macro to search variable contents and assign new variable?


I'm trying to simplify some spss syntax I've inherited. The syntax is iterated hundreds of times.

I have 80 or so variables with text in them that say "Award1" "Award2" etc all the way up to "Award17"

The syntax as it's written now:

if (NYSRIRP = "AWARD17") NYSRIRP_AMT = Award17_AMT.

Is there a macro to iterate this code for every Award number and/or every variable eg. NYSRIRP (there are 80 of these).


Solution

  • Not sure exactly how your data looks, so I'm sort of guessing here. If this doesn't fit the data please edit your question and add a description of the data and what the code is supposed to do.

    define !doawards (!pos=!cmdend)
    !do !i !in(!1)
    !do !j=1 !to 17
    if !i = !quote(!concat("AWARD", !j)) !concat(!i,"_AMT") = !concat("Award", !j, "_AMT").
    !doend
    !doend
    !enddefine.
    

    After running the macro definition above, you can call it with a list of the variables you want to iterate on, like this:

    !doawards var1 var2 NYSRIRP var4 var5 .
    

    The macro will iterate on your list of variables (please name all variables individually, don't use var1 to varX). For each variable it will iterate on numbers 1 to 17, and re-build your original command with the relevant variable name and relevant number.