BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fengyuwuzu
Pyrite | Level 9
data have;
    input id $ line startdate $10.;
datalines;
    A 1 2015-06-20
    A 2 2015-07-31
    A 3 2015-08-27
    B 1 2016-05-01
    B 2 2016-08-02
;
RUN;

 

what I want:

ID  line   startdate         enddate

A    1    2015-06-20    2015-07-30

A    2    2015-07-31    2015-08-26

A    3    2015-08-27    .

B    1    2016-05-01    2016-08-01

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data have;
    input id $ line startdate yymmdd10.;
	format startdate yymmdd10.;
datalines;
a 1 2015-06-20
a 2 2015-07-31
a 3 2015-08-27    
b 1 2016-05-01    
b 2 2016-08-02
;
run;

proc sql;
	create table want as select h.*,j.startdate-1 as enddate format=yymmdd10.
		from have as h left join have as j
		on h.id=j.id and h.line=j.line-1;
quit;
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
data have;
    input id $ line startdate yymmdd10.;
	format startdate yymmdd10.;
datalines;
a 1 2015-06-20
a 2 2015-07-31
a 3 2015-08-27    
b 1 2016-05-01    
b 2 2016-08-02
;
run;

proc sql;
	create table want as select h.*,j.startdate-1 as enddate format=yymmdd10.
		from have as h left join have as j
		on h.id=j.id and h.line=j.line-1;
quit;
--
Paige Miller
Tom
Super User Tom
Super User

First thing is to define your date variable to have dates and not character strings.

You can use this trick using an extra SET with FIRSTOBS=2 to create a "look ahead" or "lead" value for start date.

 

Take care that when you are on the last observation for a group that you not try to use the date from the first observation of the next group by using BY variable processing.

data have;
  input id $ line startdate :yymmdd10.;
  format startdate yymmdd10.;
datalines;
A 1 2015-06-20
A 2 2015-07-31
A 3 2015-08-27
B 1 2016-05-01
B 2 2016-08-02
;

data want ;
  set have ;
  by id;
  set have(firstobs=2 keep=startdate rename=(startdate=enddate)) have(drop=_all_ obs=1);
  if last.id then call missing(enddate);
  if not missing(enddate) then enddate=enddate-1;
run;
Obs    id    line     startdate       enddate

 1     A       1     2015-06-20    2015-07-30
 2     A       2     2015-07-31    2015-08-26
 3     A       3     2015-08-27             .
 4     B       1     2016-05-01    2016-08-01
 5     B       2     2016-08-02             .

 

fengyuwuzu
Pyrite | Level 9

Thank you Tom and Paige!

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 3 replies
  • 2183 views
  • 0 likes
  • 3 in conversation