BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
buszhangsy
Calcite | Level 5

Hi PG, I examined the data on 20 patients and the code does its job!

Thanks a lot!

Ksharp
Super User

If I understood what you mean.

data have;
input Patient_ID   $         Medication_name  $     Service_date : mmddyy10.;
format      Service_date  mmddyy10.;
cards;
0000120              QVar                           08/07/2007
0000120              SPIRIVA                     08/14/2007
0000120              SPIRIVA                     11/14/2007
0000120              SPIRIVA                     01/16/2008
0000120              QVar                           12/05/2008
0000120              SPIRIVA2                     08/13/2007
0000120              SPIRIVA2                     11/13/2007
0000120              SPIRIVA2                     01/15/2008
0000120              SPIRIVA3                     08/15/2007
0000120              SPIRIVA3                     11/15/2007
0000120              SPIRIVA3                     01/17/2008
0000120              SPIRIVA4                     01/11/2008
;
run;
data temp;
 if _n_ eq 1 then do;
  if 0 then set have;
  length rank 8;
  declare hash ha();
   ha.definekey('Medication_name');
   ha.definedata('rank');
   ha.definedone();
 end;
set have;
by      Patient_ID;
if ha.find() ne 0 then do;r+1;rank=r;ha.add();end;
if last.Patient_ID then do;ha.clear(); r=0;end;
drop r;
run;


proc sort data=temp ; by Patient_ID rank Service_date;run;

data x;
 set temp;
 by Patient_ID rank;
 if first.rank then n=0;
 n+1;
 lag_Service_date= lag(Service_date) ;
 month=intck('month',lag_Service_date,Service_date,'c');
 if n=2 and month le 6;
 keep  Patient_ID Medication_name lag_Service_date;
 format lag_Service_date mmddyy10.;
run;

data want;
 set x;
 by Patient_ID;
 if first.Patient_ID;
run;

Xia Keshan

Message was edited by: xia keshan

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
  • 16 replies
  • 1448 views
  • 6 likes
  • 5 in conversation