BookmarkSubscribeRSS Feed
sjm500
Calcite | Level 5
I was surprised that to see that the following code will give no errors

DATA X;
set y;

if var1 = "1";
run;

I have run something similar where var1 is numeric and yet SAS compiles and executes the code without error. I thought that you could get an error if var1 was defined to be a numeric variable. Was I wrong about this?
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
You will see a SAS NOTE (no ERROR) in your SASLOG indicating that SAS converted character to numeric or vice versa, depending on the condition presented.

On the other hand, if you attempt to combine SAS data sets which have same-named variables and different types, you will get a SAS error in the DATA step.

Scott Barry
SBBWorks, Inc.
sjm500
Calcite | Level 5
I understand that but how come SAS allows the IF statement to operate normally and give you the correct results even though VAR1 is a numeric type and I am using a string "4" in it to subset the data set. Shouldn't it have detected that VAR1 was numeric and therefore I cannot use the string "4" with VAR1. I found that behavior very strange. Maybe it was there all the time and now I am just noticing.

Message was edited by: sjm500 Message was edited by: sjm500
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Yes, what you are seeing is SAS normal behavior.

Again, the SASLOG NOTE is the only indication, as demonstrated below:

1 data _null_;
2 if 1 = '1' then putlog 'We have a winner!';
3 run;

NOTE: Character values have been converted to numeric values at the places given by:
(Line):(Column).
2:8
We have a winner!
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds



Scott Barry
SBBWorks, Inc.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1155 views
  • 0 likes
  • 2 in conversation