BookmarkSubscribeRSS Feed
PW1
Calcite | Level 5 PW1
Calcite | Level 5

Hi,

While I ran below code, getting value of variable TAG1 as "unchecked" , eventhough values are getting same for TAG and TAG2. Value for TAG1 is not getting correct according to if condition.

 

Any idea why is happening so?

 


Data readin2;
a=6882;
b=9176;
c=68.82;
d=91.76;
format TAG TAG2 12.8;
TAG=a/b;
TAG2=c/d;
length TAG1 $20;
IF TAG = TAG2 THEN TAG1 = "New";
ELSE TAG1 ="Unchecked";
run;

4 REPLIES 4
s_lassen
Meteorite | Level 14

The problem is that SAS numbers are stored as base 2 floating points, meaning that the decimals cannot be represented exactly. This means that the may be a minuscule difference between the calculation with the integers and the decimal values.

 

Try something like:

Data readin2;
a=6882;
b=9176;
c=68.82;
d=91.76;
format TAG TAG2 12.8;
TAG=a/b;
TAG2=c/d;
length TAG1 $20;
IF abs(TAG-TAG2)<1e-10 THEN TAG1 = "New";
ELSE TAG1 ="Unchecked";
run;

 

smantha
Lapis Lazuli | Level 10
Eventhough mathematically they are the same how sas is handling the precision might be different. Round both values to a fixed decimal say six positions and then compare. Format statement is display only. It does not change the original value. Use round function instead

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
  • 4 replies
  • 1223 views
  • 3 likes
  • 5 in conversation