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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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