SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
VarunD
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

What does the log say, I am going to guess that:

MBR_DT_OF_BTH

Is not a numeric date variable, 

VarunD
Obsidian | Level 7

Deleting this for privacy. Thanks RW9 for spotting the error. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1164 views
  • 1 like
  • 2 in conversation