+91 8600984908
Admin@riskycall.com

SAS and R : Data Import and Export

27 Nov 2016

SAS and R : Data Import and Export

//
Comments0
SAS and R are two statistical softwares which are widely used in various fields of research. SAS is licensed and paid software whereas R is free and open-source software. These two statistical softwares use almost every day by risk management professionals. To specify the path or directory where all files are stored and will be stored, use these command in SAS and R
  • SAS
    libname path ‘directory_location’;
  • R
    path<- “directory_location”
    setwd(path)

Data Import and Export

1.Import CSV files

SAS
data dataname;
infile ‘filename.csv’ delimiter=’,’ ;
 input varname1 varname2  varname3 $ varname4… varnamen;
run;

 

proc import datafile=’filename’
  out=dataname dbms=csv;
  delimiter=’,’ ;
  getnames=yes;
run;   

R
dataname = read.csv(“filename.csv”)

2.Read sheets from excel file

SAS
proc import datafile=’filename’
  out=dataname dbms=csv;
  delimiter=’,’ ;
  getnames=yes;
run;

R
dataname <- read.xlsx(filename, sheetName = “name”)

Read data from R to SAS

SAS
proc import datafile=”sasdata.dbf”
 out=fromr dbms=dbf;
 run;

R
library(foreign)
sasdata = data.frame(dataname)
write.dbf(sasdata,”sasdata.dbf”)

Read data from SAS to R

SAS
proc export datafile= dataname
 outfile = “rdata.dbf” dbms=dbf;
 run;

R
library(foreign)
dataname <- read.dbf(“rdata.dbf”)

Write excel file

SAS
proc export data= dataname
 outfile=’dataname.xlsx’ dbms = xlsx replace;
run;

R
library(xlsx)
write.xlsx(dataname, file = “dataname.xlsx”, sheetName = “Sheet1”)

Leave a Reply