BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input id gender $ enrolldate : mmddyy. aaa : mmddyy. bbb : mmddyy. inflam_1 : mmddyy. inflam_2 : mmddyy. inflam_3 : mmddyy. @@;
format enrolldate aaa bbb inflam_1 inflam_2 inflam_3 mmddyy10.;
datalines;
1 M 08/01/2019 09/04/2019 09/16/2019 10/02/2019 11/03/2019 12/04/2019
2 M 08/02/2019 08/14/2019 . 06/25/2019 09/14/2019 .
3 F 08/03/2019 . . 10/13/2019 11/13/2019 .
4 F 08/04/2019 07/04/2019 09/16/2019 . . .
5 F 08/05/2019 05/04/2019 07/04/2019 09/18/2019 10/18/2019 .
;
run;

data temp;
 set have;
 by id;
 if first.id then group=0;

 length flag $ 40;
 min=min(enrolldate,aaa,bbb,inflam_1,inflam_2,inflam_3);
 do date=min to '31dec2019'd;
   if date=enrolldate then do;flag='enrolldate';group+1;end;
   if date=aaa then do;flag='aaa';group+1;end;
   if date=bbb then do;flag='bbb';group+1;end;
   if date=inflam_1 then do;flag='inflam_1';group+1;end;
   if date=inflam_2 then do;flag='inflam_2';group+1;end;
   if date=inflam_3 then do;flag='inflam_3';group+1;end;
   output;
 end;
 format date mmddyy10.;
 keep  id gender  flag date group;
run;
proc summary data=temp;
by id gender group flag;
var date ;
output out=temp2(drop=_:) min=start max=stop;
run;
data temp3;
 set temp2;
 by id;
 retain trt_status ;
 if first.id then trt_status=0;
 if flag='aaa' then trt_status=1;
 if flag='bbb' then trt_status=2;
run;
data temp4;
 merge temp3 temp3(keep=id flag rename=(id=_id flag=_flag) firstobs=2);
 inflam_event=0;
 if id=_id and _flag=:'inflam_' then inflam_event=1;

 retain inflam_count _count1 _count2;
 if id ne lag(id) then do;inflam_count=0;_count1=0;_count2=0;end;
 if flag=:'inflam_' then _count1=input(scan(flag,-1,'_'),best.);
 if id=_id and _flag=:'inflam_' then _count2=input(scan(_flag,-1,'_'),best.);
 inflam_count=max(_count1,_count2);
 drop _:;
run;
data temp5;
 set temp4;
 by id;
 retain prior_inflam ;
 if first.id then prior_inflam=0;
 if id=lag(id) and lag(inflam_count)=1 then prior_inflam=1;
run;
data temp6;
 set temp5;
 by id;
 retain found;
 if first.id then found=0;
 if flag='enrolldate' then found=1;
 if found;
run;
data want;
 set temp6(drop=group flag found);
 if stop ne '31dec2019'd then stop=stop+1;
run;

View solution in original post

1 REPLY 1
Ksharp
Super User
data have;
input id gender $ enrolldate : mmddyy. aaa : mmddyy. bbb : mmddyy. inflam_1 : mmddyy. inflam_2 : mmddyy. inflam_3 : mmddyy. @@;
format enrolldate aaa bbb inflam_1 inflam_2 inflam_3 mmddyy10.;
datalines;
1 M 08/01/2019 09/04/2019 09/16/2019 10/02/2019 11/03/2019 12/04/2019
2 M 08/02/2019 08/14/2019 . 06/25/2019 09/14/2019 .
3 F 08/03/2019 . . 10/13/2019 11/13/2019 .
4 F 08/04/2019 07/04/2019 09/16/2019 . . .
5 F 08/05/2019 05/04/2019 07/04/2019 09/18/2019 10/18/2019 .
;
run;

data temp;
 set have;
 by id;
 if first.id then group=0;

 length flag $ 40;
 min=min(enrolldate,aaa,bbb,inflam_1,inflam_2,inflam_3);
 do date=min to '31dec2019'd;
   if date=enrolldate then do;flag='enrolldate';group+1;end;
   if date=aaa then do;flag='aaa';group+1;end;
   if date=bbb then do;flag='bbb';group+1;end;
   if date=inflam_1 then do;flag='inflam_1';group+1;end;
   if date=inflam_2 then do;flag='inflam_2';group+1;end;
   if date=inflam_3 then do;flag='inflam_3';group+1;end;
   output;
 end;
 format date mmddyy10.;
 keep  id gender  flag date group;
run;
proc summary data=temp;
by id gender group flag;
var date ;
output out=temp2(drop=_:) min=start max=stop;
run;
data temp3;
 set temp2;
 by id;
 retain trt_status ;
 if first.id then trt_status=0;
 if flag='aaa' then trt_status=1;
 if flag='bbb' then trt_status=2;
run;
data temp4;
 merge temp3 temp3(keep=id flag rename=(id=_id flag=_flag) firstobs=2);
 inflam_event=0;
 if id=_id and _flag=:'inflam_' then inflam_event=1;

 retain inflam_count _count1 _count2;
 if id ne lag(id) then do;inflam_count=0;_count1=0;_count2=0;end;
 if flag=:'inflam_' then _count1=input(scan(flag,-1,'_'),best.);
 if id=_id and _flag=:'inflam_' then _count2=input(scan(_flag,-1,'_'),best.);
 inflam_count=max(_count1,_count2);
 drop _:;
run;
data temp5;
 set temp4;
 by id;
 retain prior_inflam ;
 if first.id then prior_inflam=0;
 if id=lag(id) and lag(inflam_count)=1 then prior_inflam=1;
run;
data temp6;
 set temp5;
 by id;
 retain found;
 if first.id then found=0;
 if flag='enrolldate' then found=1;
 if found;
run;
data want;
 set temp6(drop=group flag found);
 if stop ne '31dec2019'd then stop=stop+1;
run;

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