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

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):(Column).
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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

6 REPLIES 6
Reeza
Super User

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;

malikah_sph
Calcite | Level 5

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. 

Reeza
Super User

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. 

malikah_sph
Calcite | Level 5
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.
Reeza
Super User

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. 

 

 

malikah_sph
Calcite | Level 5
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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 6 replies
  • 2341 views
  • 0 likes
  • 2 in conversation