DATA Step, Macro, Functions and more

how to display 2016/02/20 as it is in output in sas AND how to use first. and last. to get output?

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

how to display 2016/02/20 as it is in output in sas AND how to use first. and last. to get output?

dataset:

id    trt start_end

101 TA 2012/02/20

101 TA 2012/03/12

102 TB 2014/03/13

103 TC 2014/03/14

 

Result dataset :

id    sex start_date     end_date

101 M    2012/02/20  2012/03/12

102 F     2014/03/13  2014/03/13

103 M    2014/03/14  2014/03/14

 


Accepted Solutions
Solution
‎02-02-2017 09:16 AM
Super User
Super User
Posts: 7,997

Re: how to display 2016/02/20 as it is in output in sas AND how to use first. and last. to get outpu

Post test data in the form of a datastep.  I can't tell from that wether the dates are numeric dates or text for example.

This is just a guess (that they are number) not tested (as no test data in a datastep):

data want (drop=sd);  
  set have (rename=(start_date=sd));
  by id;
  if first.id then start_date=sd;
  if last.id then do;
    end_date=sd;
    output;
  end;
  format start_date end_date yymmdd10.;
run;

Note your results table does not match your test data - no sex, and trt.

View solution in original post


All Replies
Solution
‎02-02-2017 09:16 AM
Super User
Super User
Posts: 7,997

Re: how to display 2016/02/20 as it is in output in sas AND how to use first. and last. to get outpu

Post test data in the form of a datastep.  I can't tell from that wether the dates are numeric dates or text for example.

This is just a guess (that they are number) not tested (as no test data in a datastep):

data want (drop=sd);  
  set have (rename=(start_date=sd));
  by id;
  if first.id then start_date=sd;
  if last.id then do;
    end_date=sd;
    output;
  end;
  format start_date end_date yymmdd10.;
run;

Note your results table does not match your test data - no sex, and trt.

Respected Advisor
Posts: 3,799

Re: how to display 2016/02/20 as it is in output in sas AND how to use first. and last. to get outpu

[ Edited ]
data date;
   input id:$3. trt:$2. start_end:yymmdd.;
   format start_end yymmdds10.;
   cards;
101 TA 2012/02/20
101 TA 2012/03/12
102 TB 2014/03/13
103 TC 2014/03/14
;;;;
   run;
proc print;
   run;
proc summary nway;
   class id trt;
   var start_end;
   output out=date2(drop=_:) min=start_date max=end_date;
   run;
proc print;
   run;

Capture.PNG 

Contributor
Posts: 23

Re: how to display 2016/02/20 as it is in output in sas AND how to use first. and last. to get outpu

Posted in reply to data_null__

Well done. Thanks for your reply.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 101 views
  • 0 likes
  • 3 in conversation