Warning: Creating default object from empty value in /home3/science4/public_html/rconvert.com/Convert_SAS_To_R_Statistical_Data/wp-content/themes/optimize/functions/admin-hooks.php on line 160

SAS to R: Logging & Auditing in R Video

Video that compares SAS and R logging and demonstrates how we’ve creating logging and auditing capabilities in R.

 

1 Comment

SAS to R

Exploration of a common pitfall in SAS to R conversions.

 

 

Downloadable PDF: SAS_to_R_Common_Pitfalls

0 Comments

Reading data from URL – SAS vs R

Have you ever wanted to read data directly from a web site without needing to download it to an intermediate file for import.  Below are approaches that may be used to read data directly from a URL.

R

handle <- url("http://datasite.com")
dta <- readLines(handle)

SAS

filename handle url 'http://datasite.com';
proc import datafile=handle out=dta;
run;
0 Comments

5 Ways to Convert SAS Data to R

The process of importing SAS datasets into R is fairly simple.  As with most R and SAS tasks, there are multiple approaches to achieving the outcome, and this list is by no means exhaustive.  Below we outline five methods to transfer data between SAS and R.

Option 1: The automatic approach using read.ssd()

By entering the data directory, file to import, and SAS executable into the R read.ssd function, import can be accomplished automatically through R.

library(foreign)
tbl <- read.ssd("C:/data", "datafile",
          sascmd="C:/Program Files/SAS Institute/SAS/V9/sas.exe")

Option 2: The automatic approach using sas.get()

The Hmisc package has a similar function called sas.get.  This function also uses SAS in the background to help convert data.  We’ve found sas.get to be a bit more tricky to use versus the other methods since it requires that the SAS executable be accessible by entering “sas” on the command line.

library(Hmisc)
tbl <- sas.get("C:/data", "datafile")

Option 3: The transport-format approach using read.xport

Similar to transferring any SAS dataset between machines, this approach requires that the SAS datasets should be converted into transport format from within SAS.  Next, a simple R command is used to import the SAS files.

library(foreign)
tbl <- read.xport("sasfile.xport")

Option 4: The XLS approach

Since MS Excel files are so common for small data storage, using them as an intermediate file format for data transfer may be a reasonable approach.  Here is how you would import an xls file into R.

library(gdata)
tbl <- read.xls("datafile.xls", sheet=1)

Option 5: The delimited file approach

If all else fails, using comma, pipe, or tab delimited files as intermediaries is always an option.  Below is the code to import a comma-separated file into R.

tbl <- read.csv("C:/data/datafile.csv")
2 Comments

Entering Data in SAS vs R

Both SAS and R offer programmatic and GUI-based approaches for entering data.  In SAS, a spreadsheet-like tool may be accessed for data entry by selecting “viewtable” from the Tools menu.  Alternatively, SAS users working through Enterprise Guide may double-click on a table to reveal a spreadsheet for data entry.  In R, the data.entry() function may be ran on an object to reveal a similar spreadsheet.  Below are approaches for programmatic data entry.  It should be noted that the below examples are not exhaustive and that other methods exist for data entry in both languages.

R

x <- c(1.1, 2.1, 3.1)

SAS

data x;
input var1;
cards;
1.1
2.1
3.1
;
run;
0 Comments

CSV File Import – SAS vs R

Below are methods for importing a csv file into both R and SAS.  The csv file to import is named “file.csv” and the data, once imported, is called tbl.  In SAS, there are two main approaches to importing a csv file.  The two methods are proc import and a data step.

R

tbl <- read.csv("C:/data/file.csv")

SAS

/* PROC Import Approach */

proc import datafile="C:\data\file.csv"
    out=tbl
    dbms=csv
    replace;
    getnames=yes;
run;

/* Data Step Approach */

data tbl;
    infile "C:\data\file.csv"
    delimiter=',';
    input var1 var2 ... varn;
run;

 

0 Comments