2

I've done quite a bit of searching on this, and haven't come up with a clear solution. I'm much better with SPSS syntax than I am python, so bear with me.

I have an SPSS dataset with a ton of variables that get recoded into different forms, and I'm trying to clean up the syntax, and I'm almost there.

The dataset comes from an excel file that has the full variable label as the column header, so when SPSS imports it the unedited version is stuck in the variable label field where I want it; in other words I am not explicitly defining the variable labels at any point.

After I recode the variables I need to copy the original variable label and append 'Recoded: ' to the beginning. I'm trying to avoid several hundred lines of explicitly defining the variable labels on all the recoded variables.

The problem I'm running into is one of the labels contains a forward slash, and using the syntax in the example below results in the slash being treated as an exit character within the syntax, and the label that contains the slash is only partially copied to the recoded variable.

data list /var001 1 var002 5.
begin data
3   4
4   5
2   3
5   5
end data.
variable labels
var001 "Variable label that contains apostrophes' but not forward slashes"
/var002 "Variable label that contains apostrophes' and/or forward slashes".
RECODE var001 var002 (1 thru 3 = 0) (4 thru 5 = 1) INTO var001_r var002_r.
EXECUTE.

begin program.
variables='var001 to var002'
import spss, spssaux, spssdata
spss.Submit("set mprint on.")
vdict=spssaux.VariableDict(caseless=True)
varlist=spssaux.VariableDict().expand(variables)
for var in varlist:
    vlab = vdict[var].VariableLabel
    spss.Submit("VARIABLE LABEL %s." % ("\n" .join([r"%s_r ""Recode: %s"" " % (str(var), vlab)])))
spss.Submit("set mprint off.")
end program.

1 Answer 1

2

Don't actually need python for this one. The following syntax uses OMS to capture var names & labels from dictionary command and uses them to write a new syntax which creates the new labels:

dataset name orig.
DATASET DECLARE  vrlst.
OMS   /SELECT TABLES  /IF COMMANDS=['File Information'] SUBTYPES=['Variable Information']
  /DESTINATION FORMAT=SAV  OUTFILE='vrlst' .
DISPLAY DICTIONARY.
omsend.
dataset activate vrlst.
select if Label<>"<none>".
string cmd (a100).
compute cmd=concat("var labels ", rtrim(var1), '_r  "recoded: ', Label, '".'). 
write out="path\recode labels.sps" /cmd. 
dataset activate orig.
insert file="path\recode labels.sps".
1
  • 1
    Thank You! that works phenomenally. I hadn't thought of using OMS!
    – JYurkovich
    Nov 14, 2018 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.