BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sasbee
Fluorite | Level 6

I would like to create a method to check if a patient switch a medication (med_switch) or start a new medication that he/she had never had before (new_med) by comparing with the list of previous medication (any medication that the patient used before this current medication, list_of_previous_med).

 

subjidyear prescriptionmed_switchlist_of_previous_mednew_med
a0012001med1- -yes
a0012002med1nomed1no
a0012003med2yesmed1yes
a0012003med2nomed1,med2no
a0022001med1--yes
a0022002med2yesmed1yes
a0022003med1yesmed1, med2no
a0022004med3yesmed1, med2yes

 

I want to create the new variables in italic, however, I have no idea on how to do that on SAS.

 

I have been playing around with lag() and retain() function to compare rows of variables, however, I kept getting errors when I try to do it by subjid. 

 

I hope the community can help.  

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set have;
    by subjid;
    length med_switch $ 3 commalist $ 256;
    retain commalist;
    prev_med=lag(prescription);
    if first.subjid then prev_med=' '; 
    if not first.subjid and prescription^=prev_med then  med_switch='yes';
    else med_switch='no';
    if first.subjid then commalist=' ';
    else commalist=cats(prev_med,',',commalist);
    if find(commalist,prescription,'it')>0 then new_med=0;
    else new_med=1;
    drop commalist prev_med;
run;
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
data want;
    set have;
    by subjid;
    length med_switch $ 3 commalist $ 256;
    retain commalist;
    prev_med=lag(prescription);
    if first.subjid then prev_med=' '; 
    if not first.subjid and prescription^=prev_med then  med_switch='yes';
    else med_switch='no';
    if first.subjid then commalist=' ';
    else commalist=cats(prev_med,',',commalist);
    if find(commalist,prescription,'it')>0 then new_med=0;
    else new_med=1;
    drop commalist prev_med;
run;
--
Paige Miller
sasbee
Fluorite | Level 6

@PaigeMiller  THANK YOU SO MUCH!!!!! This does exactly what I was hoping for. You put together the code so beautifully and succulently. I learned so much from the code. 

PaigeMiller
Diamond | Level 26

Thank you! Sometimes people use the word "succulently" to describe the results of my cooking; I have never had anyone describe my SAS code as being written "succulently", but I accept the compliment!

--
Paige Miller

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 3 replies
  • 870 views
  • 0 likes
  • 2 in conversation