SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
  • 1.  Recode dynamic set of variables based on user input

    Posted Wed March 29, 2023 06:38 PM

    Hello,

    I am trying to figure out a way to recode a group of variables where the variables have the same prefix and are sequentially numbered with a suffix, but the total number of variables is dynamic.

    For example, I have these variables:

    a_1, a_2, a_3 ... a_x

    b_1, b_2, b_3 ... b_x

    c_1, c_2, c_3... c_x

    The total number of a_, b_, and c_ variables are equal. I need to do the following...

    recode a_1 to a_x (1=1) (2=0).

    recode b_1 to b_x (1=1) (2=0).

    recode c_1 to c_x (1=1) (2=0).

    The value of x is going to vary depending on the dataset I am running the code on. I want to allow the user to define the value of x before running the syntax.

    I know I could have the user do something like this to define a constant, but I'm not sure how I could then reference !x in my recode syntax:

    define !x()
    229
    !enddefine.

    Is it possible to somehow concatenate the "a_", "b_", or "c_" with !x in my recode syntax? I know there are ways to do this with python integration, but I am unable to use that at my workplace. Anyone know of any workarounds I could use to accomplish this?

    Thanks!



    ------------------------------
    Alexa Bolwin
    ------------------------------


  • 2.  RE: Recode dynamic set of variables based on user input

    Posted Wed March 29, 2023 06:55 PM
    This is a situation where you can exploit the pattern in the variable names to automate the recode.
    Go to Utilities > Define Variable Macro.
    Enter the name pattern in the dialog like this
    a_\d+
    and assign a macro name like !avars.
    You can clear out the other dialog settings.  The generated syntax would look like this, so you could just type it in.

    SPSSINC SELECT VARIABLES MACRONAME="!avars"
    /PROPERTIES PATTERN = "a_\d+".

    That will generate a macro named !avars listing all the variable namess that match the pattern.  The pattern notation means the name must start with "a_" and be followed by one or more digits.

    Then just use that in the recode.
    recode !avars (1=1) (2=0).

    and so on.

    --