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!
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.
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 = '' ;
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.
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.
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!
> " "
> 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.
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!
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.
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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.