Conditional Processing with IF-THEN/ELSE Please help me find the error:
Here is the question I am trying to answer and I cannot find the right code to get the "Middle" group processed.
This project will use the data set sashelp.shoes.
Write a SAS program that will:
• Read sashelp.shoes as input.
• Create a new SAS data set, work.shoerange.
• Create a new character variable SalesRange that will be used to categorize the observations into
three groups.
• Set the value of SalesRange to the following:
o Lower when Sales are less than $100,000.
o Middle when Sales are between $100,000 and $200,000, inclusively.
o Upper when Sales are above $200,000.
This is what I have sent through and only the lower and upper groups are calculated:
Data shoerange;
set sashelp.shoes;
Length SalesRange $ 8;
If Sales < 100000.00 then SalesRange = "Lower";
Else If Sales >=1000000.00 >=2000000.00 then SalesRange = "Middle";
Else SalesRange = "Upper";
Keep Product Stores Sales SalesRange;
run;
Hello @heatherml20,
if missing(Sales) then SalesRange = "Missing";
else if Sales < 1e5 then SalesRange = "Lower";
else if Sales <=2e5 then SalesRange = "Middle";
else SalesRange = "Upper";
See the 2018 post "Re: If then statement" for yet another approach.
Hi @heatherml20, a couple of notes:
1. There are extra 0's in the ELSE IF/THEN statement range for the Middle group. I recommend writing out large values with the comma separating every three digits (100,000) and then going through and removing the commas (100000) to ensure you're always capturing the right value.
2. The range for Sales has to be specified as a mathematical range in one condition ( a <= Sales <= b ) or as separate conditions combined with AND (Sales >= a AND Sales <= b).
3. No decimal places required here since we're capturing all values with integers.
Data shoerange;
set sashelp.shoes;
Length SalesRange $ 8;
If Sales < 100000 then SalesRange = "Lower";
Else If 100000 <= Sales <=200000 then SalesRange = "Middle";
Else SalesRange = "Upper";
Keep Product Stores Sales SalesRange;
run;
Thank you ...I did figure out to put separate statements for the Middle category but I was looking for what you have shown me that works better!
Hello @heatherml20,
if missing(Sales) then SalesRange = "Missing";
else if Sales < 1e5 then SalesRange = "Lower";
else if Sales <=2e5 then SalesRange = "Middle";
else SalesRange = "Upper";
See the 2018 post "Re: If then statement" for yet another approach.
Thank you...
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.