Solved
Contributor
Posts: 62

# 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-15 1 196684 187176 1916711 10.2402 2.02054 01-Oct-15 2 1.91671e+06 1660708 3355534.07 2.02054 1.24276 01-Oct-15 3 3.35553e+06 2976006 3698454 1.24276 1.25583 01-Oct-15 4 3.69845e+06 3039553 3817150.07 1.25583 0.93742 01-Oct-15 5 3.81715e+06 2152312 2017629.07 0.937424 1.21171 01-Oct-15 6 2.01763e+06 894441 1083806 1.21171 1.31364 01-Oct-15 7 1.08381e+06 518112 680615 1.31364 1 01-Oct-15 8 680615 680615 . 1 1 01-Nov-15 1 196684 187176 196684 1.0508 1.15415 01-Nov-15 2 1.91671e+06 1660708 1916711 1.15415 1.12753 01-Nov-15 3 3.35553e+06 2976006 3355534.07 1.12753 1.21678 01-Nov-15 4 3.69845e+06 3039553 3698454 1.21678 1.77351 01-Nov-15 5 3.81715e+06 2152312 3817150.07 1.77351 2.25574 01-Nov-15 6 2.01763e+06 894441 2017629.07 2.25574 1 01-Nov-15 7 1.08381e+06 1083806 1083806 1 1

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

## 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;``````

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

## 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: 62

## 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
Posts: 6,845

## 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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 3 replies
• 361 views
• 1 like
• 2 in conversation