Good day.
I have 4 dichotomous variables, let's call them aq_1 aq_2 aq_3 and aq_4. Each variable is answered yes/no (0/1). I would like to combine these variables into one variable 'aq_combined'. The following code has not worked as the counts are not the same as when I run each variable independently. Wondering if anyone had any insight, thanks!
if aq_1 =1 then aq_combined=1;
if aq_2=1 then aq_combined=2;
if aq_3 =1 then aq_combined=3;
if aq_4 =1 then aq_combined=4;
Hi,
Here is the code. What you can do is use an array, as all are aq_ prefix, and take max of that array, which will be 1 if 1 is present in any, and 0 if not.
data have; input aq_1 aq_2 aq_3 aq_4; datalines; 1 0 1 0 0 0 0 0 1 1 1 1 ; run; data want; set have; array aq_{4}; aq_combined=max(of aq_{*}); run;
I would try it this way:
aq_combined = aq_1 * 1000 + aq_2 * 100 + aq_3 * 10 + aq_4;
This creates aq_combined as numeric. The best answer might depend on how you are going to use aq_combined later.
It means you have cases where aq_1=1 and aq_2=1 or any of the other aq_ variables. You can run a proc freq to check it.
data check;
set have;
if sum(of aq_1-aq_4)>1 then output;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.