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?
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
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
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.
Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.
Explore Now →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.