3

I'm new to using syntax and need help recoding a long series of variables into new variables.

Essentially the bit of code I am trying to loop is:

RECODE cqC2_1_11 (9 thru Highest=10) (ELSE=COPY) INTO cqC2_1_11_T3.
VARIABLE LABELS  cqC2_1_11_T3 ‘CQC2Top3Box’.
EXECUTE.

I need to do that for variables cqC2_1_11 thru cqC2_21_415 (these variables are all sequentially next to each other).

If it's too complex to recode into a new variable because I need to specify each new variable name, I can simply recode into the same variable and save a new copy of the dataset for the analysis I need to run.

mirirai
  • 1,365
  • 9
  • 25
Chad
  • 31
  • 2
  • What is the rule/pattern for choosing the variable labels? – mirirai Feb 17 '16 at 19:16
  • Note, I suspect, you have a 10point scale you are tying to dichotomize? Your `RECODE` specification should be `(8 THRU HI=1) (ELSE=0)`? Also, you wouldn't want a generic ‘CQC2Top3Box’ variable label for all your recoded variables else your management of any output generated would be somewhat difficult. So the solution I provide below retains and transfers original variable label with a added prefix "T3B". – Jignesh Sutar Feb 17 '16 at 19:52

1 Answers1

2

Firstly, RECODE command can take in (and output) multiple variables. See the documentation and the example provide here. Though the first example provides in that link recodes into the same variables, an equivalent command to recode into different variables would be:

RECODE V1 TO V3 (0=1) (1=0) (2,3=-1) (9=9) (ELSE=SYSMIS) INTO W1 to W3.

The above works because it is intutive for SPSS to interpret and expand out W1 TO W3 to equal W1, W2, W3.

The difficulty you have, although SPSS can read sequential variables cqC2_1_11 TO cqC2_21_415 as they are input existing variables in the dataset, it is not easy to specify equivalent matching output variable names.

For example, you could not simply specify RECODE....INTO T3B_cqC2_1_11 TO T3B_cqC2_21_415 as you have double indexing occurring and SPSS would not know exactly how to expand out those names.

If however, for example you had cqC2_1 TO cqC2_415 i.e. 415 variables you could use RECODE....INTO T3B_cqC2_1 TO T3B_cqC2_415.

So your best option is to build a custom macro, using either SPSSs build in macro facility or its integrated python programmability.

Python programmability is far superior so below is python solution you can adapt for your specific needs and criteria, to help you get started:

get file="C:\Program Files\IBM\SPSS\Statistics\23\Samples\English\Employee data.sav". 

begin program.
import spss, spssaux, spssdata
spss.Submit("set mprint on.")
vd=spssaux.VariableDict(pattern="job")

spss.Submit("""RECODE %s (8 THRU HI=1) (ELSE=COPY) INTO %s.""" % ("\n".join([str(v) for v in vd]),"\n".join(["T3B_" + str(v) for v in vd])))
for v in vd:
    spss.Submit("""VARIABLE LABEL T3B_%s "%s".""" %(v, "T3B_" + v.VariableLabel))
spss.Submit("set mprint off.")
end program.
Jignesh Sutar
  • 2,909
  • 10
  • 13
  • Thank you for this response, you are correct that I am trying to create Top 3 Box scores for a series of rating scale questions. However I do not know python at all and would rather not rely on it without understanding what it's doing. Is there a simpler way to simply recode into the same variable so that I don't have to work about the variable label issue? – Chad Feb 17 '16 at 20:00
  • Yepp, if you want to recode into the same variable (and overwrite the original) the solution would be really simple. As Jignesh Sutar mentioned you can specify a set of variables in the recode command. Just use it without the `INTO` keyword: `RECODE cqC2_1_11 TO cqC2_21_415 (9 thru Highest=10) (ELSE=COPY)`. However be carefull not to overwrite your original dataset then ;-) – mirirai Feb 17 '16 at 20:20
  • This was my original attempt and it caused SPSS to display the 'transformations pending' notification, however when attempting to EXECUTE I received the following: >Error # 4617 in column 1. Text: EXECUTE >Something other than the keyword 'INTO' was found on a RECODE command after >the parenthesized value specifications. >Execution of this command stops. However when I ran a descriptives analysis it then populated the cells with the pending transform data. Not sure why EXECUTE doesn't work but forcing it to do an analysis does... – Chad Feb 17 '16 at 21:20
  • 1
    You may want to provide the syntax that generates that error for anyone to have a chance to help. Although, I suspect it is likely due to the fact of the keyword `INTO` is missing in the `RECODE` command. – Jignesh Sutar Feb 18 '16 at 09:22
  • @Chad Maybe you have forgotten the dot at the end of the recode command. – mirirai Feb 18 '16 at 14:54