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

I have a large data set like this :

ID             NAME             BEGIN              END             

191080    John             25/04/2016    01/05/2016        
191080    John           08/06/2016    10/06/2016         
191080    John           21/06/2016    24/06/2016         
191141    Alex           16/01/2017    17/01/2017 

191141    Alex           22/02/2017    29/04/2017         

 

For each first second row, I want to compute the difference (I mean get the number of days) between BEGIN  and END by ID

For exemple for John :

08/06/2016 - 01/05/2016 = 38

21/06/2016 - 10/06/2016 = 11

 

and for Alex : 22/02/2017  - 17/01/2017 = 36

 

I mean how get this :

 

 

ID             NAME             BEGIN              END               RESULT

191080    John             25/04/2016    01/05/2016        
191080    John           08/06/2016    10/06/2016           38
191080    John           21/06/2016    24/06/2016           11
191141    Alex           16/01/2017    17/01/2017 

191141    Alex           22/02/2017    29/04/2017            36

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

You can do it like this

 

data have;
input ID $ NAME $ BEGIN END;
informat BEGIN END DDMMYY10.;
format BEGIN END DDMMYY10.;
datalines; 
191080 John 25/04/2016 01/05/2016
191080 John 08/06/2016 10/06/2016
191080 John 21/06/2016 24/06/2016
191141 Alex 16/01/2017 17/01/2017
191141 Alex 22/02/2017 29/04/2017
;

proc sort data = have;
	by ID;
run;

data want;
	set have;
	
	by ID;
	lag_END = lag1(END);
	if not first.ID then RESULT = abs(intck('day', BEGIN, lag_END));

	drop lag_end;
run;

 

NOTE: Just edited the above, didn't realize about the whole lag-thing 🙂 

View solution in original post

1 REPLY 1
PeterClemmensen
Tourmaline | Level 20

You can do it like this

 

data have;
input ID $ NAME $ BEGIN END;
informat BEGIN END DDMMYY10.;
format BEGIN END DDMMYY10.;
datalines; 
191080 John 25/04/2016 01/05/2016
191080 John 08/06/2016 10/06/2016
191080 John 21/06/2016 24/06/2016
191141 Alex 16/01/2017 17/01/2017
191141 Alex 22/02/2017 29/04/2017
;

proc sort data = have;
	by ID;
run;

data want;
	set have;
	
	by ID;
	lag_END = lag1(END);
	if not first.ID then RESULT = abs(intck('day', BEGIN, lag_END));

	drop lag_end;
run;

 

NOTE: Just edited the above, didn't realize about the whole lag-thing 🙂 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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