SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
shridula95
Calcite | Level 5

Hello

I am trying to use the sashelp.shoes dataset as an input in order to create an output dataset. Additionally, I have to create a new character variable SalesRange, which has the following 3 categories:-

-Lower when Sales are less than $100,000.
-Middle when Sales are between $100,000 and $200,000, inclusively.
-Upper when Sales are above $200,000.

 

Produced below is my code. However, after different combinations of the if then else statements, I keep getting an error saying - Expecting an arithmetic operator and The symbol is not recognized and will be ignored. Any help is much appreciated. Thanks in advance!!

 

data work.shoerange;
set sashelp.shoes;
if Sales<100,000 then SalesRange="Lower";
if 100,000<=Sales<=200,000 then SalesRange="Middle";
if Sales<200,000 then SalesRange="Upper";
run;

4 REPLIES 4
Tom
Super User Tom
Super User

You have a comma in the middle of the IF statements.  For example in the first IF it is separating the number 100 from the number zero.

621   data work.shoerange;
622   set sashelp.shoes;
623   if Sales<100,000 then SalesRange="Lower";
                  -
                  388
                  200

Remove the commas.

 

How long should the new SALESRANGE variable be?

How long have you defined it to be?

What will happen when you try to assign the string 'Middle' to SALESRANGE?

 

 

Reeza
Super User
1. Remove comma in your numbers
2. Use ELSE IF rather than just IF (THIS IS IMPORTANT for the next issue)
3. Does your third category/condition make sense? I don't think it matches your condition above and will overwrite all your previous values except anything over 200k.
4. Check the length of your SalesRange variable. It will likely get truncated and you may want to explicitly set the length. SAS defaults the length to the first assigned value.
s_lassen
Meteorite | Level 14

Using the suggestions by @Reeza, your code can be rewritten like this:

data work.shoerange;
  set sashelp.shoes;
  length SalesRange $8;
  if sales<100000 then SalesRange='Lower';
  else if sales<=200000 then SalesRange='Middle';
  else SalesRange='Upper';
run;

 

Or you can create a format and use that:

proc format lib=work;
  value SalesRange
    low-<100000='Lower'
    100000-200000='Middle'
    other='Upper'
    ;
run;

data work.shoerange;
  set sashelp.shoes;
  SalesRange=put(sales,SalesRange8.);
run;

Or you can just use the format directly in e.g. PROC PRINT or PROC FREQ without calculating the variable.

 

In the real world, you may want to add a category for missing values. And you may want to save your format in another library than WORK.

Sajid01
Meteorite | Level 14

Proc sql can also be use as shown below:

proc sql;
select *,
case 
when sales < 100000 then "Lower"
when sales between 100000 and 200000 then "Middle"
when sales > 200000 then "Upper"
end as SalesRange
from sashelp.shoes;
quit;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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