BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nina4
Obsidian | Level 7

Hey everybody 

I'm working with claims data, where each patient has multiple rows. I would now like to create a new variable with a yes or no (1/0) per patient. 

This is my exercise data:

data exercise;
input patient medication$ expensive no_rows;
cards;
1 a 1 1
1 b 0 1
1 a 1 1
2 c 0 1
2 d 0 1
2 a 1 1
3 b 1 1
3 a 1 1
3 c 0 1
4 a 1 1
5 b 1 1
6 d 0 1
6 d 0 1
;
run;

Now you can see, that I coded each medicine as expensive yes or no (1/0). I would like to know how I can add a variable that tells me if the patient has an expensive medicine. 

As a table: 

PatientMedicationexpensiveno_rowexp_pat
1a111
1b011
1a111
2c011
2d011
2a111
3b111
3a111
3c011
4a111
5b111
6d010
6d010

 

Thank you for your answers and kind regards, 

Nina

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

To test if any boolean value is true use the MAX() aggregate function.

proc sql;
 create table want as 
   select *, max(expensive) as exp_pat
   from exercise
   group by patient
;

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

@Nina4 wrote:


Hey everybody 

I'm working with claims data, where each patient has multiple rows. I would now like to create a new variable with a yes or no (1/0) per patient. 

This is my exercise data:

data exercise;
input patient medication$ expensive no_rows;
cards;
1 a 1 1
1 b 0 1
1 a 1 1
2 c 0 1
2 d 0 1
2 a 1 1
3 b 1 1
3 a 1 1
3 c 0 1
4 a 1 1
5 b 1 1
6 d 0 1
6 d 0 1
;
run;

Now you can see, that I coded each medicine as expensive yes or no (1/0). I would like to know how I can add a variable that tells me if the patient has an expensive medicine. 

 


 

Please explain further. It seems to me you already have a variable that tells you if the patient has an expensive medicine.

--
Paige Miller
Nina4
Obsidian | Level 7

Hi Paige 

I have a variable that tells me if the medicine is expensive. But each patient has multiple medicines (in multiple rows) and I would like to have a variable that tells me if a patient has (at least) one expensive medicine. 

Is this clearer?

PaigeMiller
Diamond | Level 26

Thanks, yes that helps.

proc summary nway data=exercise;
    class patient;
    var expensive;
    output out=_max_(drop=_:) max=exp_pat;
Run;
data want;
    merge exercise _max_;
    by patient;
Run;

 

--
Paige Miller
Tom
Super User Tom
Super User

To test if any boolean value is true use the MAX() aggregate function.

proc sql;
 create table want as 
   select *, max(expensive) as exp_pat
   from exercise
   group by patient
;

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
  • 4 replies
  • 870 views
  • 2 likes
  • 3 in conversation