I have a dataset which looks like this.
data have;
infile datalines truncover;
input type $ quarter phase counter percent avg std;
datalines;
XX-A 202001 1 30 20 20 1
XX-A 202002 1 50 2 20 1
XX-A 202003 1 60 2 20 2
XX-A 202004 1 50 10 20 2
XX-B 202001 2 30 4 30 3
XX-B 202002 2 60 4 30 3
XX-B 202003 2 70 1 30 3
XX-B 202004 2 20 3 30 3
I want to create a new column named STATUS based on the following if/case statements:
If count is < (avg + std * 1) = 'green'
if count > (avg + std * 1) and < (avg +std * 2) = 'yellow'
if count > (avg + std * 2) and (avg + std * 3) = 'orange'
if count > (avg + std * 3) = 'red'
The first step is always determining in your own mind (i.e. not even SAS code at this point) valid rules that will create these colors. I have added in red modifications to what you typed to produce valid rules
counter <= (avg + std * 1) then 'green'
counter > (avg + std * 1) and <= (avg +std * 2) then 'yellow'
counter > (avg + std * 2) and <= (avg + std * 3) then 'orange'
counter > (avg + std * 3) then 'red'
From there, SAS code is relatively easy to produce.
data want;
set have;
length color $ 6;
if counter <= (avg + std * 1) then color= 'green';
else if counter > (avg + std * 1) and counter <= (avg +std * 2) then color='yellow';
else if counter > (avg + std * 2) and counter <= (avg + std * 3) then color= 'orange';
else if counter > (avg + std * 3) then color= 'red';
run;
There are some minor simplications you can make to the above, such as changing the next to last line to
else color='red';
The first step is always determining in your own mind (i.e. not even SAS code at this point) valid rules that will create these colors. I have added in red modifications to what you typed to produce valid rules
counter <= (avg + std * 1) then 'green'
counter > (avg + std * 1) and <= (avg +std * 2) then 'yellow'
counter > (avg + std * 2) and <= (avg + std * 3) then 'orange'
counter > (avg + std * 3) then 'red'
From there, SAS code is relatively easy to produce.
data want;
set have;
length color $ 6;
if counter <= (avg + std * 1) then color= 'green';
else if counter > (avg + std * 1) and counter <= (avg +std * 2) then color='yellow';
else if counter > (avg + std * 2) and counter <= (avg + std * 3) then color= 'orange';
else if counter > (avg + std * 3) then color= 'red';
run;
There are some minor simplications you can make to the above, such as changing the next to last line to
else color='red';
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.