[R] SAS dataset

BXC (Bendix Carstensen) BXC at novo.dk
Thu Jun 22 09:49:01 CEST 2000


> -----Original Message-----
> From: Peppy [mailto:s.adi.purnomo at EnergyLink.co.nz]
> Sent: 22. juni 2000 00:15
> To: 'R - Help desk'
> Subject: [R] SAS dataset
> Importance: High
> Sensitivity: Confidential
> 
> 
> Hello,
> 
> Is there any way we convert SAS dataset into R dataset?
> 
> Kindest Regards,
> Peppy Adi-Purnomo
> 
> ------
> Peppy Adi-Purnomo
> Energy Market Analyst
> 
> Energy Link Ltd
> Dunedin - New Zealand	
> 
> Ph.: +64 3 479 2475
> Fax: +64 3 477 8424
> 
> Email: s.adi.purnomo at energylink.co.nz
> www.EnergyLink.co.nz



I enclose the code of a SAS-macro, %out, which will produce a file
that can be read by read.table in R, (so it requires that you have SAS
installed and working on your machine), and the code of a R-function, 
sas.get, that stats SAS from within R. The latter you have to change, 
because it assumes that the shell command to execute the SAS-program 
prog in batch is: 

  sjx prog

which is just my wrapper for:

  c:\stat\sas\SAS.exe -SYSIN %1.sas -icon

Good luck,

Bendix Carstensen

----------------------
Bendix Carstensen
Senior Statistician
Steno Diabetes Centre
Niels Steensens Vej 2
DK-2820 Gentofte
Denmark
tel: +45 44 43 87 38
mob: +45 28 25 87 38
fax: +45 44 43 73 13
bxc at novo.dk
www.biostat.ku.dk/~bxc
----------------------

Here is the sas-macro:

%macro out ( data = _last_ ,
              var = ,
             file = ud.dat,
           header = Y ) ;

proc format ; value  na ( default = 15 )  ., .A  = 'NA' ;
              value $na ( default = 200 ) ' ' = 'NA' ; run ;

proc contents  data = &data.  noprint  out = varnames ;
run ;

proc sort  data = varnames ;
  by varnum ;
run ;

%let nc = 0 ;

data _null_ ;
  set varnames ( keep = name type length
                where = ( type eq 2 ) )
               end = slut ;
  length cvars lngth $ 200 ;
  retain cvars lngth nc ;
  nc + 1 ;
  length = length + 2 ;
  cvars = trim(cvars) || ' ' || left(trim(name)) ;
  lngth = trim(lngth) || ' ' || left(trim(name)) || ' $ ' || put(length,3.)
;
  if slut then do ;
               call symput ( "cvar", cvars ) ;
               call symput ( "nc", nc ) ;
               call symput ( "lngth", lngth ) ;
               end ;
run ;

%if &var.= %then %do ;

data _null_ ;
  set varnames ( keep = name ) end = slut ;
  length vars $ 200 ;
  retain vars ;
  vars = trim(vars) || ' ' || left(trim(name)) ;
  if slut then call symput ( "var", vars ) ;
run ;

%end ;

data null ;
  %if &nc.>0 %then length &lngth. ; ;
  set &data. ;
  file "&file." ;
  format &var. na. ;
  %if &nc.>0 %then ;
  %do i = 1 %to &nc. ;
      %let cvn = %scan ( &cvar., &i. ) ;
      &cvn. = '"' || trim(left(&cvn.)) || '"' ;
  %end ;

  %if %upcase(%substr(&header.,1,1))=Y %then %do ;
  vnames = translate("&var.", 'abcdefghijklmnopqrstuvwxyzæøå.',
                              'ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ_' ) ;
  if _n_ eq 1 then put vnames ;
  %end ; ;
  put _n_ &var. ;
run ;
	
%mend out ;

and here is the R-function:

sas.get <- function(dir="data", data, var=" ")
  {
  sink("xqwz.sas")
#  cat("options mprint ;")
  cat(paste("libname rtmp '", dir,
                       "' ;", sep="") )
  cat(paste("%out ( data=rtmp.", data,
                       ", var=", var,
                          ") ;", sep="") )
  sink()
  shell("sjx xqwz")         # Runs the SAS-program xqwz.sas in batch
  a <- read.table("ud.dat")
  shell("del xqwz.sas")     # The rest is cleaning up
  shell("del xqwz.log")
  shell("del xqwz.lst")
  shell("del ud.dat")
  a 
}
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list