---++ Using SAS [[%ATTACHURL%/../Hmisc/exportlib.sas][ =exportlib= ]] Macro with R [[http://biostat.mc.vanderbilt.edu/s/Hmisc/html/sasxport.get.html =sasxport.get= ]] Function ---++ Using Stat/Transfer You can use Stat/Transfer to convert any SAS dataset to Stata (probably works best - allows for long variable names and does not put blanks at end of variable labels; import into R using =stata.get=) or SPSS (use =spss.get= in R). Stat/Transfer is available on Linux and Windows computers. It is installed on the computer in the big conference room. Can export directly to R also. Some tips on getting the attributes on the data frame and running stat transfer in batch mode are [[JrSAStoR][here]]. ---++ Using the SAS Viewer The SAS Viewer can read =sas7bdat= files and export the dataset into a =csv= file. See http://biostat.mc.vanderbilt.edu/twiki/bin/view/Restricted/RestrictedSoftware for more information about getting the viewer and using it under Linux. It may be possible to use the SAS viewer to export SAS metadata along with the data and to use both of these files in importing SAS data into R. ---++ If You Have Access to SAS ---+++ Creating a SAS Data Library Suitable for Importing into R <verbatim> LIBNAME d engine "directoryname"; * Use LIBNAME d SASV5XPT "foo.xpt"; to create transport files; DATA d.dataset1; .... DATA d.dataset2; ... /* If the datasets are already created but you only want to export a subset of them do something like the following instead */ LIBNAME old "olddirectoryname"; PROC COPY IN=old OUT=work; SELECT data1 data2 data3; RUN; PROC FORMAT CNTLOUT=d.formats;RUN; * If used any PROC FORMAT ...; VALUE ...; * Can use CNTLOUT=formats if writing to work area; * work can be the first argument given to the macro; </verbatim> *Note*: If you used a newer version of SAS to create the dataset and used variable names longer than 8 characters, you'll need to specify the following option to SAS to allow truncation of names to 8 characters for creating a version 5 export file: =OPTIONS VALIDVARNAME=V6;= ---+++ Running SAS Job to Create =csv= Files <verbatim> %INCLUDE "foo\exportlib.sas"; * Define macro; LIBNAME d ...; * E.g. LIBNAME d SASV5XPT "foo.xpt"; /* To use regular SAS datasets (non-transport files) use for example LIBNAME d "olddirectory"; or LIBNAME d "."; (current working directory);*/ %exportlib(d, outdir, tempdir); * Default outdir is . (current working directory); * Default tempdir is C:/WINDOWS/TEMP; </verbatim> This creates a =.csv= file in =tempdir= for every SAS dataset in =d= (including the =PROC FORMAT= output if any) plus a file called =_contents_.csv= containing =PROC CONTENTS= output for all datasets combined. =_contents_.csv= allows the SAS data import to know about variable labels, formats, and types (including date, time, date/time variables). Under Windows, this SAS job will run much faster if you store the SAS commands in a file such as =exportsas.sas= and you left click on =exportsas.sas= then click on BATCH SUBMISSION. After the job finishes you will see file =exportsas.log= in the same directory as =exportsas.sas=. The only error messages you should see are related to missing formats - ignore these. Here are simple examples in which a single SAS dataset is exported to directory =C:my/sascsv= and there are no PROC FORMAT value labels. First consider the case where the dataset is the only dataset in the permanent data library. <verbatim> LIBNAME d "."; * SAS datasets are in current working directory; %exportlib(d, C:my/sascsv); </verbatim> If the permanent data library has more than one SAS dataset but you only want to export one of them, say ds1, use for example <verbatim> LIBNAME d "projects/mydatasets"; * SAS datasets are somewhere else; DATA ds1; SET d.ds1; RUN; %exportlib(work, C:my/sascsv); </verbatim> ---+++ Importing Data into R <verbatim> d <- sasxport.get(file, method='csv') # file is name of directory containing all the .csv files created by exportlib </verbatim> This will produce a single data frame =d= if only one =.csv= file existed, or a list of data frames whose major elements are named by lower case versions of all the SAS datasets, with underscores replaced by periods. ----- See also http://www.oview.co.uk/dsread and JrSAStoR
This topic: Main
>
WebHome
>
StatComp
>
RS
>
Hmisc
>
SASexportHowto
Topic revision: revision 13 (raw view)
Copyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki?
Send feedback