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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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