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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.