BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Greek
Obsidian | Level 7

Good morning,

I have the following table and I would like to calculate the rate of change between individual years. The second table is my desired output. It can be done easily in excel, but it is a small step of a large sas code and I would rather not interrupt the process... Any ideas?

YearPercentage
14.71%
26.17%
37.82%
49.90%
511.69%

YearPercentageChange from Prev Year
14.71%N/A
26.17%31.00%
37.82%26.74%
49.90%26.60%
511.69%18.08%
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Use a data step, with a lag function to reference the previous cell.

data want;

set have;

pct_change=percentage/lag1(percentage)-1;

format pct_change percent8.2;

run;

View solution in original post

4 REPLIES 4
DBailey
Lapis Lazuli | Level 10

proc sql;

create table want as

select

     t1.Year,

     t1.Percentage,

     t1.percentage / t2.percentage as ChangeFromPrevYear

from

     have t1

     left outer join have t2

          on t1.year-1=t2.year

quit;

Reeza
Super User

Use a data step, with a lag function to reference the previous cell.

data want;

set have;

pct_change=percentage/lag1(percentage)-1;

format pct_change percent8.2;

run;

DBailey
Lapis Lazuli | Level 10

Your approach might be easier for this case.  Most of the time I revert to sql as that's what I grew up with and it seems to be a bit more straightforward if you end up needing more complex criteria than just the previous record.  Also, the data must be sorted for the data step approach...not necessary for sql (although you probably would want it sorted just to help performance).

Greek
Obsidian | Level 7

Thank you so much!

This lag function is everything that I needed.

(I wish I knew sql)

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 1490 views
  • 4 likes
  • 3 in conversation