BookmarkSubscribeRSS Feed
sieghild
Calcite | Level 5

Ik krijg altijd de melding "ERROR: Result of WHEN clause 2 is not the same data type as the preceding results"

p.end_date is een char van 6 

p.total_days is ook een char van 6

 

case
when p.end_date = '20211231' then 'onbepaalde duur'
when p.total_days/1000 > 30 then 'minstens 1 maand'
else ''

end as ziektetermijn,

 

hoe kan ik dit oplossen?

 

3 REPLIES 3
sbxkoenk
SAS Super FREQ

Hello,

 

Not sure about how your original data set is structured like, but you may be after something like the below:

data have;
LENGTH end_date $ 8 total_days $ 6;
end_date='20211231'; total_days='';      output;
end_date=''        ; total_days='40000'; output;
run;

PROC SQL noprint;
 create table want as
 select *
, case
when p.end_date = '20211231'           then 'onbepaalde duur'
when input(p.total_days,6.)/1000 > 30  then 'minstens 1 maand'
else ''
end as ziektetermijn
 from have as p;
QUIT;
/* end of program */

Cheers,

Koen

sbxkoenk
SAS Super FREQ

Hello,

See also my previous post!!

Your "end_date" needs to be char 8 instead of char 6.

Not such a good practice to work with dates stored in character variables. Make your date a real (SAS) date!

Koen

FreelanceReinh
Jade | Level 19

Hallo @sieghild en hartelijk welkom op het forum!

 

[Switching to English now, sorry! :-)] Normally you shouldn't get this error message with your code as posted because the "result of WHEN clause 2" -- 'minstens 1 maand' -- is a character value as is the "preceding result" -- 'onbepaalde duur'. You would get the error message if you replaced one of the two character strings with a numeric value, e.g., 1.

 

Here is an example where an exact copy of your code works as expected:

 

data have;
input id end_date :$8. total_days;
cards;
1 20211231 50000
2 20210713 40000
3 20211011 20000
;

proc sql;
select
case
when p.end_date = '20211231' then 'onbepaalde duur'
when p.total_days/1000 > 30 then 'minstens 1 maand'
else ''
end as ziektetermijn,
id
from have p;
quit;

 

 


@sieghild wrote:

p.end_date is een char van 6 

p.total_days is ook een char van 6


Are you saying that these are character variables of length 6? That would be a problem because p.end_date could never be equal to an 8-character string such as '20211231' and attempting to divide p.total_days by 1000 would cause an ERROR: Expression using division (/) requires numeric types.

 

To avoid all these type conflicts, you just need to know the types (and possible values) of your variables and handle them accordingly when coding comparisons, calculations or value assignments.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 829 views
  • 0 likes
  • 3 in conversation