I have a table like this:
Yearmonth ID Sales
202001 123 1000
202002 123 1500
202003 123 800
202004 123 2000
202005 123 1300
202001 456 2000
202002 456 2500
202003 456 2700
I want to create a new table that calculates the % change between every yearmonth, group by the ID of the salesperson. How can I do this?
This is a possible solution although it is not in sql but it works
data have;
input Yearmonth ID Sales;
dif= (((lag(Sales) + Sales)*100)/lag(Sales))-100;
format dif 8.2;
Cards;
202001 123 1000
202002 123 1500
202003 123 800
202004 123 2000
202005 123 1300
202001 456 2000
202002 456 2500
202003 456 2700
;
Run;
Greetings Dilmer.
You may want to show what you would expect from that example data. There is more than one way to interpret " change between every yearmonth" and at least one of those would be nasty in SQL because order of processing is not SQL's strong point.
Like this?
data have;
input Yearmonth ID Sales;
Cards;
202001 123 1000
202002 123 1500
202003 123 800
202004 123 2000
202005 123 1300
202001 456 2000
202002 456 2500
202003 456 2700
;
data want;
set have;
by id;
format dif percent10.2;
_lag_sales=lag(sales);
if not first.id then
do;
dif=sales/_lag_sales;
end;
drop _lag_sales;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.