BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Within proc SQL, is it possible to select a numeric column, then check it against character values without an error? I've tried converting it to character but can't seem to get the syntax correct.

If the variable mcc in table x is numeric, the following yeilds an error since mcc is numeric and the where statement contains character values:

proc sql;
create table a as select mcc
from x
where mcc in ('1234','5678');
quit;

Is there a way to convert mcc to character so the where statement works? In case you are wondering, changing the where to values to numeric is not an option.

Thanks
2 REPLIES 2
Cynthia_sas
SAS Super FREQ
Hi:
You should be seeing something like this ERROR message in the log:
[pre]

ERROR: Expression using IN has components that are of different
data types.

[/pre]

But, for purposes of WHERE, you can use the PUT function to convert the compared value to character for the duration of the comparison. See the program log below.

cynthia
[pre]
9331 ods listing;
9332 proc sql;
9333 title 'bad sql where';
9334 select *
9335 from sashelp.class
9336 where age in ('11','12','13')
9337 order by age;
ERROR: Expression using IN has components that are of different
data types.
NOTE: The IN referred to may have been transformed from an OR
to an IN at some point during PROC SQL WHERE clause
optimization.
9338 quit;
NOTE: The SAS System stopped processing this step because of
errors.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

9339
9340


9341 proc sql;
9342 title 'good sql where';
9343 select *
9344 from sashelp.class
9345 where put(age,2.0) in ('11','12','13')
9346 order by age;
9347 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.10 seconds
cpu time 0.01 seconds

[/pre]
deleted_user
Not applicable
You are exactly correct and your solution works. Thank you very much.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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