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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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