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

Can someone please help with the following problem? 

What I have in the data:

IDDrugStart DTEnd DT
1Drug A3-Aug-200628-Dec-2006
1Drug B3-Aug-200618-Nov-2007
1Drug C29-Dec-200615-Dec-2008

 

What I am trying to obtain:

IDDrugStart DT NewEnd DT New
1Drug A + Drug B3-Aug-200628-Dec-2006
1Drug B + Drug C29-Dec-200618-Nov-2007
1Drug C19-Nov-200715-Dec-2008

 

Thanks a lot!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile cards expandtabs;
input ID	Drug $	(Start 	End) (: date11.);
format start end date11.;
cards;
1	DrugA	3-Aug-2006	28-Dec-2006
1	DrugB	3-Aug-2006	18-Nov-2007
1	DrugC	29-Dec-2006	15-Dec-2008
2	DrugA	3-Aug-2006	28-Dec-2006
2	DrugB	3-Aug-2006	18-Nov-2007
2	DrugC	29-Dec-2006	15-Dec-2008
;

data temp;
 set have;
 do date=start to end;
  output;
 end;
 format date date11.;
 drop start end;
run;
proc sort data=temp nodupkey;
by id date drug;
run;
data temp1;
do until(last.date);
 set temp;
 by id date;
 length drugs $ 200;
 drugs=catx('+',drugs,drug);
end;
drop drug;
run;
data temp2;
 set temp1;
 by id;
 if first.id or dif(date) ne 1 or drugs ne lag(drugs) then group+1;
run;
proc sql;
create table want as
select max(id) as id,max(drugs) as drug,min(date) as start format=date11.,max(date) as end format=date11.
 from temp2
  group by group;
quit;

View solution in original post

4 REPLIES 4
goyalrk
Calcite | Level 5

Thank you for your response. I have tried solutions to previously posted similar problems, and for one reason or the other, they do not seem to apply for the specific data example/scenario that I have (to get the drug combinations and dates as expected). I'd very much appreciate any further assistance with this particular query. 

andreas_lds
Jade | Level 19

Please post the code you have tried, along with the log so that we can help to fix the problems.

Ksharp
Super User
data have;
infile cards expandtabs;
input ID	Drug $	(Start 	End) (: date11.);
format start end date11.;
cards;
1	DrugA	3-Aug-2006	28-Dec-2006
1	DrugB	3-Aug-2006	18-Nov-2007
1	DrugC	29-Dec-2006	15-Dec-2008
2	DrugA	3-Aug-2006	28-Dec-2006
2	DrugB	3-Aug-2006	18-Nov-2007
2	DrugC	29-Dec-2006	15-Dec-2008
;

data temp;
 set have;
 do date=start to end;
  output;
 end;
 format date date11.;
 drop start end;
run;
proc sort data=temp nodupkey;
by id date drug;
run;
data temp1;
do until(last.date);
 set temp;
 by id date;
 length drugs $ 200;
 drugs=catx('+',drugs,drug);
end;
drop drug;
run;
data temp2;
 set temp1;
 by id;
 if first.id or dif(date) ne 1 or drugs ne lag(drugs) then group+1;
run;
proc sql;
create table want as
select max(id) as id,max(drugs) as drug,min(date) as start format=date11.,max(date) as end format=date11.
 from temp2
  group by group;
quit;

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
  • 4 replies
  • 1651 views
  • 1 like
  • 4 in conversation