04-26-2013 03:33 PM
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=.
04-26-2013 03:56 PM
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 = '' ;
04-29-2013 03:48 PM
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.
04-29-2013 04:40 PM
> " "
> 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.
04-29-2013 04:54 PM
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!
04-29-2013 09:06 PM
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.
04-29-2013 10:34 PM
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!
Need further help from the community? Please ask a new question.