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!
proc sql;
create table practice2 as select patient_id,
medicine,
count(distinct medicine) as no_medicine
from practice1
group by patient_id;
quit;
proc sql;
create table practice2 as select patient_id,
medicine,
count(distinct medicine) as no_medicine
from practice1
group by patient_id;
quit;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.