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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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