[R] Fwd: Re: Reading xpt files into R

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Sat Apr 14 22:30:00 CEST 2018


> On Apr 14, 2018, at 12:18 PM, WRAY NICHOLAS via R-help <r-help using r-project.org> wrote:
> 
> 
> -------- Original Message ----------
> From: WRAY NICHOLAS <nicholas.wray using ntlworld.com>
> To: peter dalgaard <pdalgd using 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:
> 
>> read.xport("test.xpt")
> 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"

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:

> 
> The "export" or "transfer format from SA is supposed to make reading data less difficult and standardized. This is what a header from the version used by the NHANES releases (that's all one line):
> 
> HEADER RECORD*******LIBRARY HEADER RECORD!!!!!!!000000000000000000000000000000  SAS     SAS     SASLIB  9.2     XP_PRO                        16SEP09:09:39:2516SEP09:09:39:25                                                                HEADER RECORD*******MEMBER  HEADER RECORD!!!!!!!000000000000000001600000000140  HEADER RECORD*******DSCRPTR HEADER RECORD!!!!!!!000000000000000000000000000000  SAS     DEMO    SASDATA 9.2     XP_PRO                        16SEP09:09:39:2516SEP09:09:39:25                                                                HEADER RECORD*******NAMESTR HEADER RECORD!!!!!!!000000014400000000000000000000  SEQN    Respondent sequence number     

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.


> 
> Nick
> 
> 
> 
> On 14 April 2018 at 10:32 peter dalgaard <pdalgd using gmail.com> wrote:
> 
> That's what he tried,

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


> 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).

Peter and I agree agree on that advice.

> 
> -pd
> 
>> 
>>            On 14 Apr 2018, at 00:18 , David Winsemius <dwinsemius using comcast.net> wrote:
>> 
>>            There is a read.xport function in the foreign package and I think most people would have chosen that one as a first attemp. It's part of the standard R distribution. It refers you to https://support.sas.com/techsup/technote/ts140.pdf for details on the format.
> 
-- 

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law




More information about the R-help mailing list