Using the XMLDouble Option for XML LIBNAME Engine in SAS 8.2
Contents
In SAS, numeric variables store values in floating-point format.
Rarely though do you display numeric values as they are stored.
Usually, a numeric variable has an assigned SAS format, which
controls the written appearance of the values, making them
more readable.
For example, if
the stored value is 12345.1234 and the
SAS format
best8.2 is assigned to the variable, SAS
displays the value as 12345.12.
When written, the SAS format reduces the number of digits.
When a numeric variable has an assigned SAS format,
the default behavior of the XML LIBNAME engine is that the format
controls the numeric values that are imported or exported.
For example, using the stored value and SAS format example above,
if you exported the value to an XML document, by default, the XML
element would contain the truncated value 12345.12, not the stored
raw value.
To retain precision, which is the number of digits used to
express the fractional part of a number, you can use the enhanced
SAS XML LIBNAME engine, which is available as an add on to Release 8.2.
The enhanced engine provides additional syntax in order to determine
the precision of a numeric value. This document describes
the new XMLDouble option.
XMLDouble is an option that you specify on the LIBNAME XML statement.
XMLDouble determines the precision of a numeric value by specifying
whether you want the value to be controlled by an assigned SAS format
or whether you want the stored raw value.
The syntax for the option is:
XMLDOUBLE=FORMAT | PRECISION
- FORMAT
- is the default behavior:
-
When exporting, the engine uses the assigned SAS format to control the values
for a numeric variable. Note that an assigned SAS format could reduce
the number of digits for a numeric value in the output.
-
When importing, the engine retrieves parsed character data from the
named element.
- PRECISION
- retains the precision of numeric values:
-
When exporting, the engine generates an attribute-value pair
(of the form RAWVALUE="value"). SAS
uses the base64 encoding of the stored machine representation.
(The base64 encoding method converts binary data into ASCII text
and vice versa and is similar to the MIME format.)
-
When importing, the engine retrieves the value from the RAWVALUE attribute
in the element, ignoring
the parsed character data content of the element.
Typically, you would use XMLDOUBLE=PRECISION to
import an XML document when data content is more
important than readability.
The following LIBNAME XML statement
includes the XMLDouble option:
libname prec xml 'C:\My Documents\myfiles\numbers.xml' xmldouble=precision;
data prec.numbers;
set numbers;
run;
Example 1: Exporting Numeric Values
This example uses a small SAS data set, with a numeric variable that
contains values with a high precision. The following
SAS program creates the data set with an assigned
user-defined format, then exports two XML documents to show the difference
in output:
libname format xml 'C:\My Documents\format.xml'; (1)
libname prec xml 'C:\My Documents\precision.xml' xmldouble=precision; (2)
data npi; (3)
do n=1 to 10;
n_pi = n*3.141592653589793;
output;
end;
format n_pi f14.2;
run;
data format.dbltest; (4)
set npi;
run;
data prec.rawtest; (5)
set npi;
run;
title 'Drops the Precision'; (6)
proc print data=format.dbltest;
format n_pi f14.10;
run;
title 'Keeps the Precision'; (7)
proc print data=prec.rawtest;
format n_pi f14.10;
run;
-
First XML LIBNAME statement assigns the libref FORMAT to
the file that will store the
generated XML document FORMAT.XML. The default behavior for
the engine is that an assigned SAS format
controls numeric values.
-
Second XML LIBNAME statement assigns the libref PREC to
the file that will store the
generated XML document PRECISION.XML. The XMLDouble option specifies
PRECISION, which causes the engine to retrieve the
stored raw values.
-
DATA step creates the temporary data set NPI. The data set has
a numeric variable that contains values with a high precision. The
variable has an assigned user-defined format that specifies
two decimal points. Here's a portion of PROC CONTENTS output for
WORK.NPI.
-
DATA step creates the data set FORMAT.DBLTEST from WORK.NPI.
-
DATA step creates the data set PREC.RAWTEST from WORK.NPI.
-
From the data set FORMAT.DBLTEST,
PROC PRINT generates the XML document
FORMAT.XML,
which contains numeric values controlled by the SAS format.
For the PRINT procedure output,
a format was specified in order to show the
precision loss. In the output, the decimals after the second digit are zeros.
Here's the procedure output:
FORMAT.DBLTEST.
-
From the data set PREC.RAWTEST,
PROC PRINT generates the XML document
PRECISION.XML,
which contains the stored numeric values.
For the PRINT procedure output,
a format was specified in order to show the
retained precision.
Here's the procedure output:
PREC.RAWTEST.
Example 2: Importing Numeric Values
This example imports the XML document
PRECISION.XML to illustrate
how you can change the behavior for importing numeric values.
The first SAS program imports the XML document using
the default behavior of the XML LIBNAME engine, which
retrieves parsed character data from the element:
libname default xml 'C:\My Documents\precision.xml';
title 'Default Method';
proc print data=default.rawtest;
format n_pi f14.10;
run;
The result of the import is
DEFAULT.RAWTEST.
The second SAS program imports the XML document using
the XMLDouble option to change the behavior of the XML LIBNAME engine,
which retrieves the value from the RAWDATA attribute in the element:
libname new xml 'C:\My Documents\precision.xml' xmldouble=precision;
title 'Precision Method';
proc print data=new.rawtest;
format n_pi f14.10;
run;
The result of the import is
NEW.RAWTEST.
Questions or comments? Send electronic mail to
XMLEngine@sas.com with your comments.