BookmarkSubscribeRSS Feed
cjacobson45
Calcite | Level 5

Hi I need help programming something:

 

Data

Member ID              DOS                            Category  

111                          1/2/2015                      PT/OT

111                          1/3/2015                      PT/OT

111                          3/5/2016                      Chiro

111                          2/6/2017                      PT/OT

222                          5/6/2015                      Chiro

222                          7/8/2016                      Chiro

222                          8/10/2016                    PT/OT

222                          9/10/2016                    Chiro

222                          9/10/2017                    PT/OT

222                         10/11/2017                   Chiro

 

Basically what I need is to pick up the initial record then the first record 365 days after the initial record.  In this case it would return the following:

 

Member ID              DOS                            Category

111                          1/2/2015                      PT/OT

111                          3/5/2016                      Chiro

222                          5/6/2015                      Chiro

222                          7/8/2016                      Chiro

222                          9/10/2017                    PT/OT

 

So you have a starter record then each new even starts 365 days after that initial record.  Please help!

                     

6 REPLIES 6
PaigeMiller
Diamond | Level 26

@cjacobson45 wrote:

Hi I need help programming something:


Basically what I need is to pick up the initial record then the first record 365 days after the initial record.  In this case it would return the following:

 

So you have a starter record then each new even starts 365 days after that initial record.  Please help!

                     


And then subsequent records for each Member ID have to have a gap of at least one year, is that correct?

 

Assuming you have sorted your data by member_id and dos, and that the dates are actual SAS datetime values (perhaps formatted appropriately), then try this:

 

data want;
    set have;
    by member_id;
    if first.member_id then output;
    prev_dos=lag(dos);
    if intck('year',prev_dos,dos,'c')>=1 then output;
    drop prev_dos;
run;
    
--
Paige Miller
cjacobson45
Calcite | Level 5

Thanks for responding.  I'm not sure if I'm missing something, but this doesn't work.  Only one record is coming back.  I need every record in red to be returned.

PaigeMiller
Diamond | Level 26

@cjacobson45 wrote:

Thanks for responding.  I'm not sure if I'm missing something, but this doesn't work.  Only one record is coming back.  I need every record in red to be returned.


Saying "doesn't work" doesn't help us help you. It provides no information at all.

 

Provide details. Show us exactly what you did, show us your data, and show us exactly what happened. If there is an error in the SAS log, then show us the SAS log.

--
Paige Miller
PGStats
Opal | Level 21

This should work, assuming your data is sorted

 

data have;
input ID DOS :mmddyy. Category $;  
datalines;
111                          1/2/2015                      PT/OT
111                          1/3/2015                      PT/OT
111                          3/5/2016                      Chiro
111                          2/6/2017                      PT/OT
222                          5/6/2015                      Chiro
222                          7/8/2016                      Chiro
222                          8/10/2016                    PT/OT
222                          9/10/2016                    Chiro
222                          9/10/2017                    PT/OT
222                         10/11/2017                   Chiro
;

data want;
do until(last.id);
    set have; by id dos;
    if intnx("year", dos, -1, "same") >= prevDos then do;
        output;
        prevDos = dos;
        end;
    end;
drop prevDos;
run;
PG
cjacobson45
Calcite | Level 5

Thanks I believe that worked!! 🙂

Ksharp
Super User

The same as PG's .

 

data have;
input ID DOS :mmddyy. Category $;  
format dos mmddyy10.;
datalines;
111                          1/2/2015                      PT/OT
111                          1/3/2015                      PT/OT
111                          3/5/2016                      Chiro
111                          2/6/2017                      PT/OT
222                          5/6/2015                      Chiro
222                          7/8/2016                      Chiro
222                          8/10/2016                    PT/OT
222                          9/10/2016                    Chiro
222                          9/10/2017                    PT/OT
222                         10/11/2017                   Chiro
;

data want;
 set have;
 by id ;
 retain temp;
 if first.id then do;temp=dos;output;end;
  else if intnx('year',temp,1,'s')=<dos then do;temp=dos;output;end;
drop temp;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 751 views
  • 0 likes
  • 4 in conversation