Difference in dates in SAS by group -


consider data set test in following form:

 group   date   1      05jan2014   1      08jan2014   1      14jan2014   2      05jan2013   2      10feb2015   2      27feb2015 

i want calculate difference in dates based on group. code below takes difference between every 2 dates:

  data test;   datediff = dif(date);   run; 

how take difference between dates in 1 group? moreover, there way take difference between last , first dates in each group?

starting this:

 data test;   datediff = dif(date);   run; 

let's address isues 1 @ time. first, adding set statement , by statement, can add first , last allow determine in group. assumes it's sorted by group.

data test2;   set test;   group;   datediff=dif(date); run; 

this doesn't work differently (assuming had set statement originally, anyway). now, have new options.

first, while can use dif, recommend retain method this. can more see it's doing, , avoid common pitfalls: particularly, lag , dif don't compare previous record - create queue , compare that, can lead complications when using conditional statements.

data test2;   set test;   group;   retain last_date;   if first.group last_date=0;   datediff = date - last_date;   output;   last_date = date; run; 

this same thing before - compares previous value current value - makes bit easier see, , add in option reset last_date variable when first.group true - meaning we're on first row of new value of group. won't drop of these intermediate variables, in production code can , should. retain means value persist across rows (instead of being reset every time new row).

now have 1 variable tracking previous row's value of date, it's pretty easy see how can first->last difference.

data test2;   set test;   group;   retain last_date orig_date;   if first.group do;     last_date=0;     orig_date=date;  **new;   end;   datediff = date - last_date;    if last.group group_datediff = date-orig_date;  **new;   output;   last_date = date; run; 

now we've done same thing before - we're resetting orig_date each time see first.group , calculating group_datediff when hit last.group.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -