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.2401537 | 2.02054 |
01-Oct-15 | 2 | 1916711 | 1660708 | 3355534.07 | 2.02054429 | 1.24276 |
01-Oct-15 | 3 | 3355534.07 | 2976006 | 3698454 | 1.24275755 | 1.25583 |
01-Oct-15 | 4 | 3698454 | 3039553 | 3817150.07 | 1.25582613 | 0.93742 |
01-Oct-15 | 5 | 3817150.07 | 2152312 | 2017629.07 | 0.93742407 | 1.21171 |
01-Oct-15 | 6 | 2017629.07 | 894441 | 1083806 | 1.21171314 | 1.31364 |
01-Oct-15 | 7 | 1083806 | 518112 | 680615 | 1.31364454 | 1 |
01-Oct-15 | 8 | 680615 | 680615 | . | 1 | 1 |
01-Nov-15 | 1 | 196684 | 187176 | 196684 | 1.05079711 | 1.15415 |
01-Nov-15 | 2 | 1916711 | 1660708 | 1916711 | 1.15415293 | 1.12753 |
01-Nov-15 | 3 | 3355534.07 | 2976006 | 3355534.07 | 1.12752931 | 1.21678 |
01-Nov-15 | 4 | 3698454 | 3039553 | 3698454 | 1.21677562 | 1.77351 |
01-Nov-15 | 5 | 3817150.07 | 2152312 | 3817150.07 | 1.77351149 | 2.25574 |
01-Nov-15 | 6 | 2017629.07 | 894441 | 2017629.07 | 2.25574287 | 1 |
01-Nov-15 | 7 | 1083806 | 1083806 | 1083806 | 1 | 1 |
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;
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;
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.