end;
j=1;
do k = 1 to 5;
if indexc(old(k),from(j)) > 0 then do;
new(k)=translate(old(k),to(j),from(j));
j+1;
if j=4 then j=1;
end;
end;
decrypt_num=input(cats(of new1-new5),5.);
keep num encrypt_num decrypt_num;
run;
proc print;
run;
The following output shows the results of the PROC PRINT for Example 3:
Numerical Accuracy in SAS Software
Overview
In any number system, whether it is binary or decimal, there are limitations to how
precise numbers can be represented. As a result, approximations have to be made. For
example, in the decimal number system, the fraction 1/3 cannot be perfectly represented
as a finite decimal value because it contains infinitely repeating digits
(.333...). On
computers, because of finite precision, this number must be approximated. Numerical
precision is the accuracy with which numbers are approximated or represented.
In computing, software applications are particularly susceptible to numerical precision
errors due to finite precision and machine hardware limitations. Computers are finite
machines with finite storage capacity, so they cannot represent an infinite set of numbers
with perfect precision.
The problem is further compounded by the fact that computers use a different number
system than people do. Decimal infinite-precision arithmetic is the norm for human
calculations but computers use finite binary representations of values and finite-
precision arithmetic. This representation has been proven adequate for many
calculations. Yet, depending on the problem, you may need an extended precision that is
wider than what the hardware offers. In that case, representation and arithmetic are done
mostly in software and are relatively much slower than hardware arithmetic.
60 Chapter 4 SAS Variables
Furthermore, although computers do allow the use of decimal numbers and decimal
arithmetic via human-centric software interfaces, all numbers and data are eventually
converted to binary format to be stored and processed by the computer internally. It is in
the conversion between these 2 number systems – decimal to binary – that precision is
affected and rounding errors are introduced.
Truncation in Binary Numbers
Just like there are decimal values with infinitely repeating representations, there are also
binary values that have infinitely repeating representations. However, the numbers that
are imprecise in decimal are not always the same ones that are imprecise in binary.
For example, the decimal value 1/10 has a finite decimal representation (0.1), but in
binary it has an infinitely repeating representation. In binary, the value converts to
0.000110011001100110011 ...
where the pattern 0011 is repeated indefinitely. As a result, the value will be rounded
when stored on a computer.
Performing calculations and comparisons on imprecise numbers in SAS can lead to
unexpected results. Even the simplest calculations can lead to a wrong conclusion.
Hardware cannot always match what might seem obvious and expected in the decimal
system.
For example, in decimal arithmetic, the expression (3 x 0.1) is expected to be equal
to
0.3, so the difference between (3 x 0.1) and (0.3), must be 0. Because the
decimal values 0.1 and 0.3 do not have exact binary representations, this equality does
not hold true in binary arithmetic. If you compute the difference between the two values
in a SAS program, the result is not 0, as Example Code 4.6 on page 61 illustrates.
In the example, SAS sets the variables point_three and
three_times_point_three to 0.3 and (3 x 0.1), respectively. It then compares
the two values by subtracting one from the other and writing the result to the SAS log:
Example Code 4.6 Comparing Imprecise Values in SAS
data a;
point_three=0.3;
three_times_point_one= 3 * 0.1;
difference= point_three - three_times_point_one;
put 'The difference is ' difference;
run;
Output 4.5 Log Output for Comparing Imprecise Values in SAS
The log output shows that (3 x 0.1) — 0.3 does not equal 0, as it does in decimal
arithmetic. This is because the variable "difference" is the result of calculations that are
performed on rounded values, or, infinitely repeating binary values.
Numerical Accuracy in SAS Software 61

Get SAS 9.4 Language Reference, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.