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
24 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.
7. nahallac | September 17th, 2008 at 10:47 pm
The assign missing to 0 and add the two variables together worked for me. Thank you.
8. Rediviva | September 24th, 2008 at 5:38 am
My problem is that I want to combine two variables (A and B) into a new variable C in such a way that if var A is greater than B then C= A and if B is greater than A then C=B and if they are equal then of C=A and if A and B are missing then system missing.
I am new to SPSS and I can’t seem to get this to work using syntax. I would greatly appreaciate it if someone gave a more detailed description of how to get this to work. Thanks.
9. tinizul | October 3rd, 2008 at 3:46 am
hello! I used the syntax that was in the asnwer to the orignal question and it worked perfect. I now need to combine two variable that were open ended questions, which basically means that the answers are words. Please help!
tinizul
10. stp | November 21st, 2008 at 10:14 am
I want to combine two string variables into a new string variable. I’ve tried recode, but it wont put two into one.
I have Var1 and Var2 each has values A and B.
I want to combine Var1 & Var2 into a new Var3 with all values listed. Respondents were randomized into 2 conditions and did not answer both Var1 and Var2 but only one so there’s no overlap of inconsistent responses.
Help with command?
11. Ro | December 16th, 2008 at 4:26 pm
I want to combine three scale variables into a new scale variable.
help please!
12. Katelyn | February 10th, 2009 at 6:02 pm
I need to recode multiple variables into one single variable. The variables are numeric and there may be some overlapping. For example if one person answered 3 and 4 to 2 separate variables, I would like the new variable so show 3,4. How do I do this? Or if someone answered 4 and nothing else, how do I show just the value 4?
Thanks!
13. Lisa | February 28th, 2009 at 3:11 am
HELP!! can anyone please tell me how to get spss to add together frequency counts in 5 different variables.
14. Andrew | March 2nd, 2009 at 1:16 pm
I’ve used bits of everyones advice and have a solution:
For combining two variables, both with a 0 or 1 responses (real life example):
Compute Attitudes_Concerns=$sysmis.
IF (attitu=0 & concer=0) Attitudes_Concerns=0.
IF (attitu=0 & concer=1) Attitudes_Concerns=1.
IF (attitu=1 & concer=0) Attitudes_Concerns=1.
IF (attitu=1 & concer=1) Attitudes_Concerns=1.
EXECUTE .
For combining more than two variables its important to work out the various combinations of 0’s and 1’s. E.g: for combining three variables:
Compute Adherence_discussion=$sysmis.
IF (comprx=0 & compso=0 & compcq=0) Adherence_discussion=0.
IF (comprx=1 & compso01=1 & compcq=1) Adherence_discussion=1.
IF (comprx=1 & compso01=0 & compcq=0) Adherence_discussion=1.
IF (comprx=0 & compso=0 & compcq=1) Adherence_discussion=1.
IF (comprx=1 & compso=1 & compcq=0) Adherence_discussion=1.
IF (comprx=0 & compso=1 & compcq=1) Adherence_discussion=1.
IF (comprx=1 & compso=0 & compcq=1) Adherence_discussion=1.
IF (comprx=0 & compso=1 & compcq=0) Adherence_discussion=1.
EXECUTE .
15. farzana | June 26th, 2009 at 7:46 am
i have entry four variable (gul, snuff, alcohol, cannabis)separately in yes=1, and no=2.
But now i want to make a new variable in a way like gul=1, snuff=2, alcohol=3, cannabis=4.
please help.
16. Rob | August 5th, 2009 at 12:20 am
gday help please
i have been able to recode most of my synatx as required however i get caught when it comes to negative numbers
eg. of my syntax that worked with only positive numbers:
DO IF (Gender = 1 & Age_C4_recode_grip = 4).
RECODE Grip_Strength_C4 (Lowest thru 18.9=1) (19 thru 24.9=2) (25 thru 28.9=3) (29 thru 31.9=4) (32 thru 99998=5) (ELSE=0) INTO Grip_Strength_C4_female_age4.
END IF.
VARIABLE LABELS Grip_Strength_C4_female_age4 ‘Grip_Strength_C4_female_age4′.
EXECUTE.
my syntax with negative numbers that doesn’t work
DO IF (Gender = 0 & Age_C1_recode_grip = 1).
RECODE Flexibility_C1 (Lowest thru -9.01=1) (-9 thru 2.9=2) (3 thru 8.9=3) (9 thru14.9=4) (15 thru 99998=5) (ELSE=0) INTO Flexibility_C1_male_age1.
END IF.
VARIABLE LABELS Flexibility_C1_male_age1 ‘Flexibility_C1_male_age1′.
EXECUTE.
please help what am i doing wrong?
17. Rob | August 5th, 2009 at 12:46 am
error message
>Error # 4245 in column 87. Text: =
>Unrecognized text appears on the command where either a number, a text string,
>or a keyword was expected.
>This command not executed.
END IF.
VARIABLE LABELS Flexibility_C1_male_age1 ‘Flexibility_C1_male_age1′.
>Warning # 4461 in column 18. Text: Flexibility_C1_male_age1
>An unknown variable name was specified on the VAR LABELS command. The name
>and the label will be ignored.
EXECUTE.
18. RB | August 5th, 2009 at 2:32 pm
I needed to create a unique identifier in my data set so that I could match pre and post-test data. I used the concat fuction to essentially create an ID by drawing from 5 different variables (date of birth, gender, school, etc.). My problem is, if any of those 5 variables has missing data, i want the new id to be considered sysmis. Does anyone know how to do this? Or how to use a recode statement with string variables so that I can tell SPSS that if any of the 5 original variables have missing data then code the new ID variable as missing?
Thanks!!!
19. Franklin | September 7th, 2009 at 2:49 am
Does SPSS remember the formula used in Compute?. Regarding this comment raised earlier, the issue is to know wether thinhgs done in the compute are saved in the spss journal. its easy to find that out practically
20. Macarena | December 25th, 2009 at 1:23 am
Well, first of all I would like to recognize that this blog is extremely useful, and very well done. Secondly, I must confess I’m not good with English, so I will do my best to get understand by you.
I’m trying to create a new variable from 5 original variables. This 5 variables are:
A: “Do you have problem nº1?”
B: “Do you have problem nº2?”
C: “Do you have problem nº3?”
D: “Do you have problem nº4?”
E: “Do you have problem nº5?”
All this variables have just two answers: 1 “Yes”, and 2 “No”.
But, they all have the value 0 for missing answers. In all of this variables there is the same amount of missing, because, the poll was made during a whole year but the first month this questions weren’t applied.
And I want to create one new variable:
X: “Has at least one problem”. And the values would be:
1 “Yes, has at least one problem”, 2 “No, the person has no problems”, 0 “Missing”.
I tried dozens of combinations for the syntax, but none has resulted.
I don’t find the mistake in the following:
Compute
X=1.
If (A=2 & B=2 & C=2 & D=2 & E=2) X=2.
If ((A ~= 1 & A ~=2) | (B ~= 1 & B ~= 2) | (C ~= 1 & C ~= 2) | (D ~= 1 & D ~= 2) | (E ~= 1 & E ~= 2)) X=0.
execute.
One example of error message that I found was:
>Error # 4382 in column 43. Text: :
>An equals sign was not found when expected after a target variable in a
>COMPUTE command.
>This command not executed.
When I skipped this error message, and the command is executed, the results are ridiculous. I hadn’t any missing value, and sometimes the frequencies show that a 75% or more of the interviewed had at least one of that problems, when, it is expectected to be a population smaller than 10%…
Please help me, I need to compare the population with and without problems for an exam, and I can’t continue without this part of the work.
Best wishes,
Merry Christmas,
Macarena.
21. Rachel | January 18th, 2010 at 2:04 pm
I have two variables and essentially want to resolve them into one variable by taking the highest rating from either one of the variables. So i have a parent report and a self-report and I want to combine them into one single report using the higher of the two responses for each case. Can anyone help please?
Thanks!!
22. megha | February 15th, 2010 at 8:12 am
hi
i am working on one research paper. i have 27 variables or we can call dependent variables. i applied factor analysis on them. i got 6 factor. now i want to use these factor for further test. i want to calculate a combined mean for all items which is under factor one by using spss commands. please help to guide how can calculate combine mean for 10 or more variables.
thanks
23. col | March 11th, 2010 at 8:09 pm
1. ben | May 11th, 2007 at 8:48 pm
YOU ARE A LIFESAVER!!
24. Jacob | March 12th, 2010 at 4:47 pm
Hello,
I am trying to do this:
COMPUTE SCIDr = SCIDr_temp .
IF (SCIDr = SYSMIS) SCIDr = SSCHLCDEr .
EXECUTE .
COMPUTE B = A .
IF (B = $SYSMIS) B = C .
EXECUTE .
Basically, the variable A is missing values. I am trying to replace those missing values with the values for C.
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