BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aidan
Quartz | Level 8

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Tom
Super User Tom
Super User

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;
Aidan
Quartz | Level 8
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;
Tom
Super User Tom
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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