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

Hi everyone

I have a sample of patient with their medicine they bought. Some patients bought the same medicine several times. I would like to create a variable, where you can see the number of different medications they take (not the number of medicines they bought).

Patient_id

medicine

No_medicine (new variable, how it should be)

1

 a

2

1

 a

2

1

 b

2

2

 b

2

2

 c

2

3

 a

2

3

 a

2

3

 d

2

4

 c

1

5

 d

2

5

 e

2

5

 d

2

6

 b

1

7

 e

1

7

 e

1

8

 c

4

8

 d

4

8

 e

4

8

 f

4

9

 a

2

9

 e

2

10

 e

1

 

I already tried: 

data practice2; set practice1;
No_medicine+1;
by patient_id medicine;
if first.patient_id or first.medicine=1;
run;

But the result was nonsense. 

How can I create this variable? 

 

Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
proc sql;
     create table practice2 as select patient_id,
        medicine,
        count(distinct medicine) as no_medicine
        from practice1
     group by patient_id;
quit;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
proc sql;
     create table practice2 as select patient_id,
        medicine,
        count(distinct medicine) as no_medicine
        from practice1
     group by patient_id;
quit;
--
Paige Miller
Nina4
Obsidian | Level 7
Dear Paige

Thank you for your solution. It works! 🙂

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 480 views
  • 1 like
  • 2 in conversation