In our previous entry, we described how to generate a dataset from SAS that could be used for analyses in R. Alternatively, someone primarily using R might want to test the new ”statistical graphics” procedures available starting with SAS 9.2. Here we show how one might create a long data set in R, export it, read it in SAS, and generate a plot similar to that shown in example 7.9.

**R**

In R, we use the `reshape() function` (section 1.5.3) to make the ”long” data set. We make a data frame (section B.4.5) from the data we’ll need in SAS. We check the data, using the `order()` function (section 1.5.6) to organize the new data frame by subject instead of visit number. Finally, we write out the data set in dBase format (section 1.2.2).

> ds <- read.csv

("http://www.math.smith.edu/sasr/datasets/helpmiss.csv")

> attach(ds)

> long <- reshape(ds, idvar="id",

varying=list(c("cesd1","cesd2","cesd3","cesd4")),

v.names="cesdtv", timevar="visit", direction="long")

> attach(long)

> tosas <- data.frame(id, visit, cesdtv)

> head(tosas[order(id, visit),])

id visit cesdtv

1 1 1 7

471 1 2 NA

941 1 3 8

1411 1 4 5

2 2 1 11

472 2 2 NA

> library(foreign)

> write.dbf(tosas,"c:\book\tosas.dbf")

**SAS**

In SAS, we read in the data from the dBase format ﬁle (section 1.1.5), `proc sort` (section 1.5.6) it in place, and check the data using `proc print`(section 1.2.4).

proc import datafile="C:booktosas.dbf"

out=fromr dbms=dbf;

run;

proc sort data=fromr; by id visit; run;

proc print data=fromr (obs=5); run;

Obs id visit cesdtv

1 1 1 7

2 1 2 .

3 1 3 8

4 1 4 5

5 2 1 11

Finally, we generate the desired plot with `proc sgpanel` (section 5.1.11), using the `where` statement (section A.6.3) to select the ﬁrst 20 subjects.

proc sgpanel data=fromr;

where id le 20;

panelby id / rows=4 columns=5;

scatter x=visit y=cesdtv;

run;

*Related*

To

**leave a comment** for the author, please follow the link and comment on his blog:

** SAS and R**.

R-bloggers.com offers

**daily e-mail updates** about

R news and

tutorials on topics such as: visualization (

ggplot2,

Boxplots,

maps,

animation), programming (

RStudio,

Sweave,

LaTeX,

SQL,

Eclipse,

git,

hadoop,

Web Scraping) statistics (

regression,

PCA,

time series,

trading) and more...

If you got this far, why not

__subscribe for updates__ from the site? Choose your flavor:

e-mail,

twitter,

RSS, or

facebook...

**Tags:** Convert R to SAS, foreign library, order, proc import, reshape, sort