Write and run SAS programs in your web browser

Lag Function

Accepted Solution Solved
Reply
Contributor
Posts: 57
Accepted Solution

Lag Function

Hi there

 

I want to use the lag function to return the final column on the right of the attached.

 

When the month changes I need the lag to reset itself.

 

Has anyone any helpful code so I can achieve this?

 

Thanks,
Aidan 

 

 

 

01-Oct-151196684187176191671110.24015372.02054
01-Oct-152191671116607083355534.072.020544291.24276
01-Oct-1533355534.07297600636984541.242757551.25583
01-Oct-154369845430395533817150.071.255826130.93742
01-Oct-1553817150.0721523122017629.070.937424071.21171
01-Oct-1562017629.0789444110838061.211713141.31364
01-Oct-15710838065181126806151.313644541
01-Oct-158680615680615.11
01-Nov-1511966841871761966841.050797111.15415
01-Nov-1521916711166070819167111.154152931.12753
01-Nov-1533355534.0729760063355534.071.127529311.21678
01-Nov-1543698454303955336984541.216775621.77351
01-Nov-1553817150.0721523123817150.071.773511492.25574
01-Nov-1562017629.078944412017629.072.255742871
01-Nov-15710838061083806108380611

 


Accepted Solutions
Solution
‎06-21-2016 11:05 AM
Super User
Super User
Posts: 5,991

Re: Lag Function

The last column on the right looks like it is actually the data from the next row and not the previous row that LAG() would allow. There is no LEAD() function so you will probably want read the input data twice.

You didn't provide variable names so lets assume that the first column is called MONTH and the others are VAR2 to VAR7.

 

data want ;
  set have end=eof;
  by MONTH;
  if not eof then set have(keep=VAR6 rename=(VAR6=VAR7) firstobs=2);
  if last.MONTH then VAR7=1 ;
run;

View solution in original post


All Replies
Solution
‎06-21-2016 11:05 AM
Super User
Super User
Posts: 5,991

Re: Lag Function

The last column on the right looks like it is actually the data from the next row and not the previous row that LAG() would allow. There is no LEAD() function so you will probably want read the input data twice.

You didn't provide variable names so lets assume that the first column is called MONTH and the others are VAR2 to VAR7.

 

data want ;
  set have end=eof;
  by MONTH;
  if not eof then set have(keep=VAR6 rename=(VAR6=VAR7) firstobs=2);
  if last.MONTH then VAR7=1 ;
run;
Contributor
Posts: 57

Re: Lag Function

Thanks for the reply

My code is as follows with the updated Variables

Do I need to have an input statement before your code also?

data want ;
set have end=eof;
by MEM_TED;
if not eof then set have(keep=INC_GRUF rename=(INC_GRUF=NEXT_INC_GRUF) firstobs=2);
if last.MEM_TED then NEXT_INC_GRUF=1 ;
run;
Super User
Super User
Posts: 5,991

Re: Lag Function

You do not need an input statement as you need to have a SAS dataset and not just a raw text file to run that code.  If you do not yet have the data in a dataset then run a datastep before hand to create it.  You should probably also change the dataset names of HAVE and WANT to match your actual dataset names.

 

Also your data will need to be sorted by MEM_TED and at least one other variable to make sure that it is is in the right order.  You might want to add a PROC SORT step to make sure if you are not positive that the data is sorted properly.

 

Also note that your MEM_TED variable looks like a date.  If you want to group by that variable make sure that all of the records use the same day of the month (in your example they all use the first day of the month).  Otherwise the different dates will be treated as separate by groups.

Post a Question
Discussion Stats
  • 3 replies
  • 269 views
  • 1 like
  • 2 in conversation