| Return to previous page
|
To generate key field values, use the ordinal="yes" attribute in the COLUMN element in order to create a counter variable. A counter variable keeps track of the number of times the location path, which is specified by the INCREMENT-PATH element, is encountered. The counter variable increments its count by 1 each time the path is matched. (The counter variable is similar to the _N_ automatic variable in DATA step processing in that it counts the number of observations being read into a SAS data set.)
Note: When using a counter variable to create a key field for related data sets, you must specify the same location paths for both TABLE elements; otherwise, the results will not match. Each table must have the same generated key for like-named data elements.
The following XMLMap imports the Pharmacy.XML document as two SAS data sets that have related information and also creates a key field that holds generated numeric key values:
<?xml version="1.0" encoding="UTF-8" ?>
<!-- ############################################################ -->
<!-- 2003-04-15T10:55:43 -->
<!-- SAS XML Libname Engine Map -->
<!-- Generated by XML Mapper, 9.1.10.20030413.1400 -->
<!-- ############################################################ -->
<SXLEMAP version="1.2" name="SXLEMAP">
<!-- ############################################################ -->
<TABLE name="PERSON">
<TABLE-PATH syntax="XPath">/PHARMACY/PERSON</TABLE-PATH> (1)
<COLUMN name="KEY" retain="YES" ordinal="YES"> (2)
<INCREMENT-PATH syntax="XPath">/PHARMACY/PERSON</INCREMENT-PATH>
<TYPE>numeric</TYPE>
<DATATYPE>integer</DATATYPE>
<FORMAT width="3">Z</FORMAT>
</COLUMN>
<COLUMN name="NAME">
<PATH syntax="XPath">/PHARMACY/PERSON/NAME</PATH>
<TYPE>character</TYPE>
<DATATYPE>string</DATATYPE>
<LENGTH>11</LENGTH>
</COLUMN>
<COLUMN name="STREET">
<PATH syntax="XPath">/PHARMACY/PERSON/STREET</PATH>
<TYPE>character</TYPE>
<DATATYPE>string</DATATYPE>
<LENGTH>18</LENGTH>
</COLUMN>
<COLUMN name="CITY">
<PATH syntax="XPath">/PHARMACY/PERSON/CITY</PATH>
<TYPE>character</TYPE>
<DATATYPE>string</DATATYPE>
<LENGTH>6</LENGTH>
</COLUMN>
</TABLE>
<!-- ############################################################ -->
<TABLE name="PRESCRIPTION">
<TABLE-PATH syntax="XPath">/PHARMACY/PERSON/PRESCRIPTION</TABLE-PATH> (3)
<COLUMN name="KEY" retain="YES" ordinal="YES"> (4)
<INCREMENT-PATH syntax="XPath">/PHARMACY/PERSON</INCREMENT-PATH>
<TYPE>numeric</TYPE>
<DATATYPE>integer</DATATYPE>
<FORMAT width="3">Z</FORMAT>
</COLUMN>
<COLUMN name="NUMBER">
<PATH syntax="XPath">/PHARMACY/PERSON/PRESCRIPTION/NUMBER</PATH>
<TYPE>numeric</TYPE>
<DATATYPE>integer</DATATYPE>
</COLUMN>
<COLUMN name="DRUG">
<PATH syntax="XPath">/PHARMACY/PERSON/PRESCRIPTION/DRUG</PATH>
<TYPE>character</TYPE>
<DATATYPE>string</DATATYPE>
<LENGTH>12</LENGTH>
</COLUMN>
</TABLE>
</SXLEMAP>
The following explains the XMLMap syntax that generates the key fields:
This is the process that the XML engine follows in order to generate the key field values for the Prescription data set:
Note: Because the increment paths for the counter variables must be the same for both TABLE elements, the behavior of the XML engine for the Prescription table Key variable is the same as the Person table Key variable. While the XML engine tracks the occurrence of a PERSON tag as a key for both counter variables, the observations are derived from different TABLE-PATH locations.
filename pharm 'c:\My Documents\XML\Pharmacy.xml'; filename map 'c:\My Documents\XML\PharmacyOrdinal.map'; libname pharm xml xmlmap=map;Here is PROC PRINT output for both of the imported SAS data sets with a numeric key:
PROC PRINT Output for PHARM.PERSON
The SAS System 1
Obs KEY NAME STREET CITY
1 001 Brad Martin 11900 Glenda Court Austin
2 002 Jim Spano 1611 Glengreen Austin
PROC PRINT Output for PHARM.PRESCRIPTION
The SAS System 2
Obs KEY NUMBER DRUG
1 001 1234 Tetracycline
2 001 1245 Lomotil
3 002 1268 Nexium