BookmarkSubscribeRSS Feed
mpangli
Calcite | Level 5
 

I want to be able to calculate the percentage change between the top value then the value below (CUML_VOL) and keep moving down a row and doing the same

so for the first line I want to be able to calculate the difference between 66040.047 and 65895.855 as a starting point.

i want the result to be displayed on the top row (but as you can see it stores it on the 2nd row and so on)

and to keep working down until it gets to a new UC_POD_EXT 

so in this case it would restart the calculation when it gets to UC_POD_EXT = 1346502

 

this is the code I tried: 

 

 

data alldata;
set alldata;
retain percent_change;
by uc_pod_ext;
if last.uc_pod_ext then percent_change=0;
else
percent_change = lag(cuml_vol) - cuml_vol ;
run;

 

SAS output.PNG

 

 

 

 

 

2 REPLIES 2
Kurt_Bremser
Super User

This:

data alldata;
set alldata;

is a bad idea; if something untoward happens in the DATA step, you have destroyed your dataset.

Do a "look-ahead":

data want;
merge
  alldata
  alldata (
    firstobs=2
    keep=uc_pod_ext cuml_vol
    rename=(uc_pod_ext=_uc cuml_vol=_cuml)
  )
;
if uc_pod_ext = _uc
then percent_change = _cuml - cuml_vol;
else percent_change = 0;
drop _:;
run;

Untested; for tested code, supply example data in usable form, namely a data step with datalines; do not post pictures, and post your code into a code box opened with the "little running man" button right next to the one indicated:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

mpangli
Calcite | Level 5

thank you so much - it worked 🙂

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 776 views
  • 1 like
  • 2 in conversation