[SOLVED] Calculate the lag of years by groups of records and between consecutive dates


This Content is from Stack Overflow. Question asked by NewUsr

I would like to ask your help to do the following:

I have a data set that looks like this:

 ID        Date1       Date2       
 a         2015        2015            
 a         2016        2016            
 a         2017        2018            
 a         2018        2020            
 b         2015        2016            
 b         2018        2019            
 b         2020        2020  
....      .....        .....

Desired output:

 ID         Lag      
 a          Start       
 a           1        
 a           1        
 a           0        
 b          Start         
 b           3        
 b           1

…. ….. …..

I need to count how many years pass from Date2 to Date1 next row for each ID. For example: from Date2 = 2015 (first row) to Date1 2016 (second row) there is 1 year of difference. Can anyone help me please? The first row in the desired output should be set to “Start” to indicate that the lag cannot be calculated because it is the starting point.

Thank you in advance


Simple lag() and a first.

data want ;
  set have ;
  by ID ;

  l = lag(Date2) ;
  if not first.ID then diff = sum(Date1,-l) ;
run ;

This Question was asked in StackOverflow by NewUsr and Answered by Chris J It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?