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

I've got dates describing periodes (start end) and a variable describing waiting time (wt). However, the dates are partialle overlapping. This is nearly fixed in the attached program but I would like to have latest observation of wt to be placed in correctly. Anyone?

1 ACCEPTED SOLUTION

Accepted Solutions
Daniel-Santos
Obsidian | Level 7

Hi.

 

If I got this right, you just need to sort the data correctly and fetch the previous stdate between by groups.

 

* sort reversing stdate (recent to later);
proc sort data=waits;
     by hospid diag descending stdate;

run;

* adjust overlapping between by groups;
data waits2;
     set waits;
     format stdate enddate date9.;
     drop _:;
     by hospid diag;

     * fetch previous stdate;
     _enddate=lag1(stdate);
     * skip first by group row, fix overlapping;
     if not first.diag then enddate=min(enddate,_enddate-1);

run;

* sort back (later to recent);
proc sort;
     by hospid diag stdate;
run

 

lagN functions will retrieve the previous Nth row value of PDV.

 

More here: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212547.htm

 

The rest is just pure data step logic.

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

View solution in original post

3 REPLIES 3
Shmuel
Garnet | Level 18

You have similar situation as in next post.See solution - it fits your case:

 

https://communities.sas.com/t5/General-SAS-Programming/Sorting-overlapping-dates-and-address-histori...

Daniel-Santos
Obsidian | Level 7

Hi.

 

If I got this right, you just need to sort the data correctly and fetch the previous stdate between by groups.

 

* sort reversing stdate (recent to later);
proc sort data=waits;
     by hospid diag descending stdate;

run;

* adjust overlapping between by groups;
data waits2;
     set waits;
     format stdate enddate date9.;
     drop _:;
     by hospid diag;

     * fetch previous stdate;
     _enddate=lag1(stdate);
     * skip first by group row, fix overlapping;
     if not first.diag then enddate=min(enddate,_enddate-1);

run;

* sort back (later to recent);
proc sort;
     by hospid diag stdate;
run

 

lagN functions will retrieve the previous Nth row value of PDV.

 

More here: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212547.htm

 

The rest is just pure data step logic.

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

terjeph
Obsidian | Level 7

Thanks! worked well.

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
  • 1296 views
  • 3 likes
  • 3 in conversation