我试图用嵌套的and
编写一个SQL select语句来获取一些数据:
PROC SQL;
CREATE TABLE WORK.TEMP AS
SELECT DISTINCT
(CASE WHEN t1.TYPE_OF_SALE = 'Cake' AND t1.ADD_ON_FLAG = 0 THEN 'Arrangements'
WHEN t1.TYPE_OF_SALE = 'Cake' AND t1.ADD_ON_FLAG = 1 THEN 'Add-ons'
ELSE 'Direct' END) SALE_TYPE, #ERROR IN THIS LINE
(t1.UNIT_PRICE+t1.SERVICE_CHARGE_AMT) AS TOTAL_VAL
FROM WORK.FB_ORDERS t1
ORDER BY SALE_TYPE;
QUIT;
我出于某种原因在第一行中出错,是否无法将and
与case statement
一起使用?
Error Message:
Error 22-322: Syntax error, expecting one of the following: !,!!,&...
此错误存在于case statement
答案 0 :(得分:5)
我认为问题在于SAS需要as
来定义列别名:
PROC SQL;
CREATE TABLE WORK.TEMP AS
SELECT DISTINCT (CASE WHEN t1.TYPE_OF_SALE = 'Cake' AND t1.ADD_ON_FLAG = 0 THEN 'Arrangements'
WHEN t1.TYPE_OF_SALE = 'Cake' AND t1.ADD_ON_FLAG = 1 THEN 'Add-ons'
ELSE 'Direct'
END) as SALE_TYPE,
(t1.UNIT_PRICE+t1.SERVICE_CHARGE_AMT) AS TOTAL_VAL
FROM WORK.FB_ORDERS t1
ORDER BY SALE_TYPE;
QUIT;
SAS允许列之后的其他表达式,例如FORMAT,所以我认为需要as
来防止表达式中出现歧义。
答案 1 :(得分:2)
你需要一个AS:
proc sql;
select distinct
(case when age<14 and sex='F' then "Young Girl"
when age<14 and sex='M' then "Young Boy"
else "Other" end) as type from sashelp.class;
quit;