1
votes

I want to write a XML-File by reading a SAS Data Set and using SAS's own XMLV2 XMLMaps with a numeric format.

In the code example below i tried the "Z3"-Format (add leading zeros to your integer). But other numeric formats like "12.2" (length 12 with 2 decimal places) also don't work.

Applying a format in SAS XMLV2 XMLMaps is usually done with the

<FORMAT>

tags in the COLUMN definition. This works fine for date formats, but for numeric formats it doesn't seem to - or i'm forgetting something here..

/*Minimal SAS Code Example:*/

filename xmlmap temp;
filename xmlout temp;

/*Define SAS XMLV2 XMLMap:*/
data _null_;
   file xmlmap encoding='UTF-8';
   infile datalines4;
   input;
   put _infile_;
datalines4;
<?xml version="1.0" encoding="UTF-8"?>
<SXLEMAP name="TEST" version="2.1">

   <OUTPUT>
      <TABLEREF name="TEST" />
   </OUTPUT>

   <NAMESPACES count="0"/>

   <TABLE name="TEST">
      <TABLE-PATH syntax="XPath">/Root</TABLE-PATH>

      <COLUMN name="ORIGINAL">
         <PATH syntax="XPath">/Root/ORIGINAL</PATH>
         <TYPE>numeric</TYPE>
         <DATATYPE>integer</DATATYPE>
      </COLUMN>

      <COLUMN name="FORMATED">
         <PATH syntax="XPath">/Root/FORMATED</PATH>
         <TYPE>numeric</TYPE>
         <DATATYPE>integer</DATATYPE>
         <FORMAT width="3" ndec="0">Z3</FORMAT>
      </COLUMN>

   </TABLE>

</SXLEMAP>
;;;;

/*Define Input Data Set:*/
data WORK.TEST;
   infile datalines;
   input
      ORIGINAL :8.
      FORMATED :8.
      ;
   format FORMATED Z3.0;
datalines;
4 4
10 10
;
run;

/*Link XMLMap with the Input Data Set and Output XML-File*/
libname XMLTEST
   xmlv2
   xmlfileref=xmlout
   xmltype=xmlmap
   xmlmap=xmlmap
   xmlencoding='UTF-8'
   ;

/*Write Data Set to XML-File:*/
data XMLTEST.TEST;
   set WORK.TEST;
run;

/*Print XML-File to Log:*/
data _null_;
   infile xmlout;
   input;
   put _infile_;
run;

filename xmlmap clear;
filename xmlout clear;

Expected Result (XML-File) - here with leading zeros in the Node FORMATED:

 <Root>
 <ORIGINAL>4</ORIGINAL>
 <FORMATED>004</FORMATED>
 </Root>
 <Root>
 <ORIGINAL>10</ORIGINAL>
 <FORMATED>010</FORMATED>
 </Root>

Actual Result (XML-File) - no leading zeros in the Node FORMATED:

 <Root>
 <ORIGINAL>4</ORIGINAL>
 <FORMATED>4</FORMATED>
 </Root>
 <Root>
 <ORIGINAL>10</ORIGINAL>
 <FORMATED>10</FORMATED>
 </Root>
1

1 Answers

0
votes

The SXLEMAP Column element defines how the incoming xml gets mapped into appropriate SAS data set column attributes.

The DATATYPE element tells the engine to output an integer value in the xml output -- and that will be unformatted. The map informs the xml consumer on how it should deal with the parsed data value.

If you re-read the xml you created

data in;
  set xmltest.test;
run;

you will see the map has an incorrect element value Z3 for format. The value should be just Z, and the modifiers to the format come in from the <FORMAT> width and ndec attributes.

I'm not sure of the way, in a single xml file, to preface the xmldata nodes with the data's SAS metadata.