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;
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.
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.
Ready to level-up your skills? Choose your own adventure.