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

Hi All,

 

I have the below scenario,

 

scenario.JPG

 

Input:

ID utp start date end date
123 0 1/1/2021 31/1/2021
123 0 1/2/2021 10/3/2021
123 0 11/3/2021 30/4/2021
123 1 1/5/2021 24/5/2021
123 1 25/5/2021 4/6/2021
123 1 5/6/2021 20/6/2021
123 0 21/6/2021 1/7/2021
123 0 2/7/2021 9/7/2021
123 0 10/7/2021 21/7/2021
123 0 22/7/2021

14/8/2021

 

Output:

ID utp start date end date
123 0 1/1/2021 30/4/2021
123 1 1/5/2021 20/6/2021
123 0 21/6/2021 14/8/2021

 

 

Can anyone please suggest an approach to retreive the above output based on the prescribed input?

 

Any suggestions are highly appreciable!!

 

Thanks in advance!!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Use the NOTSORTED option on the BY statement.

 

 

data want;
set have;
retain start;

by id utp NOTSORTED;
if first.utp then Start = start_date;
if last.utp then do;
end = end_date;
output;
end;

format start end date9.;
drop start_date end_date;
run;

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Please explain the logic that allows you to derive the output data from the input data.

--
Paige Miller
Vigneswar
Obsidian | Level 7

Hi, thanks for your response!!

In this example, there are 3 combinations that need to be extracted. Within a ID group, there are 2 possible utp values and different start and end dates. Row 1 to 3, 4 to 6, 7 to 10 there are 3 combinations. So mainly I want to extract based on ID group, utp and dates. In my output (first row) ID 123 and utp 0 are combined with start date from 1st row and end date from 3rd row. Likewise, 2nd row in the output is combined with start date from 4th row and end date in 6th row. 3rd row in the output is combined with start date from 7th row and end date in 10th row.

To summarize, we need extract based on (ID and utp) group and extract their start and end date from different rows.

Please let me know if you need more clarity.
PaigeMiller
Diamond | Level 26

So you want the minimum of the start_date and the maximum of the end_date for each grouping of the variables ID and UTP?

 

Or do you want always the first row start_date and the last row end_date for each grouping of the variables ID and UTP?

--
Paige Miller
Reeza
Super User

Use the NOTSORTED option on the BY statement.

 

 

data want;
set have;
retain start;

by id utp NOTSORTED;
if first.utp then Start = start_date;
if last.utp then do;
end = end_date;
output;
end;

format start end date9.;
drop start_date end_date;
run;
Vigneswar
Obsidian | Level 7
Thank you so much!!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 871 views
  • 2 likes
  • 3 in conversation