From SAS to R

SAS users, welcome to the R world. That page is intended to help you in the switch procedure from SAS to R. You will quickly forget about DATA and PROC steps and discover a whole new world of statistical computing.

Frank Harrell said on R-help on Thu, 2003-09-04 at 08:34:

See (link) Section 1.6 for a comparison of S and SAS (though this doesn’t address numerical reliability). Overall, SAS is about 11 years behind R and S-Plus in statistical capabilities (last year it was about 10 years behind) in my estimation. Frank Harrell SAS User, 1969-1991

Another user comments:

I agree with Frank that R has many advantages over SAS, but an extraordinary exception is in the area of mixed models, where R is literally about 10 years behind SAS (based on the release date of PROC MIXED). PROC MIXED has simple model specification, abundant choice of error structures, enterprise-level data capacity, robust algorithms, convenient tools for reporting general combinations of BLUPs and BLUEs, approximate tests of covariance parameters, multiple options for degrees of freedom in tests of fixed effects, an abundance of example code available on the internet, etc. etc. Not a single one of these features has (traditionally) been available in R. (I know, I have beaten myself blue trying to do these things in R.) Doug Bates and others are doing some great work developing the lme4 package, but SAS is many years ahead in the race. (Genstat too, but that’s another issue.) Use SAS for mixed models. Use R for most everything else.

Random Samples

In this page, Christian Robert describes how to draw random samples from SAS, as an illustration of “Why R and not SAS”; the. Here are the equivalent R commands:

  • a sample of 300 observations in a population of 500 individuals without replacement (21 lines of SAS macro code):
Data2 <- Data[sample(500, 300)]
  • a random sample of size 20 with replacement in a vector of size 100 (14 lines of SAS code):
Sample1 <- test[sample(100, 20, replace = TRUE)]
  • a sample $$X_1, \ldots,X_{20} ~ N(0,1)$$ (6 lines of SAS code – not too bad)
select <- rnorm(20)

Sample from the exponential distribution

With SAS, one can draw a sample of 20 observations from the exponential distribution as follows :

data test(drop=i);
  do i=1 to 20;
    x=lambda * ranexp(4145);
    output; 
  end;
run;

4145 is the seed of the generator, more info about SAS seeds. More important here is the fact that lambda is not a parameter of the SAS function ranexp, the SAS user has to figure out((it can be a good exercize though)) that if $$X ~ epsilon(1)$$ then $$\lambda X ~ epsilon(\lambda)$$.

In R, it is much simpler

test <- rexp(20, lambda)

Note that R does not require the seed, but if you want, you can set it using set.seed

External links

Romain François 2006/03/04 15:49

 
getting-started\translations\sas2r.txt · Last modified: 2009/06/18
 
Recent changes RSS feed R Wiki powered by Driven by DokuWiki and optimized for Firefox Creative Commons License