- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;