Combine two variables into one
April 10th, 2007 andris
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 .
EXECUTE .
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.
Entry Filed under: 2. Enhancing your data, 5. Coding SPSS Syntax
6 Comments Add your own
1. ben | May 11th, 2007 at 8:48 pm
If both variables are numeric, all you have to do is recode each original variable so system-missing values = 0. Then simply compute the result variable = original1 + original2. That should do the trick. I’m assuming your data looks likethis:
v1 v2 res
2 . .
5 . .
. 9 .
. 7 .
so recode v1 and v2 to look like this:
v1 v2 res
2 0 .
5 0 .
0 9 .
0 7 .
then when result is the sum of v1+v2 it will be:
res
2
5
9
7
2. Andrea | May 25th, 2007 at 10:44 am
What if there are overlapping records? How would one combine two variables in order to know if respondants answered yes to either variable1 or variable2…to create a combined variable of “yes to either v1 or v2″ or “no to both”?
Thanks.
3. Wayne | May 29th, 2007 at 7:00 pm
This seems awfully simple but I haven’t found the solution yet.
I want to combine 2 variables “A” and “B”.
Each can be 0, 1, or missing.
If either is 1, then I want AB to be 1.
If BOTH are 0, then I want AB to be 0.
Otherwise I want AB to be missing.
Help??
4. Shawne | June 28th, 2007 at 12:59 pm
“This seems awfully simple but I haven’t found the solution yet.
I want to combine 2 variables “A” and “B”.
Each can be 0, 1, or missing.
If either is 1, then I want AB to be 1.
If BOTH are 0, then I want AB to be 0.
Otherwise I want AB to be missing.”
Off the top of my head, you could try computing a new variable as “1″ to account for A or B having a 1. You could then do an “IF” scenario where the new variable equals “0″ provided that A and B equal 0. To give you an idea of the syntax:
COMPUTE C = 1.
IF (A = 0 AND B = 0) C = 0.
That is, your new variable will contain a zero under the condition that both A and B contain a 0. This assumes, however, that every case for A and B has a valid entry. Otherwise, you will potentially end up with C = 1 for instances where A and/or B have a missing value. To account for this, you could try creating an empty variable then apply the appropriate conditional statements for each outcome you want - if A = 1 and B = 0 then C = 1, etc.
5. Thomas | June 30th, 2007 at 11:57 pm
I am still learning to SPSS, but my variable combination issue is different. I want to take the values of v1 and the values of v2 and put them in a new variable that has the values of v1v2. For example, v1 has a value of 1104 and v2 has a value of 34. I want to new variable to read 110434. It can be string of numerical. How do I do this? Thanks in advance for your assistance.
6. Smash | August 26th, 2007 at 9:08 am
For Wayne question (comment 3):
compute C=$sysmis.
if (A=0 & B=0) C=0.
if (A=1 | B=1) C=1.
exec.
For Thomas question (comment 5):
If there are sting variables, use the CONCAT function (to join variables) and RTRIM function (to remove blanks).
If both variables are absolute numerical, then you can transform them into string and add like previously (easy solution) or use formula:
V1V2=nr*V1+V2, where nr is number of digits “0″ to add into V1, so the digits number of V2, calculated as below:
compute nr=10.
compute #tmp=V2.
loop #i=1 to 10.
do if (#tmp>10).
compute #tmp=trunc(#tmp/10).
compute nr=nr*10.
end if.
end loop.
exec.
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed