BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rfarmenta
Obsidian | Level 7

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

8 REPLIES 8
LarryWorley
Fluorite | Level 6

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 =  '' ;


rfarmenta
Obsidian | Level 7

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.

Astounding
PROC Star

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.

rfarmenta
Obsidian | Level 7

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!

Astounding
PROC Star

> " "

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

rfarmenta
Obsidian | Level 7

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!

Astounding
PROC Star

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.

rfarmenta
Obsidian | Level 7

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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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