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
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;
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;
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.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.
Find more tutorials on the SAS Users YouTube channel.