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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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