BookmarkSubscribeRSS Feed
sasuser_sk
Quartz | Level 8

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

6 REPLIES 6
jimbarbour
Meteorite | Level 14

@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

Patrick
Opal | Level 21

@jimbarbour I believe needs also removal of the opening bracket before CASE

SASKiwi
PROC Star

It looks like t1.SRVC_ID_3Mos and t1.SRVC_ID_6Mos are different data types. You should check if that is the case.

ChrisNZ
Tourmaline | Level 20

> 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.

 

Reeza
Super User
PROC SQL NOEXEC;
SELECT CASE
when missing(t1.SRVC_ID_3Mos) then t1.SRVC_ID_6Mos
else t1.SRVC_ID_6Mos
END AS New_Date
FROM WORK.QUERY_FOR_AVG_3MONTHS_NONNULL t1;
QUIT;



Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 712 views
  • 3 likes
  • 7 in conversation