data work.ifthen; set work.pelicanstores; if Net_Sales < 50 THEN Sales = "0-$50"; ELSE IF Net_Sales > 51 AND < 100 Sales = "$51-$100"; ELSE IF Net_Sales > 100 AND <200 Sales = "$101-$200"; ELSE IF Net_Sales > 200 Sales ="$200+"; Run;
I run clear through the IF, but I am getting an error on my first ELSE .
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant,
a missing value, arrayname, (, +, -, INPUT, NOT, PUT, ^, _NEW_, ~.
I cannot figure out what's wrong with my syntax. Can anyone help?
My objective is to group Net_Sales into categories so they display nicely on Cross Tab.
Thanks for your help
if the above works, make sure to add a length statement for sales to accommodate the values and avoid truncation
see if this correction helps:
data work.ifthen;
set work.pelicanstores;
if Net_Sales < 50 THEN Sales = "0-$50";
ELSE IF Net_Sales > 51 AND Net_Sales< 100 then Sales = "$51-$100";
ELSE IF Net_Sales > 100 AND Net_Sales<200 then Sales = "$101-$200";
ELSE IF Net_Sales > 200 then Sales ="$200+";
Run;
or
data work.ifthen;
set work.pelicanstores;
if Net_Sales < 50 THEN Sales = "0-$50";
ELSE IF 51< Net_Sales< 100 then Sales = "$51-$100";
ELSE IF 100< Net_Sales<200 then Sales = "$101-$200";
ELSE IF Net_Sales > 200 then Sales ="$200+";
Run;
if the above works, make sure to add a length statement for sales to accommodate the values and avoid truncation
One step closer! I am getting some weird results in the new variable. Why is the second part missing?
@AMMAN wrote:
data work.ifthen; set work.pelicanstores; if Net_Sales < 50 THEN Sales = "0-$50"; ELSE IF Net_Sales > 51 AND < 100 Sales = "$51-$100"; ELSE IF Net_Sales > 100 AND <200 Sales = "$101-$200"; ELSE IF Net_Sales > 200 Sales ="$200+"; Run;
Your ranges have gaps LT 50 then GT 51. Also the labels are not clear "0-$50" but 50 is not included in the range.
I would use a VALUE format
proc format;
value sales
0 - 50 = '$0-$50'
50< - 100 = '>$50-$100'
100< - 200 = '>$100-$200'
200 - high = '200+'
;
quit;
Thanks for your help!
Dive into keynotes, announcements and breakthroughs on demand.
Explore Now →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.