Contributor
Posts: 26

Creating a Variable

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;

Super User
Posts: 9,599

Re: Creating a Variable

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;
```
Super User
Posts: 6,774

Re: Creating a Variable

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.

Super User
Posts: 23,724

Re: Creating a Variable

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;
``````
Discussion stats
• 3 replies
• 201 views
• 3 likes
• 4 in conversation