- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you...