BookmarkSubscribeRSS Feed
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;
percent_change = lag(cuml_vol) - cuml_vol ;


SAS output.PNG






Super User


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;
  alldata (
    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 _:;

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

Calcite | Level 5

thank you so much - it worked 🙂


Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.


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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1 like
  • 2 in conversation