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;
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?
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.
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.