## Combine variables

Solved
Regular Contributor
Posts: 150

# Combine variables

This is probably a really dumb/simple question but how do I combine variables in SAS? I have 2 variables, COPD and COPD1, some participants answered COPD and others answered COPD1 but the question is the same so I want to combine them to have complete responses for analyses. Thanks for your help. I know how to create a new variable from two variables but I can't remember how to just simply combine two variables. I need to take into account missing data as well, so If COPD=. and COPD1=. then new_var=.

Thanks!

Accepted Solutions
Solution
‎04-26-2013 04:03 PM
Super User
Posts: 6,785

## Re: Combine variables

It looks like you need one less variable, not one more variable.  Maybe this would do what you want:

data want;

set have;

if copd1 > " " then copd = copd1;

drop copd1;

run;

Good luck.

All Replies
Frequent Contributor
Posts: 129

## Re: Combine variables

one way to do this is with if-then statements using logical expressions on both of you original vars.

if copd = 'Y' or copd1 = 'Y' then copd_new = 'Y' ;

else if copd = 'N' or copd1 = 'N' then copd_new = 'N' ;

else copd_new =  '' ;

Regular Contributor
Posts: 150

## Re: Combine variables

This is how I recode categorical variables but I am not combining the variables based on any set of responses, since one variable contains responses for some participants and the other contains responses for a different set of participants. Thank you for taking the time to reply.

Solution
‎04-26-2013 04:03 PM
Super User
Posts: 6,785

## Re: Combine variables

It looks like you need one less variable, not one more variable.  Maybe this would do what you want:

data want;

set have;

if copd1 > " " then copd = copd1;

drop copd1;

run;

Good luck.

Regular Contributor
Posts: 150

## Re: Combine variables

Thank you for your reply, for the code, what does >" " do? For combining variables there does not need to be a certain response entered, correct? Thank you again!

Super User
Posts: 6,785

## Re: Combine variables

> " "

>    greater than

" "    blank

So this statement is checking whether COPD1 contains text.  If it does, it copies that text to COPD:

if copd1 > " " then copd = copd1;

Presumably, COPD was blank before that copying took place.

Regular Contributor
Posts: 150

## Re: Combine variables

Thank you!

I tested out the code and it seems to be doing what I want. One thing, COPD is not a blank prior to the copying, it also has records. Will that affect anything? After running the code I did a proc print to compare the new and old variable and everything seemed to work fine. Thanks!

Super User
Posts: 6,785

## Re: Combine variables

Are you saying that some participants might have an answer for both COPD and COPD1?  If so, you have to decide what to do in that case.  It isn't a programming problem until you decide on the result you would like to see.

This program only copies the COPD1 values that are nonblank.  Presumably the COPD values are originally blank on those observations, so no data is being lost.

Regular Contributor
Posts: 150