SAS How To's

How to Easily Create an XML File in SAS

In this article, we discuss how to create an XML file in SAS.

An XML (Extensible Markup Language) file is used to structure data for storage and transport. It consists of tags and text. The tags give structure to the file, while the text contains the actual data.

One of the main advantages of XML files is that it is encoded in plain text. Therefore, you can open them in any text editor. For example, Notepad++ or Sublime Text. But, how do you generate an XML file?

In SAS, the easiest way to create an XML file from a dataset is with the LIBNAME statement. When you write a dataset to the location of the LIBNAME statement, SAS automatically converts the data into an XML file. Alternatively, you can use the ODS statement to export a SAS dataset as an XML file.

In this article, we show both methods and discuss their (dis)advantages.

How to Create an XML File in SAS

For the examples hereafter, we will use the well-known CARS dataset from the SASHELP library. This dataset contains information about vehicles stored in 144 rows and 15 columns. We will export this dataset as an XML file.

This image shows the first 10 rows of the CARS dataset.

SAS CARS Dataset

As you can see, the data contains numeric and character columns. Some of the numeric variables have a format. For example, the column Invoice.

Create an XML File with the LIBNAME Statement in SAS

The first option to convert a SAS dataset into an XML file is by using the LIBNAME statement.

The LIBNAME statement tells SAS to create a library, that is to say, a connection between the SAS environment and a physical location on your PC or server. With a library, you can read and write data from and to SAS. Because SAS can work with various types of data, different types of libraries exist (i.e., engines). For example, the XLSX library or the SPSS library.

To export a SAS dataset as an XML file, you use the LIBNAME statement and the XML engine. This statement creates a library and defines the location and name of the XML file. When you write a dataset to this library, SAS converts the dataset automatically into an XML file.

The LIBNAME statement starts with the LIBNAME keyword, followed by the name of the library, the XML keyword, and the location and name of the XML file. (For more information, please read this article.)

The name of the library must comply with the SAS naming convention. In other words, it has a maximum length of 8 characters, must start with an English letter or underscore, and does not contain blanks.

The last part of the LIBNAME statement defines the location and name of the file. You write the path between (double) quotes and the file name contains the .XML extension.

The SAS code below shows an example of how to convert a SAS dataset into an XML file using the LIBNAME statement.

libname xml_file xml "/my_files/cars.xml";
data xml_file.cars;
    set sashelp.cars;
run;

First, we created a SAS library called XML_FILE and defined the location and name of the XML file (“/my_files/cars.xml”). Then, we used a SAS Data Step to read the CARS dataset from the SASHELP library and write it to the XML_FILE library. By writing the dataset to a library, SAS converts the data automatically into an XML file. For example, the file cars.xml.

The image below shows the first rows of the cars.xml file.

Create an XML File with SAS using the LIBNAME statement

We can notice a few things about the XML file that SAS has generated:

  • The file starts with the XML version and encoding.
  • The root (i.e., first) element of an XML file created by SAS is always the <TABLE> tag.
  • Each row of the dataset has its own tag. In this case, the <CARS> tag.
  • The columns and data are stored between separated tags. For example, <MAKE>.

As you can see, the XML file does not contain formats, variable labels, and lengths. For example, the column invoice had a dollar-format in the CARS dataset but is unformatted in the XML file.

The unformatted values do come not as a surprise as the SAS log contains an explicit note.

Export Dataset as XML file

If you want the XML file to have formats, then you need to convert the numeric values into character values first. For example, the SAS code below reads the column invoice and creates a new column invoice_fmt that contains the original value and the format. However, the new column is of type character instead of numeric.

libname xml_file xml "/my_files/cars.xml";
data xml_file.cars;
    set sashelp.cars;
 
    invoice_fmt = put(invoice, dollar12.2);
run;

Create an XML File with the ODS Statement in SAS

The second method to create XML files in SAS is by using the ODS statement.

ODS stands for Output Delivery System and formats reports nicely in a new file. Its main advantage is that ODS supports many types of documents that can be shared easily between different systems. For example, it can create HTML pages and generate PDF files.

You can also create an XML file from a SAS report with the ODS statement. The ODS statement creates an XML file of all SAS output that is generated between the ODS start and ODS close statement.

The ODS statement starts with the ODS keyword and the XML keyword. The latter keyword lets SAS know which type of document to create. The last part of the ODS statement is the FILE=-option. With this option, you define the location and name of the output file.

The example below demonstrates how to create an XML file of a SAS report.

ods xml file="/my_files/cars.xml";
proc print noobs data=sashelp.cars;
run;
ods xml close;

In this particular example, we use the PROC PRINT statement to generate a report of the CARS dataset. Because the PROC PRINT statement is between two ODS statements, SAS also converts the report into an XML file.

Notice that SAS needs the ODS XML CLOSE statement. If you omit this statement, SAS won’t create the XML file.

This image shows a part of the file we have created.

Create an XML File with SAS using the ODS statement

As you can see, the structure of the XML file generated by the ODS statement is completely different than the file the LIBNAME statement creates. As a result, the ODS statement is more focused on formatting and presenting data, whereas the LIBNAME statement is more used to create data that can be transferred easily between systems.

One thought on “How to Easily Create an XML File in SAS

Comments are closed.