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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 232 views
  • 1 like
  • 2 in conversation