在Proc sql中使用嵌套大小写,我需要根据指定的年份和月份获取列的值。例如 - 如果年份是2018年,月份是12月那么它应该返回列x201811的值,如果它是null,它应该返回零。我在下面写的代码给出了语法错误。请提出解决方案。
CASE WHEN (( Year(date) = 2018 ) AND ( Month(date) = 12 )) THEN x201811
WHEN x201811 IS NULL THEN 0
ELSE x201811
WHEN ((Year(date)=2018) AND (Month(Date)=11)) THEN x201810
WHEN x201810 IS NULL THEN 0
ELSE x201810
答案 0 :(得分:1)
您可以使用COALESCE
函数使第一个非空值成为指定值。
CASE
WHEN (( Year(date) = 2018 ) AND ( Month(date) = 12 ))
THEN coalesce (x201811, 0)
WHEN ((Year(date)=2018) AND (Month(Date)=11))
THEN coalesce (x201810, 0)
ELSE coalesce (x201810, 0)
END as got_a_value
替代方法是嵌套CASE
语句
CASE
WHEN (( Year(date) = 2018 ) AND ( Month(date) = 12 ))
THEN CASE WHEN x201811 is null then 0 else x201811 end
WHEN ((Year(date)=2018) AND (Month(Date)=11))
THEN CASE WHEN x201810 is null then 0 else x201810 end
ELSE ...
END as got_a_value