BookmarkSubscribeRSS Feed
GregBond
Obsidian | Level 7

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;

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Astounding
PROC Star

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.

Reeza
Super User

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1366 views
  • 3 likes
  • 4 in conversation