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

This error is so basic I am baffled. Note I changed the names in this example because the data is sensitive and I can't display it. 

 

proc sql;
Create table dora.counselor3
as select a.*
, case when 'first name'n= 'John' and 'last name'n ='Smith' then 'RIMS Staff ID If you use RIMS, y'n = '1111'
else 'RIMS Staff ID If you use RIMS, y'n end as correctid

from dora.counselor2 a;
quit;

 

'RIMS Staff ID If you use RIMS, y'n is a string field

 

I get ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.

 

Which makes no sense. The data I put in is a string value and so is the variable in question.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You're assigning a value to  'RIMS Staff ID If you use RIMS, y'n = '1111'

but end the CASE statement with:

end as correctid

 

I think you're mixing up IF/THEN notation and CASE notation. 

 

I think this is what you're looking for here:

case when 'first name'n= 'John' and 'last name'n ='Smith' then  '1111'
else 'RIMS Staff ID If you use RIMS, y'n 
end as correctid

@noetsi wrote:

This error is so basic I am baffled. Note I changed the names in this example because the data is sensitive and I can't display it. 

 

proc sql;
Create table dora.counselor3
as select a.*
, case when 'first name'n= 'John' and 'last name'n ='Smith' then 'RIMS Staff ID If you use RIMS, y'n = '1111'
else 'RIMS Staff ID If you use RIMS, y'n end as correctid

from dora.counselor2 a;
quit;

 

'RIMS Staff ID If you use RIMS, y'n is a string field

 

I get ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.

 

Which makes no sense. The data I put in is a string value and so is the variable in question.



View solution in original post

4 REPLIES 4
Reeza
Super User

You're assigning a value to  'RIMS Staff ID If you use RIMS, y'n = '1111'

but end the CASE statement with:

end as correctid

 

I think you're mixing up IF/THEN notation and CASE notation. 

 

I think this is what you're looking for here:

case when 'first name'n= 'John' and 'last name'n ='Smith' then  '1111'
else 'RIMS Staff ID If you use RIMS, y'n 
end as correctid

@noetsi wrote:

This error is so basic I am baffled. Note I changed the names in this example because the data is sensitive and I can't display it. 

 

proc sql;
Create table dora.counselor3
as select a.*
, case when 'first name'n= 'John' and 'last name'n ='Smith' then 'RIMS Staff ID If you use RIMS, y'n = '1111'
else 'RIMS Staff ID If you use RIMS, y'n end as correctid

from dora.counselor2 a;
quit;

 

'RIMS Staff ID If you use RIMS, y'n is a string field

 

I get ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.

 

Which makes no sense. The data I put in is a string value and so is the variable in question.



noetsi
Obsidian | Level 7

thank you. As I feared a very basic error. I rarely work with multiple fields in the case statement. Usually I just work with the one I am changing. In fact I fixed this, before I saw the replies, with an insert statement.

ballardw
Super User

Style comment: If your variable names are so sensitive they can't be shared then you might talk to someone about considering simpler variable names that are not sensitive and place the "sensitive" information into a LABEL for SAS variables. Then you can have much more coherent internal report text using the labels and don't try to create such ugly variables, even in dummy code, as 'RIMS Staff ID If you use RIMS, y'n. I choke just thinking about how many times I would screw up typing that sort of variable name. Plus SAS variable labels can be much longer than variable names, up to 256 characters.

 

I strongly suspect that your code should have thrown an error. You should include the complete log of code plus notes, warnings and errors for almost anything. Copy from the log, on the forum open a text box using the </> icon that appears above the message window and paste the text. You can edit the pasted text if you really have something that sensitive.

Tom
Super User Tom
Super User

Is that third variable NUMERIC or CHARACTER?  Your current code it treating it as both.

You have this syntax.

case
  when A='John' and B='Smith' then (C = '1111')
  else C
end as D

So in the first place you use C you are comparing it to a string literal.  So you are treating it as a character variable. The result of the comparison will be a numeric value of either 0 or 1 depending on whether the value of C matches the string. So the new variable D should be numeric.  But in the ELSE clause you are taking the value of C to use as the value for the new numeric variable D.  So is C a character string or a number?

 

I suspect you meant to code this pattern instead.

case
  when A='John' and B='Smith' then '1111'
  else C
end as D

Now the variable C should be a character string to match the string literal result of the first WHEN clause.

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 1046 views
  • 4 likes
  • 4 in conversation