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

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'

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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';

 

 

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

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';

 

 

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1 reply
  • 572 views
  • 2 likes
  • 2 in conversation