Hi All,
I've to read two variables of different format from a file and compare them, I read variables keeping them in the same dataset as ,
DATA INDATA ;
INFILE '.....................';
INPUT
@001 VAR1 13.2
@025 VAR2 18.2
;
IF VAR1 NE VAR2 THEN DO;
...........................
...........................
END;
RUN;
Here the values of VAR1 and VAR2 are same, say 45869.32 I would expect that the statements within in the IF should not execute, but its not so. Which I thought is due to the format mismatch, so I tried formatting the variables with same format , but the IF still executes.
Anyone have any idea on whats happening ?
Thanks!!
What are the statements that are executing?
Does the following work as expected for you?
DATA INDATA ;
INPUT
@001 VAR1 13.2
@025 VAR2 18.2
;
IF VAR1 NE VAR2 THEN DO;
x=0;
END;
else x=1;
cards;
45869.32 45869.32
45869.32 45869.33
45869.34 45869.34
;
My guess is that you are trying to use an if then statement that conflicts with how SAS functions, namely what occurs during the compilation and what occurs during the execution phases. Take a look at: http://analytics.ncsu.edu/sesug/2010/CC09.Whitlock.pdf
Question: How do you tell if they are equal? Are they equal in the raw data or you think they are equal in the SAS table? if the former, then why you read them in different informat? Will the following example mimic your scenario:
data _null_;
input var1 4.2 @7 var2 5.2;
if var1=var2 then put "equal"; else put "not equal";
cards;
1.232 1.232
run;
Haikuo
comparing decimal fractions like 1.234 and 2.4 create difficulties because SAS stores these decimal fractions as log values in a way that could be called "imprecise most of the time". When the values are really integers like 2 rather than nearly an integer like 1.99999999999999999999 there is probably no comparison problem.
To ensure you get the two-decimal place equality between 1.229 and 1.2345 just convert to integers or strings, like
if round( var1 * 100 ) = round( var2 * 100 ) then /*equal */ ;
or
if put( var1, 20.2) = put( var2, 20.2) then /*'equal */ ;
peterC
Thank You Peter , It worked ....
I did this
DATA INDATA ;
INFILE '.....................';
INPUT
@001 VAR1 13.2
@025 VAR2 18.2
;
VAR1 = ROUND(VAR1 *100);
VAR2 = ROUND(VAR2 *100);
IF VAR1 NE VAR2 THEN DO;
...........................
...........................
END;
RUN;
The IF condition failed as expected,this is how the VAR1 and VAR2 appears in the file
VAR1 : 0000012345.67
VAR2 : 000000000012345.67
If you use that solution, you will be accepting a different sort of incorrect result. These values will be found to be equal:
1.211
1.214
Does your incoming data always contain a decimal point for both variables? If so, will there always be two digits after the decimal point?
Yes, the incoming the data will always be a decimal value with two digits after the decimal Point.So I thought the idea of Multiplying by 100 makes sense.
OK, if the data always contains the decimal point plus two digits after, you'll be fine.
I suspect that your INPUT statement is to blame. From the data you posted it looks like VAR1 has a leading space so that by using 13.2 format you are not reading the 7 in the hundredths place that is in column 14. Using INFORMATs for reading numbers is normally not needed. In particular the only difference in result from using 13.2 instead of 18.2 is just the number of characters read from the input file. Do you need to use that style of input statement?
Why not re-write it using column input?
input var1 1-14 var2 25-42 ...
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.