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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

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;
J_J_J
Obsidian | Level 7

Kurt, thank you so much for good solution!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

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.

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
  • 836 views
  • 2 likes
  • 2 in conversation