Hi everyone!
I have data like this:
data have; ID='A'; year=2020; month=11; rate=50; output; ID='A'; year=2020; month=12; rate=20; output; ID='A'; year=2021; month=1; rate=70; output; ID='A'; year=2021; month=2; rate=50; output; ID='A'; year=2021; month=3; rate=50; output; ID='A'; year=2021; month=4; rate=45; output; ID='A'; year=2021; month=5; rate=40; output; ID='A'; year=2021; month=6; rate=35; output; ID='A'; year=2021; month=7; rate=30; output; ID='A'; year=2021; month=8; rate=25; output; ID='A'; year=2021; month=9; rate=25; output; ID='A'; year=2021; month=10; rate=25; output; ID='A'; year=2021; month=11; rate=20; output; ID='A'; year=2021; month=12; rate=15; output; ID='B'; year=2020; month=11; rate=65; output; ID='B'; year=2020; month=12; rate=35; output; ID='B'; year=2021; month=1; rate=85; output; ID='B'; year=2021; month=2; rate=65; output; ID='B'; year=2021; month=3; rate=65; output; ID='B'; year=2021; month=4; rate=60; output; ID='B'; year=2021; month=5; rate=55; output; ID='B'; year=2021; month=6; rate=50; output; ID='B'; year=2021; month=7; rate=45; output; ID='B'; year=2021; month=8; rate=40; output; ID='B'; year=2021; month=9; rate=40; output; ID='B'; year=2021; month=10; rate=40; output; ID='B'; year=2021; month=11; rate=35; output; ID='B'; year=2021; month=12; rate=30; output; ;run;
and I'd like to find result to each row comparing previous month rate with next month rate:
| id | year | month | rate | result |
| A | 2020 | 11 | 50 | |
| A | 2020 | 12 | 20 | green |
| A | 2021 | 1 | 70 | red |
| A | 2021 | 2 | 50 | green |
| A | 2021 | 3 | 50 | yellow |
| A | 2021 | 4 | 45 | green |
| A | 2021 | 5 | 40 | green |
| A | 2021 | 6 | 35 | green |
| A | 2021 | 7 | 30 | green |
| A | 2021 | 8 | 25 | green |
| A | 2021 | 9 | 25 | yellow |
| A | 2021 | 10 | 25 | yellow |
| A | 2021 | 11 | 20 | green |
| A | 2021 | 12 | 15 | green |
| B | 2020 | 11 | 65 | |
| B | 2020 | 12 | 35 | green |
| B | 2021 | 1 | 85 | red |
| B | 2021 | 2 | 65 | green |
| B | 2021 | 3 | 65 | yellow |
| B | 2021 | 4 | 60 | green |
| B | 2021 | 5 | 55 | green |
| B | 2021 | 6 | 50 | green |
| B | 2021 | 7 | 45 | green |
| B | 2021 | 8 | 40 | green |
| B | 2021 | 9 | 40 | yellow |
| B | 2021 | 10 | 40 | yellow |
| B | 2021 | 11 | 35 | green |
| B | 2021 | 12 | 30 | green |
If next month rate is less than previous one then result is 'green', if gather then 'red' and if it doesn't change then it's 'yellow'
Do you have any good ideas, how to do it without creating tables for each pair of months?
Have a nice day
Kudos for supplying usable example data.
Try this:
data want;
set have;
by id;
length result $6;
l_rate = lag(rate);
if not first.id
then do;
if l_rate > rate then result = "Green";
else if l_rate < rate then Result = "Red";
else result = "Yellow";
end;
drop l_rate;
run;
Kudos for supplying usable example data.
Try this:
data want;
set have;
by id;
length result $6;
l_rate = lag(rate);
if not first.id
then do;
if l_rate > rate then result = "Green";
else if l_rate < rate then Result = "Red";
else result = "Yellow";
end;
drop l_rate;
run;
Kurt, thank you so much for good solution!
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 the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.