mice <- sas.get("saslib", mem="mice", var=c("dose", "strain", "ld50"))
plot(mice$dose, mice$ld50)
nude.mice <- sas.get(lib=unix("echo $HOME/saslib"), mem="mice",
ifs="if strain='nude'")
nude.mice.dl <- sas.get(lib=unix("echo $HOME/saslib"), mem="mice",
var=c("dose", "ld50"), ifs="if strain='nude'")
# Get a dataset from current directory, recode PROC FORMAT; VALUE \dots
# variables into factors with labels of the form "good(1)" "better(2)",
# get special missing values, recode missing codes .D and .R into new
# factor levels "Don't know" and "Refused to answer" for variable q1
d <- sas.get(mem="mydata", recode=2, special.miss=TRUE)
attach(d)
nl <- length(levels(q1))
lev <- c(levels(q1), "Don't know", "Refused")
q1.new <- as.integer(q1)
q1.new[is.special.miss(q1,"D")] <- nl+1
q1.new[is.special.miss(q1,"R")] <- nl+2
q1.new <- factor(q1.new, 1:(nl+2), lev)
# Note: would like to use factor() in place of as.integer \dots but
# factor in this case adds "NA" as a category level
d <- sas.get(mem="mydata")
sas.codes(d$x) # for PROC FORMATted variables returns original data codes
d$x <- code.levels(d$x) # or attach(d); x <- code.levels(x)
# This makes levels such as "good" "better" "best" into e.g.
# "1:good" "2:better" "3:best", if the original SAS values were 1,2,3
# For the following example, suppose that SAS is run on a
# different machine from the one on which S is run.
# The sas_get macro is used to create files needed by
# sas.get. To make a text file containing the sas_get macro
# run the following S command, for example:
# cat(sas.get.macro, file='/sasmacro/sas_get.sas', sep='\n')
# Here is the SAS job. This job assumes that you put
# sas_get.sas in an autocall macro library.
# libname db '/my/sasdata/area';
# %sas_get(db.mydata, dict, data, formats, specmiss,
# formats=1, specmiss=1)
# Substitute whatever file names you may want.
# Next the 4 files are moved to the S machine (using
# ASCII file transfer mode) and the following S
# program is run:
mydata <- sas.get(sasout=c('dict','data','formats','specmiss'),
id='idvar')
# If PKZIP is run after %sas_get, e.g. "PKZIP port dict data formats"
# (assuming that specmiss was not used here), use
mydata <- sas.get(sasout='a:port', id='idvar')
# which will run PKUNZIP port to unzip a:port.zip, creating the
# dict, data, and formats files which are generated (and later
# deleted) by sas.get
# Retrieve the same variables from another dataset (or an update of
# the original dataset)
mydata2 <- sas.get('mydata2', var=names(mydata))
# This only works if none of the original SAS variable names contained _
# Code from Don MacQueen to generate SAS dataset to test import of
# date, time, date-time variables
# data ssd.test;
# d1='3mar2002'd ;
# dt1='3mar2002 9:31:02'dt;
# t1='11:13:45't;
# output;
#
# d1='3jun2002'd ;
# dt1='3jun2002 9:42:07'dt;
# t1='11:14:13't;
# output;
# format d1 mmddyy10. dt1 datetime. t1 time.;
# run;
Run the code above in your browser using DataLab