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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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