Hello,
I have a table named 'role' with several columns. I want to change the value of one of its columns based on this scenario:
If CD_ROLE is empty, then put SOURCE in CD_ROLE and put NU_ROLE in NU_CLI
if CD_ROLE is not empty then do nothing
Formats:
CD_ROLE : $2.
NU_ROLE: $20.
NU_CLI: $20.
SOURCE: $4.
What I tried:
PROC SQL; CREATE TABLE r2; SELECT r.*, CASE WHEN CD_ROLE = ' ' THEN CD_ROLE = r.SOURCE WHEN CD_ROLE = ' ' THEN NU_CLI = r.NU_ROLE ELSE CD_ROLE END AS CD_ROLE length = 20 FROM role r; quit;
I am getting error:
Result of WHEN clause 3 is not the same data type as the preceding results.
Please help 😞
@MILKYLOVE wrote:
I am getting error:
Result of WHEN clause 3 is not the same data type as the preceding results.
This happens because somewhere, you are trying to assign a numeric variable to a character variable, or the other way around. So, what are the variable types (numeric or character) of the variables in use in this problem?
From now on, when you get an error, show us the ENTIRE log for this PROC or DATA step. Do not show us the error message detached from the rest of the log for this PROC or DATA step.
All the variables are of 'character' data type. That's why I am confused as to why I am getting this error message.
Maybe I have wrongly written the code as well?
Please show us the ENTIRE log for this PROC SQL. Copy the log as text, and paste it into the window that appears when you click on the </> icon.
ONE case statement assigns ONE value to ONE variable.
So your code is apparently trying to violate that.
CASE WHEN CD_ROLE = ' ' THEN r.SOURCE ELSE CD_ROLE END AS CD_ROLE length = 20
and a second case for the
CASE WHEN CD_ROLE = ' ' THEN r.NU_ROLE
else ' ' /* guessing as you did not provide an ELSE rule for NU_CLI*/ END AS NU_CLI length = 20
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!
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.
Ready to level-up your skills? Choose your own adventure.