BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
new_sas_user_4
Obsidian | Level 7

Hi,

 

I am writing a when clause saying:

case when put(AB,1.)='1' then XY='0' else XY end as XY

 

AB is numeric and so to put it in the case statement I converted it to char but it is still saying the error "Result of when clause 2 is not the same data type as the preceding results".

XY is character.

 

How can I put the case statement here !

 

Thanks in advance:)

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@new_sas_user_4 wrote:

Hi,

 

I am writing a when clause saying:

case when put(AB,1.)='1' then XY='0' else XY end as XY

 

AB is numeric and so to put it in the case statement I converted it to char but it is still saying the error "Result of when clause 2 is not the same data type as the preceding results".

XY is character.

 

How can I put the case statement here !

 


The expression XY='0' is a Boolean expression, it evaluates as either true or false which in SAS is a numeric one or zero. So you can't have a numeric in one case (when put(AB,1.)='1' then XY='0') and a character XY in the other case.

 

Try this:

case when AB=1 then '0' else XY end as XY

 

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

@new_sas_user_4 wrote:

Hi,

 

I am writing a when clause saying:

case when put(AB,1.)='1' then XY='0' else XY end as XY

 

AB is numeric and so to put it in the case statement I converted it to char but it is still saying the error "Result of when clause 2 is not the same data type as the preceding results".

XY is character.

 

How can I put the case statement here !

 


The expression XY='0' is a Boolean expression, it evaluates as either true or false which in SAS is a numeric one or zero. So you can't have a numeric in one case (when put(AB,1.)='1' then XY='0') and a character XY in the other case.

 

Try this:

case when AB=1 then '0' else XY end as XY

 

--
Paige Miller
new_sas_user_4
Obsidian | Level 7

Thanks a lot 🙂 🙂

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 2 replies
  • 2808 views
  • 0 likes
  • 2 in conversation