BookmarkSubscribeRSS Feed
Mei121
Calcite | Level 5

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.

2 REPLIES 2
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
hashman
Ammonite | Level 13

@Mei121:

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.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 5592 views
  • 1 like
  • 3 in conversation