Skip to content

Reading xpt files into R

4 messages · Nick Wray, William Dunlap, David Winsemius

#
-------- Original Message ----------
From: WRAY NICHOLAS <nicholas.wray at ntlworld.com>
To: peter dalgaard <pdalgd at gmail.com>
Date: 14 April 2018 at 20:18
Subject: Re: [R] Reading xpt files into R


Well yesterday I'd downloaded the "foreign" package and tried to open the xpt file using that:

library(foreign)
read.xport("test.xpt")

I got the following error and warning messages:
Error in read.xport("test.xpt") : 
The specified file does not start with a SAS xport file header!
In addition: Warning message:
In readBin(file, what = character(0), n = 1, size = nchar(xport.file.header,  :
null terminator not found: breaking string at 10000 bytes

I can open the xpt using wordpad and there is a header but it seems to be just text.  I really don't know what constitutes an "
SAS xport file header"

Nick
On 14 April 2018 at 10:32 peter dalgaard <pdalgd at gmail.com> wrote:
That's what he tried, but the bottom line is that just because something is called foo.xpt there is no guarantee that it actually is a SAS XPORT file. Firefox plugins use the same extension but it could really be anything - naming conventions are just that: conventions.

So dig deeper and find out what the file really is (or was supposed to be).

-pd
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
#
Does read.xport read both version 5 and version 8 xpt files?  This link to
the Library of Congress can get you started on how to interpret the
header.  (It states that Version 8 was introduced in 2012 but was not in
wide use as of early 2017.)

https://www.loc.gov/preservation/digital/formats/fdd/fdd000464.shtml

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, Apr 14, 2018 at 12:18 PM, WRAY NICHOLAS via R-help <
r-help at r-project.org> wrote:

            

  
  
#
I'm not sure why Peter deleted my copy of a sample of a SAS xport header that I took from an NHANES data distribution. He seemed to think I was confused about the function you had been using. The reason I mentioned that `read.xport` was from the 'foreign' package is that one generally loads that package to make the function available, while it appears you were using a different package, SASxport, and I didn't know whether that package had a function which had the same name as the one from pkg-foreign, and if it did whether it might depend on the read.xport function in foreign. You should not need to download the 'foreign' package, since it ships with every distribution of R. These are the arguments accepted by that function:

SASxport::read.xport
function (file, force.integer = TRUE, formats = NULL, name.chars = NULL, 
    names.tolower = FALSE, keep = NULL, drop = NULL, as.is = 0.95, 
    verbose = FALSE, as.list = FALSE, include.formats = FALSE) 


 When I look at the SASxport::read.xport function code, it is in fact, _not_ the same function. But it does have the R statement about what it thinks qualifies as a SAS xprot file:

xport.file.header <- "HEADER RECORD*******LIBRARY HEADER RECORD!!!!!!!000000000000000000000000000000  "

It checks to see whether the file starts with that string.

This is what appeared in my first message:
So the header is text, but it is text with a particular structure. If your file doesn't have that structure, then it's not a SAS xport file. The .xpt extension is also used for Mozilla Firefox plugins.
Actually not, Peter. Wray was using a function of the same name, but not from pkg-foreign. Perhaps he was following the tutorial at:

http://www.phusewiki.org/wiki/index.php?title=Open_XPT_File_with_R
Peter and I agree agree on that advice.
#
_not_ the
"
Version 8 SAS xport files have the header
  HEADER RECORD*******LIBV8 HEADER RECORD!!!!!!!000000000000000000000
000000000
It is easy to check for that in your text editor or in R.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, Apr 14, 2018 at 1:30 PM, David Winsemius <dwinsemius at comcast.net>
wrote: