Search code examples
macrosspss

Loop for automated formatting of variables in SPSS


I have a file with the following variables and associated formats.

ID --> String
Var1_flag1 --> Numeric
Var2_flag2 --> Numeric
Var3_flag1 --> Numeric
Var4_flag3 --> Numeric
Var5_flag3 --> Numeric
Var6_flag3 --> Numeric
Var7_flag2 --> Numeric
Var8_flag2 --> Numeric
Var9_flag1 --> Numeric

I would like to define the formats based on a regular expression within the variable names (rather than having to explicitly define the variables for each column name).

For example I would like all variables that have flag1 in the name to be formatted to F10.3, flag2 to be formatted to F10.2 and flag3 to be formatted to F10.1 and all columns that don't match these conditions to retain their existing format.


Solution

  • You can use spssinc select variables to create lists of variables according to an attribute like you described, and then use the list in your syntax. For example:

    spssinc select variables macroname="!flag1" /properties pattern=".*flag1".
    spssinc select variables macroname="!flag2" /properties pattern=".*flag2".
    spssinc select variables macroname="!flag3" /properties pattern=".*flag3".
    alter type !flag1 (F10.3) !flag2 (F10.2) !flag3 (F10.1) .