BookmarkSubscribeRSS Feed
AmitChop7391
Obsidian | Level 7

Can I check the same simultaneously for multiple DRUG flags. For example, if I want to check the same for flags DRUG_1, DRUG_2, DRUG_3. If they are having same values in first and last observation for each patient or not.

Reeza
Super User
You're kinda changing the question on here. It really helps if you provide this information at the outset. I may have missed it, but I don't see your expected output anywhere.
ballardw
Super User

@AmitChop7391 wrote:

Can I check the same simultaneously for multiple DRUG flags. For example, if I want to check the same for flags DRUG_1, DRUG_2, DRUG_3. If they are having same values in first and last observation for each patient or not.


To do multiple at one time might look like this:

data have;
   input PT_ID DRUG_NAME $ DRUG_1 DRUG_2 Date :mmddyy10.; 
   format date mmddyy10.;
   drug_1 = (drug_1=1);
   drug_2 = (drug_2=1);
datalines;
1 A 1 . 1/1/2017 
1 A 1 . 5/2/2017 
1 B . 1 8/2/2017 
1 B . 1 9/2/2017 
1 A 1 . 12/2/2017 
1 A 1 . 12/31/2017 
2 A 1 . 5/3/2017 
2 B . 1 6/3/2017 
2 A 1 . 9/3/2017 
3 A 1 . 5/6/2017 
3 B . 1 7/9/2017 
3 B . 1 9/9/2017 
4 A 1 1 5/6/2017 
4 B . . 7/9/2017 
4 B . 1 9/9/2017 
;

data temp;
   set have;
   by  PT_ID;
   if first.pt_id or last.Pt_id;
run;
proc summary data=temp nway;
   class pt_id;
   var drug_1 drug_2;
   output out=work.drugs (drop=_type_ _freq_ )
       range =  max=/autoname;
run;

Note I added a PT_id = 4 to demonstrate a drug_2 "match".

This has two output variables for each "drug". I suspect your want a first and last match when they are both 1 in the original data. The approach I used previously would treat "missing" as same. So if you do not want to consider "missing" as possibly "same" we need a way to determine that. The two variables combine, the _range and _max would be used together to determine a match as below:

data work.matched;
   set work.drugs;
   drug_1match = (drug_1_range=0 and drug_1_max=1);
   drug_2match = (drug_2_range=0 and drug_2_max=1);
run;

If the match variable is 1 then we have the condition you are looking for. (I think).

If you have a lot of drug variables the last bit of code could use arrays to do the same calculation for each of them.

 

Look at this last set's match variables and see if the 1 values correspond to your rule given the example data I used.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 17 replies
  • 5358 views
  • 4 likes
  • 5 in conversation