Hi,
I am trying to calculate age for Missing values as per the following step and getting syntax error.
What am I doing wrong here :
Varun
PROC SQL;
CREATE TABLE Employee2 AS
SELECT DISTINCT t1.Table1,
variable1,
variable2,
case when variable1 NE . and variable2 LT 18.
then '17 & Younger'
case when variable1 NE . and variable2 GT 18.
then '18 & Older'
case when variable1 EQ .
then YRDIF(Today(),variable2,'ACT/ACT'))
else 'Not defined'
end as Age
else 'Not defined'
end as Age
FROM Table2 t1
;
QUIT;
That shows where you issue is, your case when statement is invalid.
Case statements have two forms:
case when <condition> then <value> when <condition> then <value> else <value> end
Or:
case <variable> when <condition> then <value> when <condition> then <value> else <value> end
So:
case when MBR_AGE NE . and MBR_AGE LT 18. then '17 & Younger' <--- valid case when MBR_AGE NE . and MBR_AGE GT 18. then '18 & Older' <--- invalid, extra "case" case when MBR_AGE EQ . then YRDIF(Today(),MBR_DT_OF_BTH,'ACT/ACT')) <---- invalid extra 'case' else 'Not defined' <---valid only when above corrected end as Age else 'Not defined' <--- invalid, no case defined. end as Age
What does the log say, I am going to guess that:
MBR_DT_OF_BTH
Is not a numeric date variable,
Deleting this for privacy. Thanks RW9 for spotting the error.
That shows where you issue is, your case when statement is invalid.
Case statements have two forms:
case when <condition> then <value> when <condition> then <value> else <value> end
Or:
case <variable> when <condition> then <value> when <condition> then <value> else <value> end
So:
case when MBR_AGE NE . and MBR_AGE LT 18. then '17 & Younger' <--- valid case when MBR_AGE NE . and MBR_AGE GT 18. then '18 & Older' <--- invalid, extra "case" case when MBR_AGE EQ . then YRDIF(Today(),MBR_DT_OF_BTH,'ACT/ACT')) <---- invalid extra 'case' else 'Not defined' <---valid only when above corrected end as Age else 'Not defined' <--- invalid, no case defined. end as Age
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.