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

Hi everyone,

 

I'm a bit stumped as to how to approach this data manipulation problem, and I'm hoping the community can help. This is my first time posting, so my apologies if I'm doing this incorrectly. I currently have a dataset that looks like this:

 

groupdate
A2/12/2018
A7/18/2018
A8/23/2018
A10/4/2018
B5/10/2018
B9/4/2018
B12/18/2018
C3/19/2018
C6/2/2018
D3/12/2019
D4/25/2019
D5/23/2019
D6/19/2019

 

My goal is to take each date within a group and the subsequent date that follows to create two new "start" and "end" variables that indicate a date range. Here is my desired output:

 

groupstartend
A2/12/20187/18/2018
A7/18/20188/23/2018
A8/23/201810/4/2018
B5/10/20189/4/2018
B9/4/201812/18/2018
C3/19/20186/2/2018
D3/12/20194/25/2019
D4/25/20195/23/2019
D5/23/20196/19/2019

 

Unfortunately, I do not have SAS code that I've tried, as I do not even know how to start to go about doing this. 😞 Any help is much appreciated. 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data want;
set have;
by group; /* this lets you use first. and last. */
format start end mmddyy10.;
start = lag(date);
if not first.group
then do;
  end = date;
  output; /* first observation of a group will not be output */
end;
drop date;
run;

View solution in original post

4 REPLIES 4
Reeza
Super User

It usually helps if you post data as text rather than images. If we want to work with your data we'd have to type it out. 

 

There's a somewhat neat trick you can use to get this (untested) because I'm too lazy to type out your data. 

Merge BY group but set the first observation to be 2, so it gets the second row merged with the first. 

Let me know how this works:

 

data want;
merge have (rename=date=start_date) 
           have(firstobs=2 rename=date=end_date);
by group;

if last.group then delete;

run;

@sasburger wrote:

Hi everyone,

 

I'm a bit stumped as to how to approach this data manipulation problem, and I'm hoping the community can help. This is my first time posting, so my apologies if I'm doing this incorrectly. I currently have a dataset that looks like this:

 

transmissiondiagramsampledata.PNG

 

My goal is to take each date within a group and the subsequent date that follows to create two new "start" and "end" variables that indicate a date range. Here is my desired output:

 

transmissiondiagramsampledata2.PNG

 

Unfortunately, I do not have SAS code that I've tried, as I do not even know how to start to go about doing this. 😞 Any help is much appreciated. 


 

sasburger
Fluorite | Level 6
Thanks Reeza! I was floored by how clever this was. I will definitely post text instead of images next time.
Kurt_Bremser
Super User
data want;
set have;
by group; /* this lets you use first. and last. */
format start end mmddyy10.;
start = lag(date);
if not first.group
then do;
  end = date;
  output; /* first observation of a group will not be output */
end;
drop date;
run;
sasburger
Fluorite | Level 6
Worked like a charm. 🙂 Thank you so so much!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 625 views
  • 0 likes
  • 3 in conversation