DATA Step, Macro, Functions and more

If/ Else statement error

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

If/ Else statement error

Can anyone provide some help with the following code:

 

data age;
set models_baseline;
age= Int((survey_compdate-dob)/365.25);
if age = "0" then delete;
else if age >= "18" and <= "35" then agegroup=1;
else if age >= "36" and <= "44" then agegroup=2;
else if age >= "45" and <= "59" then agegroup=3;
else if age >= "60" then agegroup=4;
run;

 

I am receiving the following error messages in the log:

 

7237 data age;
7238 set models_baseline;
7239 age= Int((survey_compdate-dob)/365.25);
7240 if age = "0" then delete;
7241 else if age >= "18" and <= "35" then agegroup=1;
-- ---------
22 180 180
7242 else if age >= "36" and <= "44" then agegroup=2;
-- ---------
22 180 180
7243 else if age >= "45" and <= "59" then agegroup=3;
-- ---------
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.

7244 else if age >= "60" then agegroup=4;
7245 run;

NOTE: Character values have been converted to numeric values at the places given by:
(Line)Smiley SadColumn).
7240:10 7241:16 7242:16 7243:16 7244:16
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.AGE may be incomplete. When this step was stopped there were 0
observations and 148 variables.
WARNING: Data set WORK.AGE was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds

 


Accepted Solutions
Solution
‎06-27-2016 11:59 AM
Super User
Posts: 17,748

Re: If/ Else statement error

The IF condition? Your missing a variable to compare - it isn't valid SAS syntax. 

 

I'm not sue what worked so can't comment beyond that without seeing the code. Part of the problem likely stems from the fact that there's more than one thing wrong with the code so without fixing all the errors you can get unexpected results. 

 

 

View solution in original post


All Replies
Super User
Posts: 17,748

Re: If/ Else statement error

[ Edited ]

You've specified your else statements incorrectly. Also character values for age won't compare accuratkey, 9 is greater than 10 alphabetically. Make sure you have a numeric variable. That's probably why you have the note regarding type conversion in your log. 

 

Heres an example of how it should look:

 

else if age >= 45 and AGE <= 59 then agegroup=3;

 

or 

 

else if 45 <= age <= 59 then agegroup=3;

Contributor
Posts: 21

Re: If/ Else statement error

Hi, 

 

Thank you for your response. I initially did it the way you mentioned it above and I remember receiving an error when I did it that way as well. However, I will try it again. 

Super User
Posts: 17,748

Re: If/ Else statement error

You also shouldn't calculate age with that formula. If you search on here the question of how to calculate age has come up a few times in the last few weeks and you'll find several formulas to calculate age. 

Contributor
Posts: 21

Re: If/ Else statement error

Is there a reason why, its not an acceptable formula. I previously tried several other formulas and the one that I used is the only one that provided me success.
Solution
‎06-27-2016 11:59 AM
Super User
Posts: 17,748

Re: If/ Else statement error

The IF condition? Your missing a variable to compare - it isn't valid SAS syntax. 

 

I'm not sue what worked so can't comment beyond that without seeing the code. Part of the problem likely stems from the fact that there's more than one thing wrong with the code so without fixing all the errors you can get unexpected results. 

 

 

Contributor
Posts: 21

Re: If/ Else statement error

I was able to get everything to work using the age calculation and your suggestions for the age group. I see where I made my error.

Thank you
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 311 views
  • 0 likes
  • 2 in conversation