BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
eemrun
Obsidian | Level 7

I need to calculate rows with a lag variable but struggling to do that. Essentially it should follow this function

For first row, D = A+B

For subsequent rows, D = lag(D)+A

 

Any guidance?

 

DateABCD
202303254509298.45644500000 5899009298
202304125121716.6 58990092986024131015
202305224951659.8 60241310156249082675
202306189510381.8 62490826756438593057
202307225507645.3 64385930576664100702
202308332878952.2 66641007026996979654
20230969832226.1 69969796547066811880
202310155360032.1 70668118807222171912
202311212155525.5 72221719127434327438
202312104031780 74343274387538359218
20240167473757.38 75383592187605832975
202402120658625.5 76058329757726491601
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

No LAG() needed.  Just make sure D is a NEW variable and RETAIN its value.

data want ;
  set have;
  retain d;
  if _n_=1 then d=a+b;
  else d=d+a ;
run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

No LAG() needed.  Just make sure D is a NEW variable and RETAIN its value.

data want ;
  set have;
  retain d;
  if _n_=1 then d=a+b;
  else d=d+a ;
run;
eemrun
Obsidian | Level 7
Oh brilliant! So simple. Thanks so much!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 871 views
  • 1 like
  • 2 in conversation