BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pavlockb3
Calcite | Level 5

I am working with an advertising database for a school project and I've been using the online SAS Studio. I am trying to create a new categorical variable ('TVSpend') from a numerical variable ('TVBudget' which shows the amount spent on TV Advertising) with my dataset. I am having issues with it, but the first 2 variables I created worked fine. I'm sure it is something small I am overlooking, can anyone help?

The error I am getting is right after  "If TVBudget >= 100 and < 200 then TVSpend = 'Medium'; else" and the message is:

_ _______ _
22 180 180
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant,
a missing value, INPUT, PUT.
 
ERROR 180-322: Statement is not valid or it is used out of proper order.


Here is my code:

Libname Final '/home/pavlockb30/my_courses/SASSpring2019/MyData/FinalProject';
Data Final.Advertising;
Infile '/home/pavlockb30/my_courses/SASSpring2019/MyData/FinalProject/Advertising.csv' DSD Firstobs=2;
Input Observation TVBudget RadioBudget NewspaperBudget Sales;
If Sales > Sum(TVBudget, RadioBudget, NewspaperBudget) then Profit = 'Yes'; else
If Sales < Sum(TVBudget, RadioBudget, NewspaperBudget) then Profit = 'No'; else
If Sales = Sum(TVBudget, RadioBudget, NewspaperBudget) then Profit = 'Breakeven';

If TVBudget > RadioBudget and NewspaperBudget then LargestExpenditure = 'TV'; else
If RadioBudget > TVBudget and NewspaperBudget then LargestExpenditure = 'Radio'; else
If NewspaperBudget > TVBudget and RadioBudget then LargestExpenditure = 'Newspaper';

If TVBudget = . then TVSpend = 'Missing'; else
If TVBudget < 100 then TVSpend = 'Low'; else
If TVBudget >= 100 and < 200 then TVSpend = 'Medium'; else
If TVBudget <= 200 then TVSpend = 'High';
   
Run;


Proc Print Data = Final.Advertising Noobs;
Run;
1 ACCEPTED SOLUTION

Accepted Solutions
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

try changing this

If TVBudget >= 100 and < 200 then TVSpend = 'Medium'; else

to this

If TVBudget >= 100 and TVBudget < 200 then TVSpend = 'Medium'; else

If TVBudget >= 200 then TVSpend = 'High';

View solution in original post

3 REPLIES 3
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

try changing this

If TVBudget >= 100 and < 200 then TVSpend = 'Medium'; else

to this

If TVBudget >= 100 and TVBudget < 200 then TVSpend = 'Medium'; else

If TVBudget >= 200 then TVSpend = 'High';

pavlockb3
Calcite | Level 5

That was it! Thank you so much!

ballardw
Super User

Or even

If 100 <= TVBudget < 200 then TVSpend = 'Medium'; else

Another approach is to create a custom format such as

 

Proc format;
value tvspend
. = 'Missing'
low - <100 = 'Low'
100 -< 200 = 'Medium'
200 - High = 'High'
;

And use the format with the numeric value:

proc freq ;
   tables tvbudget;
   format tvbudget tvspend.;
run;

Advantages:

1) I you need to create a different grouping or change boundaries just modify the Proc Format code or create a new format as needed

2) values will likely sort more consistently, as is "High" will come before "Low" or "Medium" when using character variables.

3) if you have multiple variables that would use the same boundaries you simply use the same format, no need for additional variables generally.

4) you don't have to address variable length issues that sometimes arise when your first character value is shorter than others and you get truncated values because you did not explicitly set a length for the target variable(s).

Most of the analysis and graphic procedures will honor groups created by proc format.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 994 views
  • 0 likes
  • 3 in conversation