BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
heatherml20
Calcite | Level 5

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @heatherml20,

 

  • You can also simplify the range condition by omitting the lower bound: The inequality 100000 <= Sales is already implied by the ELSE keyword in conjunction with the previous IF condition Sales < 100000.
  • Using scientific notation (e.g., 100000=1e5) you could avoid typing many zeros.
  • Note that missing values in variable Sales (which do not occur in SASHELP.SHOES) would satisfy the condition Sales < 100000, but should possibly not be classified as "Lower." So, for a real-world dataset the IF-THEN/ELSE statement could look like this:
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.

View solution in original post

5 REPLIES 5
Astounding
PROC Star
Your code added a zero, so your middle range is between 1M and 2M.

Also, you mixed the conditions around for middle. 1M will never be greater than 2M.
Use simple code that you understand such as:
and sales >= 200000 then
antonbcristina
SAS Employee

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;

   

heatherml20
Calcite | Level 5

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!

FreelanceReinh
Jade | Level 19

Hello @heatherml20,

 

  • You can also simplify the range condition by omitting the lower bound: The inequality 100000 <= Sales is already implied by the ELSE keyword in conjunction with the previous IF condition Sales < 100000.
  • Using scientific notation (e.g., 100000=1e5) you could avoid typing many zeros.
  • Note that missing values in variable Sales (which do not occur in SASHELP.SHOES) would satisfy the condition Sales < 100000, but should possibly not be classified as "Lower." So, for a real-world dataset the IF-THEN/ELSE statement could look like this:
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.

heatherml20
Calcite | Level 5

Thank you...

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 5 replies
  • 330 views
  • 5 likes
  • 4 in conversation