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

Hello everyone,

Has anyone done something like this before?

I have the following example dataset:

iced_tea_0-1614255367694.png

I want to use this dataset to create a new one, where Obs 6 is dropped, due to occurring within the range of dates in Obs 5. In other words, since the range of dates Nov2 - Nov4, 2019 is subsumed into a longer time range above, Nov1-Nov10, 2019 - I don't need Obs 6 at all and want to somehow get rid of it.

Below is the code that creates this example and also creates the dataset that I would like to create. The point is somehow flagging Obs 6, so that I can then drop it, so the eventual dataset doesn't have to be 100% identical to what wrote in the second part of the code. As long as I can somehow flag that record.

/*Example of initial dataset.*/
data example;
   input employee_id $ startdate :DATE8. enddate :DATE8.;
   format startdate enddate DATE8.;
   datalines;
12345	 21JUN20	 24JUN20
12345	 28JUN20	 18JUL20
12345	 29JUL20	 14AUG20
12345	 09NOV20	 20NOV20
67890	 01NOV19	 10NOV19
67890	 02NOV19	 04NOV19
;
run;
proc print data=example;
run;
/*Example of eventual dataset.*/
data want;
   input employee_id $ startdate :DATE8. enddate :DATE8. final_startdate :DATE8. final_enddate :DATE8. drop $;
   format startdate enddate final_startdate final_enddate DATE8.;
   datalines;
12345	 21JUN20	 24JUN20	 21JUN20	 24JUN20	no
12345	 28JUN20	 18JUL20	 28JUN20	 18JUL20	no
12345	 29JUL20	 14AUG20	 29JUL20	 14AUG20	no
12345	 09NOV20	 20NOV20	 09NOV20	 20NOV20	no
67890	 01NOV19	 10NOV19	 01NOV19	 10NOV19	no
67890	 02NOV19	 04NOV19	 .	         .	        yes
;
run;
proc print data=want;
run;

 Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data example;
   input employee_id $ startdate :DATE8. enddate :DATE8.;
   format startdate enddate DATE8.;
   datalines;
12345	 21JUN20	 24JUN20
12345	 28JUN20	 18JUL20
12345	 29JUL20	 14AUG20
12345	 09NOV20	 20NOV20
67890	 01NOV19	 10NOV19
67890	 02NOV19	 04NOV19
;
run;
data temp;
 set example;
 do date=startdate to enddate ;
  output;
 end;
keep employee_id date;
format date date9.;
run;
proc sort data=temp nodupkey;
by employee_id date;
run;
data temp;
 set temp;
 by employee_id;
 if first.employee_id or dif(date) ne 1 then group+1;
run;
proc summary data=temp;
by employee_id group;
var date;
output out=want min=startdate max=enddate;
run;

View solution in original post

2 REPLIES 2
Ksharp
Super User
data example;
   input employee_id $ startdate :DATE8. enddate :DATE8.;
   format startdate enddate DATE8.;
   datalines;
12345	 21JUN20	 24JUN20
12345	 28JUN20	 18JUL20
12345	 29JUL20	 14AUG20
12345	 09NOV20	 20NOV20
67890	 01NOV19	 10NOV19
67890	 02NOV19	 04NOV19
;
run;
data temp;
 set example;
 do date=startdate to enddate ;
  output;
 end;
keep employee_id date;
format date date9.;
run;
proc sort data=temp nodupkey;
by employee_id date;
run;
data temp;
 set temp;
 by employee_id;
 if first.employee_id or dif(date) ne 1 then group+1;
run;
proc summary data=temp;
by employee_id group;
var date;
output out=want min=startdate max=enddate;
run;
iced_tea
Obsidian | Level 7
Wow brilliant! Thank you so much Ksharp! Totally works and I even understand the method you used. Thank you.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 661 views
  • 2 likes
  • 2 in conversation