I am very new to SAS. I am trying to create a Case When Statement.
/* TOT_PREM */
Case When t1.TOT_PREM IS NULL Then 0 Else
(Sum(t1.TOT_PREM)) END AS Total Premium,
I am getting an error message ERROR22-322: Syntax error, expecting one of the following: !,!!,&,*,**,+,-,/,<,<=,<>,=,.,.,.=,?,AND,BETWEEN,
What am I doing wrong?
When you get an error copy the entire procedure or data step code that generates the error from log including the error messages and paste into a code box opened using the forums {I} icon. This preserves the formatting of the error messages and the indicators that often show what SAS identified as the triggering error.
And example:
244 proc sql; 245 select case when sex='M' then 0 246 else 1 end as Total whatsit ------- 22 ERROR 22-322: Syntax error, expecting one of the following: ',', AS. 247 from sashelp.class; 248 quit; NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
Notice that I am getting the same error: 22-322 with slightly different "expected" but the underscores show that "whatsit" is out of place.
In this case if I wanted a variable "Total Whatsit" which has a space in the middle then I have to use the construct "Total whatsit"n to indicate a name literal that violates typical variable name rules. OR perhaps I thought there was another variable whatsit I wanted to select but just forgot to place the required comma between variables or expressions in SQL.
Since you did not include the entire code from the log your specific error appears to be in a construct that allows more options than the example I showed but the basic principal applies: one of the expected characters or keywords needs to be before the underscored word or you need to use a different variable name as the result of the case expression. I strongly suggest not using the name literal as the added quotes and required n following get tiresome.
Try MISSING() instead.
SAS doesn't really have a concept of NULL.
when missing(t1.tot_prem) then 0
else ...
@EKeimig wrote:
I am very new to SAS. I am trying to create a Case When Statement.
/* TOT_PREM */
Case When t1.TOT_PREM IS NULL Then 0 Else
(Sum(t1.TOT_PREM)) END AS Total Premium,
I am getting an error message ERROR22-322: Syntax error, expecting one of the following: !,!!,&,*,**,+,-,/,<,<=,<>,=,.,.,.=,?,AND,BETWEEN,
What am I doing wrong?
This is the message I get now.
46 Case When Missing(t1.TOT_PREM) Then 0 Else
47 (Sum(t1.TOT_PREM)) END AS Total Premium,
_______
22
ERROR 22-322: Syntax error, expecting one of the following: ',', AS, FROM.
You don't need to do that at all...SAS ignores missing values in the sum. If you were doing a mean this may be required, but it's not for a SUM().
Try SUM(t1.TOT_PREM) instead.
@EKeimig wrote:
This is the message I get now.
46 Case When Missing(t1.TOT_PREM) Then 0 Else
47 (Sum(t1.TOT_PREM)) END AS Total Premium,
_______
22
ERROR 22-322: Syntax error, expecting one of the following: ',', AS, FROM.
When you get an error copy the entire procedure or data step code that generates the error from log including the error messages and paste into a code box opened using the forums {I} icon. This preserves the formatting of the error messages and the indicators that often show what SAS identified as the triggering error.
And example:
244 proc sql; 245 select case when sex='M' then 0 246 else 1 end as Total whatsit ------- 22 ERROR 22-322: Syntax error, expecting one of the following: ',', AS. 247 from sashelp.class; 248 quit; NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
Notice that I am getting the same error: 22-322 with slightly different "expected" but the underscores show that "whatsit" is out of place.
In this case if I wanted a variable "Total Whatsit" which has a space in the middle then I have to use the construct "Total whatsit"n to indicate a name literal that violates typical variable name rules. OR perhaps I thought there was another variable whatsit I wanted to select but just forgot to place the required comma between variables or expressions in SQL.
Since you did not include the entire code from the log your specific error appears to be in a construct that allows more options than the example I showed but the basic principal applies: one of the expected characters or keywords needs to be before the underscored word or you need to use a different variable name as the result of the case expression. I strongly suggest not using the name literal as the added quotes and required n following get tiresome.
I know this is old, but in case anyone else finds it:
There are a few problems with the code here. One is just extraneous parentheses, one is that there's "Total Premium" has a space in it...but the larger one is the logic of the CASE statement. Remember that CASE looks at a single row and returns a result for each row, so you can't have a SUM *inside* your CASE statement. The order must be reversed, like so:
SUM(CASE WHEN t1.TOT_PREM IS NULL THEN 0 ELSE t1.TOT_PREM END) AS Total_Premium
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: