Starting with Stata version 8.2, it is possible to save and use SAS XPORT (.xpt) file directly from within Stata. If your Stata 8 is not fully up to date, then see Installing, Customizing, Updating Stata to make sure your copy of Stata is fully up to date. Reading and writing SAS XPORT files can be useful for creating data files for submission to the FDA, as well as assisting in transferring data between other statistical packages, such as SAS.
Creating .xpt files
Let’s use a file called hsb2 from our web site and use that as an example. Note that this data file contains value labels.
Below we create hsb2.xpt (and it also creates a file with formats called formats.xpf). See the help for fdasave for information about how to use the .xpf file.use https://stats.idre.ucla.edu/stat/stata/notes/hsb2, clear (highschool and beyond (200 cases))
fdasave hsb2
Instead, we could write a file with SAS code for making the value labels like this. This time we save the file as hsb2a.
fdasave hsb2a, vallabfile(sascode)
and instead Stata creates hsb2.xpt and hsb2.sas (which has the code for creating the SAS formats associated with the value labels).
Reading the .xpt file in SAS, Example 1
Here is an example of how to read the file hsb2 that we created. In this example, we will force SAS to ignore the formats (value labels) using the OPTIONS NOFTMERR; option.
OPTIONS NOFMTERR; libname in XPORT "c:datahsb2.xpt"; proc contents data=in.hsb2; run;
In the example above, we use the libname statement to point to the file c:datahsb2.xpt and then use proc contents to have a look at the file assuming all looks good, then we can run the next couple of steps to make a copy of the file as a regular SAS data file.
data "c:datahsb2" set in.hsb2; run; proc contents data="c:datahsb2"; run; proc means data="c:datahsb2"; run; proc print data="c:datahsb2"(obs=10); run;
In the example above we use the data step to make a regular SAS data file called c:datahsb2.sas7bdat and then use proc contents, proc means, and proc print to inspect that copy of the file. It is advisable to compare these results to a decribe, summarize and list in 1/10 from the original Stata file.
Reading the .xpt file in SAS, Example 2
Here is an example of how to read the file hsb2a that we created above, where we also included the vallabfile(sascode) option. In this example, we read the SAS program that wrote out the format file (which has the Stata value labels).
%include "c:datahsb2a.sas"; libname in XPORT "c:datahsb2a.xpt"; proc contents data=in.hsb2a; run;
In the example above, we use the %include statement to read in the file with the SAS formats (Stata value labels) and then use the libname statement to point to the file c:datahsb2a.xpt and then use proc contents to have a look at the file. Assuming all looks good, then we can run the next couple of steps to make a copy of the file as a regular SAS data file.
data "c:datahsb2"; set in.hsb2; run; proc contents data="c:datahsb2"; run; proc means data="c:datahsb2"; run; proc print data="c:datahsb2a"(obs=10); run;
In the example above we use the data step to make a regular SAS data file called c:datahsb2a.sas7bdat and then use proc contents, proc means, and proc print to inspect that copy of the file. It is advisable to compare these results to a decribe, summarize and list in 1/10 from the original Stata file. To use c:datahsb2a.sas7bdat in the future, say to get a proc means for the data, you would type.
%include "c:datahsb2a.sas"; proc means data="c:datahsb2"; run;
Reading the .xpt file in SPSS
Reading the .xpt file in SPSS is very easy. You simply select File then Open and then for Files of Type select SAS Transport file (*.xpt) and then point to, for example, c:datahsb2.xpt and it will read that file. However, you will not get the value labels.