Hi, I am trying to create a waterfall in which I need subtract a value of a var1 in previous row from value of var2 of the current row.
For example
Id var1 var2 result
1 50 50 .
2 45 5 50-5= 45
3 30 15 45-15=30
4 20 10 30-10=20
Is it possible to do so in proc report... If so then how?
Thanks in advance.
Easy in a DATA step using the LAG() function.
I'm not sure it can be done in PROC REPORT, but you can always do the math in the DATA step and then use PROC REPORT on the results.
Create a view:
data have ;
input Id var1 var2 ;
cards ;
1 50 50
2 45 5
3 30 15
4 20 10
;
run ;
data v / view = v ;
set have ;
_n_ = lag (var1) ;
if nmiss (_n_) then result = _n_ ;
else result = _n_ - var2 ;
run ;
and then run your proc REPORT against the view V as input.
Kind regards
Paul D.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.