BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
mahi263
Calcite | Level 5

Hello everyone, 

I got stuck with the following query, can you please check and do the needful. Thanks in advance!

 

This is what I have

SubjectCategoryStartEnd
101Period17-Aug-2424-Aug-24
101Period212-Aug-2414-Aug-24
101Period325-Aug-2427-Nov-24
101Period227-Aug-2427-Aug-24
101Period411-Sep-2427-Nov-24
101Period220-Sep-2420-Sep-24
101Period213-Oct-2416-Oct-24
101Period220-Oct-2422-Oct-24
101Period27-Nov-248-Nov-24

 

I want the result in the below fashion. Please do the needful

SubjectCategoryStartEnd 
101Period17-Aug-2411-Aug-24 
101Period212-Aug-2414-Aug-24 
101Period115-Aug-2424-Aug-24Since this continued from 7Aug till 24Aug in between Period 2 occurred
101Period325-Aug-2426-Aug-24 
101PEriod227-Aug-2427-Aug-24 
101Period411-Sep-2419-Sep-24 
101Period220-Sep-2420-Sep-24 
101Period421-Sep-2412-Oct-24 
101Period213-Oct-2416-Oct-24 
101Period417-Oct-2419-Oct-24 
101Period220-Oct-2422-Oct-24 
101Period423-Oct-246-Nov-24 
101Period27-Nov-248-Nov-24 
101Period49-Nov-2427-Nov-24Period4 started 11-sep and ended 27Nov

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

There are too many scenarioes you need to take into account of.

Anyway, here could give you a start.

data have;
infile cards expandtabs truncover;
input Subject	Category $	Start :date11.	End :date11.;
format Start 	End :date11.;
cards;
101	Period1	7-Aug-24	24-Aug-24
101	Period2	12-Aug-24	14-Aug-24
101	Period3	25-Aug-24	27-Nov-24
101	Period2	27-Aug-24	27-Aug-24
101	Period4	11-Sep-24	27-Nov-24
101	Period2	20-Sep-24	20-Sep-24
101	Period2	13-Oct-24	16-Oct-24
101	Period2	20-Oct-24	22-Oct-24
101	Period2	7-Nov-24	8-Nov-24
;
data temp;
set have;
do date=start to end;
output;
end;
drop start end;
format date date11.;
run;
proc sort data=temp;
by Subject date;
run;
data temp2;
set temp;
by Subject date;
if last.date;
run;
data temp3;
set temp2;
by Subject category notsorted;
if dif(date) ne 1 or first.category then group+1;
run;
proc sql;
create table want as
select group,Subject,category,min(date) as start format=date11.,max(date) as end format=date11.
from temp3
group by group,Subject,category;
quit;


View solution in original post

2 REPLIES 2
Ksharp
Super User

There are too many scenarioes you need to take into account of.

Anyway, here could give you a start.

data have;
infile cards expandtabs truncover;
input Subject	Category $	Start :date11.	End :date11.;
format Start 	End :date11.;
cards;
101	Period1	7-Aug-24	24-Aug-24
101	Period2	12-Aug-24	14-Aug-24
101	Period3	25-Aug-24	27-Nov-24
101	Period2	27-Aug-24	27-Aug-24
101	Period4	11-Sep-24	27-Nov-24
101	Period2	20-Sep-24	20-Sep-24
101	Period2	13-Oct-24	16-Oct-24
101	Period2	20-Oct-24	22-Oct-24
101	Period2	7-Nov-24	8-Nov-24
;
data temp;
set have;
do date=start to end;
output;
end;
drop start end;
format date date11.;
run;
proc sort data=temp;
by Subject date;
run;
data temp2;
set temp;
by Subject date;
if last.date;
run;
data temp3;
set temp2;
by Subject category notsorted;
if dif(date) ne 1 or first.category then group+1;
run;
proc sql;
create table want as
select group,Subject,category,min(date) as start format=date11.,max(date) as end format=date11.
from temp3
group by group,Subject,category;
quit;


mahi263
Calcite | Level 5

Thanks a lot @Ksharp amazing. it worked, I need to add a few more components to get the data as desired. Have a Good one,. Thanks again.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 652 views
  • 1 like
  • 2 in conversation