## 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

## 19 Comments

1.ben | May 11th, 2007 at 8:48 pmIf 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 amWhat 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 pmThis 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 pmI 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 amFor 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 pmThe assign missing to 0 and add the two variables together worked for me. Thank you.

8.Rediviva | September 24th, 2008 at 5:38 amMy 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 amhello! 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 amI 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 pmI want to combine three scale variables into a new scale variable.

help please!

12.Katelyn | February 10th, 2009 at 6:02 pmI 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 amHELP!! 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 pmI’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.Gideon | July 22nd, 2011 at 6:43 amHi, I am working with SPSS but I am stuck and need help. I have entered data in SPSS and entered 20 questions (Names). However upon the 2nd thought, I want to merge two questions each into One without losing the response. In the process, i wish to get average of each two questions into one, giving me only 10 questions (names).

Since each respondent responded to all the questions as entered in teh coding as Name, I dont know what to do again.

some one should help please

16.Gideon | July 22nd, 2011 at 6:44 amThe data is a scale measure please

17.Joelle | August 18th, 2011 at 1:53 amPlease urgent help needed!!

I want to transform 1 variable into multiple ones,

or let spss get values such as “12″ as “1″ and “2″.

Here’s the problem:

I have a variable that could have multiple values: 1, 2, 3 or 4. some have both 1 and 2 (input 12), others 2 and 3 (23), and others 3 and 4 and 5 (345).

I really searched everywhere and didn’t find where I can just tell SPSS to take 12 as 1 and 2.

I started “recoding” into different variables, but I have a loot of variables and a lot of combinations, having sometimes values of 1 to 6.

Ur help is much needed and appreciated

thanks

18.Jane | September 1st, 2011 at 8:51 amI am trying to recode two variables into one new variable. The first variable has the possible values of (1,2) and the second variable has the possible values of (1, 2). I would like to recode the new variable as a 1,2,3,or 4 depending on the combination of the two original variables scores (i.e. 1,1=1; 1,2=2; 2,1=3; and 2,2 =4). I have searched and cannot find a recode or compute syntax that works. Can someone please help?

Many thanks,

19.Jessie | September 23rd, 2011 at 5:31 amPlease help me!!

I am trying to create a new variable (1=yes 2=no) which combines 5 variables I already have. I have attempted to write a syntax (as I think there are 7 cases no and 1 yes) but it isn’t working and keeps giving me all no’s and not any yes’s.

I wrote this:

IF(B1_victim=2)Newvar=2

IF((B1_victim=1)&(B2A_victim_violent=1))Newvar=1

IF((B1_victim=1)&(B2A_victim_violent=2))Newvar=2

IF((B1_victim=1)&(B2A_victim_violent=3))Newvar=2

IF((B1_victim=1)&(B2C_victim_property=1))Newvar=2

IF((B1_victim=1)&(B2C_victim_property=2))Newvar=2

IF((B1_victim=1)&(B2C_victim_property=3))Newvar=2

EXECUTE.

but it wont work….

Secondly, once I have created this new variable, I want to perform a cluster analysis on 3 variables (this new variable, and two continuous variables ranging 1-7) to see how the variables naturally cluster into groups.

But I need help!

JESSIE