DATA Step, Macro, Functions and more

how to get first obs value of start date and last obs value of end date where multiple obs present

Reply
Super Contributor
Posts: 272

how to get first obs value of start date and last obs value of end date where multiple obs present

Dear,

In my data, each subject has startdate(sdate) enddate(edate) of dose of a drug taken. For the derivation an variable I need an output dataset with first.sdate of each subject and last.edate of each subject. Thank you

 

output need;

id           sdate                    edate

1            05OCT2015         30OCT2015

2            05OCT2015         29OCT2015

3            06OCT2015         25OCT2015

 

 

data one;
input id sdate  edate date9.
datalines;
1 05OCT2015 06OCT2015
1 06OCT2015 17OCT2015
1 17OCT2015 30OCT2015
2 05OCT2015 10OCT2015
2 11OCT2015 20OCT2015
2 20OCT2015 25OCT2015
2 26OCT2015 29OCT2015
3 06OCT2015 17OCT2015
3 17OCT2015 25OCT2015
;
PROC Star
Posts: 283

Re: how to get first obs value of start date and last obs value of end date where multiple obs prese

Posted in reply to knveraraju91

 


data one;
input id sdate : date9. edate :date9.;
format sdate edate date9.;
datalines;
1 05OCT2015 06OCT2015
1 06OCT2015 17OCT2015
1 17OCT2015 30OCT2015
2 05OCT2015 10OCT2015
2 11OCT2015 20OCT2015
2 20OCT2015 25OCT2015
2 26OCT2015 29OCT2015
3 06OCT2015 17OCT2015
3 17OCT2015 25OCT2015
;


data want;
set one;
by id;
retain _sdate;
if first.id then _sdate=sdate;
else if last.id then sdate=_sdate;
if last.id;
drop _sdate;
run;

 

Regards,

Naveen Srinivasan

Super Contributor
Posts: 340

Re: how to get first obs value of start date and last obs value of end date where multiple obs prese

Posted in reply to knveraraju91

Try:

 

Proc SQL;
  Create Table Want As
    Select id,
	  Min(sdate) as sdate Format=Date9.,
	  Max(edate) as edate Format=Date9.
    From one
    Group By id;
Quit;
Super User
Posts: 11,343

Re: how to get first obs value of start date and last obs value of end date where multiple obs prese

Posted in reply to knveraraju91

If there are no other variables involved:

 

proc summary data=have nway;

   class id;

   var sdate edate;

   ouput out=want(drop= _Smiley Happy min(sdate)= max(edate)=;

run;

Ask a Question
Discussion stats
  • 3 replies
  • 155 views
  • 2 likes
  • 4 in conversation