Posts filed under '5. Coding SPSS Syntax'
One of the many questions we got the last few days was from Dana. Dana asked us a question about combining two variables into one:
“Hello, I need to essentially combine two variables that have been
standardized into one new variable.Â Data that is present in one variable
is missing on the other and vice versa.Â I tried making a new variable and
then recoding missing data so it would pull in the values from the other
variable.Â Can you only recode missing values into numbers, or can I pull
an entirely different variable into it?Â Basically, how do I combine two
variables into one?Â Is there an easier way? Thanks!”
Well, there certainly is an easier way to combine two variables. Let’s assume you have the variables VAR00001 and VAR00002 which you want to combine into one new variable. We also assume that they do not have overlapping records, with which I mean that there is no record which has a value in both variables. To combine the two, you can use the following SPSS Syntax:
COMPUTE NEW_VAR = VAR00001.
IF (VAR00002 = 4) NEW_VAR = 4 .
IF (VAR00002 = 5) NEW_VAR = 5 .
IF (VAR00002 = 6) NEW_VAR = 6 .
The first part (COMPUTE NEW_VAR = VAR00001.) creates the new variable and copies all values fromÂ VAR00001 into the new variable. The second part copies the values from VAR00002 to the new variable, for the values 4, 5 and 6. Very probably, your second variable contains different values, so adjust the Syntax to your needs.
April 10th, 2007
Today, Aleksandar sent us the following question:
“I’m having problems to recode system missing values to 0 with syntax
editor. How can I do that?”
In SPSS Syntax, missing values can be addressed via the keywords “sysmis” or “missing”. For this explanation we assume that in addition to the values 1 and 2, there are also values 3 and 4 which are defined as missing values. Please find some syntax below and an explanation of what they do. var2 stands for the variable name we are recoding.
RECODE var2 (missing = 3).
All missing values (i.e., values 3 and 4 and perhaps also system missing values) will have the value 3. 3 will still be defined as missing.
RECODE var2 (missing = 15).
All missing values (i.e., values 3 and 4 and perhaps also system missing values) will have the value 15. 15 will not be defined as missing.
If you have system missing values and want to recode only these to another value, you can use the keyword “sysmis” instead of “missing”.
RECODE var2 (sysmis = 6).
Again, if you have defined values 3 and 4 as missing, 6 will not be recognized as a (formerly) missing value. You may wish to either define 6 now as missing or to assign a value label that tells you the meaning of 6.
December 7th, 2006
This week we got a question from Timo.
Is it possible to use syntax when recoding variables? For example, if I
had a variable that included the following values:
and I wanted to recode any values that included ‘bird’ into a new value
‘bird’, can I do this with the Recode transformation?
To solve to problem the following syntax is an option:
DATA LIST LIST /var1(A15).
DO IF INDEX(UPCASE(var1),”BIRD”) > 0 .
- COMPUTE newVar=”BIRD”.
Do you also need an answer to your SPSS question, submit your question here.
August 17th, 2006
I am also writing webpages, and code (X)HTML and PHP by hand. If you code by hand, any code, and also SPSS syntax code, than it is very usefull to have syntax highlighting. Syntax highlighting is a feature of text / code editors that highlights or colourises code. By colourizing code you can very easily recognise types of code.
Recently in the SPPS newsgroup (comp.soft-sys.stat.spss, read it with Google Groups) there has been a discussion or question started by Aldo Cimino on this matter. The answers in the discussion where pointing at the FAQ of SPSSTools.net where you can find an explanation on using Textpad to edit SPSS syntax. ZEUS Editor also supports adding a new language to their definitions. VIM Editor has a file which supports SPSS Syntax, you could try that one, but the author already mentions it needs updating. Crimson Editor already has a SPSS package ready. If you decide to use one of the mentioned tools, or have other suggestions, please let us know! We would be happy to publish your experiences.
March 20th, 2006