Combine variables

Accepted Solution Solved
Reply
Regular Contributor
Posts: 150
Accepted Solution

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: 5,516

Re: Combine variables

Posted in reply to rfarmenta

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.

View solution in original post


All Replies
Frequent Contributor
Posts: 129

Re: Combine variables

Posted in reply to rfarmenta

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

Posted in reply to LarryWorley

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: 5,516

Re: Combine variables

Posted in reply to rfarmenta

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

Posted in reply to Astounding

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: 5,516

Re: Combine variables

Posted in reply to rfarmenta

> " "

>    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

Posted in reply to Astounding

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: 5,516

Re: Combine variables

Posted in reply to rfarmenta

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

Re: Combine variables

Posted in reply to Astounding

No, participants will not have an answer for both COPD and COPD1 luckily. I was just making sure no data was being lost and that seems to be the case from looking at the data before and after creating the new variable. Thank you vey much for all your help!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 741 views
  • 7 likes
  • 3 in conversation