Fluorite | Level 6

## Create a new variable based on values

I have a data set below:

 Patient ID Drug 1 Drug 2 Drug 3 Drug 4 Drug 5 Drug 6 Drug 7 1 Statin Statin BB ACEI Statin 2 BB BB BB 3 Statin BB BB 4 ACEI Statin ACEI ACEI 5 ACEI BB 6 Statin Statin Statin 7 BB

How do I create another variable (Total med) where:

If Statin and BB and ACEI are in at least one of the 7 columns, then Total Med = Triple

If ONLY Statin and BB are in at least one of 7 columns, then Total Med = Double_SB

If ONLY Statin and ACEI are in at least one of 7 columns, then Total Med = Double_SA

If ONLY Statin appeared in at least one of 7 columns, then Total Med = Statin alone

If ONLY BB appeared in at least one of 7 columns, then Total Med = BB alone

If ONLY ACEI appeared in at least one of 7 columns, then Total Med = ACEI alone

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: Create a new variable based on values

Something like this in a data step

``````data want;
set have;
length totalmed \$ 15;
/* If Statin and BB and ACEI are in at least one of the 7 columns, then Total Med = Triple */
if whichc('Statin',of drug:)>0 and whichc('BB',of drug:)>0 and whichc('ACEI',of drug:)>0 then totalmed='Triple';
/* If ONLY Statin and BB are in at least one of 7 columns, then Total Med = Double_SB */
if whichc('Statin',of drug:)>0 and whichc('BB',of drug:)>0 and whichc('ACEI',of drug:)=0 then totalmed='Double_SB';
/* You type the rest */
run;
``````
--
Paige Miller
3 REPLIES 3
Diamond | Level 26

## Re: Create a new variable based on values

Something like this in a data step

``````data want;
set have;
length totalmed \$ 15;
/* If Statin and BB and ACEI are in at least one of the 7 columns, then Total Med = Triple */
if whichc('Statin',of drug:)>0 and whichc('BB',of drug:)>0 and whichc('ACEI',of drug:)>0 then totalmed='Triple';
/* If ONLY Statin and BB are in at least one of 7 columns, then Total Med = Double_SB */
if whichc('Statin',of drug:)>0 and whichc('BB',of drug:)>0 and whichc('ACEI',of drug:)=0 then totalmed='Double_SB';
/* You type the rest */
run;
``````
--
Paige Miller
Fluorite | Level 6

## Re: Create a new variable based on values

Thank you @PaigeMiller

## Re: Create a new variable based on values

Hello @Sam1010 and welcome to the SAS Support Communities!

So, basically, each of the possible combinations of the three drug classes corresponds to one value of the new variable. Then I would also define the cases "none of the three" and "only ACEI and BB" (the latter occurs for patient no. 5; how about denoting this combination as "Double_AB"?) and use the CHOOSEC function:

``````data have;
input Patient_ID (Drug1-Drug7) (\$);
cards;
1 Statin Statin BB . ACEI . Statin
2 . BB . BB BB . .
3 Statin . BB . . BB .
4 ACEI Statin . ACEI . . ACEI
5 . ACEI BB . . . .
6 Statin . . Statin . Statin .
7 BB . . . . . .
;

data want;
set have;
array d[*] drug:;
length Total_Med \$12;
Total_Med=choosec(1 + ('ACEI' in d) + 2*('BB' in d) + 4*('Statin' in d),
' ','ACEI alone','BB alone','Double_AB???','Statin alone','Double_SA','Double_SB','Triple');
run;``````

The first argument of the CHOOSEC function is a number in {1, 2, ..., 8} and uniquely determines the combination (remember the binary number system). The subsequent strings describe these combinations.

From SAS Users blog