Hi,
I am using case when In SAS EG with the code and error below. All fields are numeric and I don't understand what the problem is. Help would be appreciated. Thanks!
PROC SQL NOEXEC;
SELECT (CASE
when(t1.SRVC_ID_3Mos is missing ) then t1.SRVC_ID_6Mos
else t1.SRVC_ID_6Mos
END) AS CALCULATION
FROM WORK.QUERY_FOR_AVG_3MONTHS_NONNULL t1;
QUIT;
;*';*";*/;quit;run;
2 OPTIONS PAGENO=MIN;
3 OPTION DEBUG=DBMS_SELECT SQL_IP_TRACE=(NOTE, SOURCE);
4 PROC SQL NOEXEC;
5 SELECT (CASE
6 when(t1.SRVC_ID_3Mos is missing ) then t1.SRVC_ID_6Mos
7
8 else t1.SRVC_ID_6Mos
9 END) AS CALCULATION
10 FROM WORK.QUERY_FOR_AVG_3MONTHS_NONNULL t1;
ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.
11 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
12 %PUT SQL_IPTRACE_RESULT=&SYS_SQL_IP_ALL;
SQL_IPTRACE_RESULT=-1
13 OPTIONS SQL_IP_TRACE=(NONE);
14 QUIT; RUN;
15
@sasuser_sk wrote:
PROC SQL NOEXEC;
SELECT (CASE
when(t1.SRVC_ID_3Mos is missing ) then t1.SRVC_ID_6Mos
else t1.SRVC_ID_6Mos
END) AS CALCULATION
FROM WORK.QUERY_FOR_AVG_3MONTHS_NONNULL t1;
QUIT;
Try removing the parenthesis after "END".
However, note, the code doesn't make sense to me. Maybe I'm misunderstanding but it looks like that if the 3Mos value is missing the 6Mos value is used instead, but if it is not missing then the 6Mos value is also used. Did you mean to use the 3Mos value if not missing?
Like this, maybe?
PROC SQL NOEXEC;
SELECT (CASE
when(t1.SRVC_ID_3Mos is missing ) then t1.SRVC_ID_6Mos
else t1.SRVC_ID_3Mos
END AS CALCULATION
FROM WORK.QUERY_FOR_AVG_3MONTHS_NONNULL t1;
QUIT;
Jim
@jimbarbour I believe needs also removal of the opening bracket before CASE
It looks like t1.SRVC_ID_3Mos and t1.SRVC_ID_6Mos are different data types. You should check if that is the case.
> All fields are numeric
SAS doesn't seem to think so.
Also, no need for brackets to write a CASE clause.
Also, use the coalesce() function here instead of CASE.
Your code as posted works:
data QUERY_FOR_AVG_3MONTHS_NONNULL;
SRVC_ID_3Mos = .;
SRVC_ID_6Mos = 1;
output;
SRVC_ID_3Mos = 1;
SRVC_ID_6Mos = 1;
output;
run;
PROC SQL NOEXEC;
SELECT (CASE
when(t1.SRVC_ID_3Mos is missing ) then t1.SRVC_ID_6Mos
else t1.SRVC_ID_6Mos
END) AS CALCULATION
FROM WORK.QUERY_FOR_AVG_3MONTHS_NONNULL t1;
QUIT;
Log:
78 PROC SQL NOEXEC; 79 SELECT (CASE 80 when(t1.SRVC_ID_3Mos is missing ) then t1.SRVC_ID_6Mos 81 82 else t1.SRVC_ID_6Mos 83 END) AS CALCULATION 84 FROM WORK.QUERY_FOR_AVG_3MONTHS_NONNULL t1; NOTE: Statement not executed due to NOEXEC option. 85 QUIT; NOTE: Verwendet wurde: PROZEDUR SQL - (Gesamtverarbeitungszeit):
Start a fresh SAS session and run my code for a test, then run yours.
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.