DATA Step, Macro, Functions and more

Data STEP "IF"

Reply
New Contributor
Posts: 2

Data STEP "IF"

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?
Super Contributor
Super Contributor
Posts: 3,174

Re: Data STEP "IF"

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.
New Contributor
Posts: 2

Re: Data STEP "IF"

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Data STEP "IF"

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)Smiley SadColumn).
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.
Ask a Question
Discussion stats
  • 3 replies
  • 104 views
  • 0 likes
  • 2 in conversation