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?
Date | A | B | C | D |
202303 | 254509298.4 | 5644500000 | 5899009298 | |
202304 | 125121716.6 | 5899009298 | 6024131015 | |
202305 | 224951659.8 | 6024131015 | 6249082675 | |
202306 | 189510381.8 | 6249082675 | 6438593057 | |
202307 | 225507645.3 | 6438593057 | 6664100702 | |
202308 | 332878952.2 | 6664100702 | 6996979654 | |
202309 | 69832226.1 | 6996979654 | 7066811880 | |
202310 | 155360032.1 | 7066811880 | 7222171912 | |
202311 | 212155525.5 | 7222171912 | 7434327438 | |
202312 | 104031780 | 7434327438 | 7538359218 | |
202401 | 67473757.38 | 7538359218 | 7605832975 | |
202402 | 120658625.5 | 7605832975 | 7726491601 |
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;
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;
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.
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.