Skip to main content

Write SAS Code for a Procedure

Source: R/sas-proc.R

Description

Writes the SAS code for one statistical procedure (proc) as text. Each statement is one argument: the argument name is the SAS keyword, and the value is the rest of the statement, written as you would write it in SAS. The most common statements, such as model and class, have their own arguments. Any other statement can also be passed by name.

Usage

sas_proc(
  proc,
  ...,
  class = NULL,
  model = NULL,
  tables = NULL,
  var = NULL,
  by = NULL,
  where = NULL,
  data = "&dsn",
  options = NULL
)

Arguments

Arguments Description
proc A single string with the name of the proc, such as "freq" or "mixed". Any proc name works, but only the procs listed in “Default statements” get default statements.
… Other statements, passed by name, such as lsmeans, strata, or output. Their values work the same way as for class and the other common statements. Use the same name more than once to repeat a statement.
class, model, tables, var, by, where Common statements. Each value is a string, or a character vector that is joined with spaces. Use a slash for statement options, as in SAS, for example model = "mpg = wt / clb". If you do not pass a statement, the proc default is used, if there is one. Pass NULL to leave the statement out.
data A single string with the data set name, or NULL to leave out data=. The default is the &dsn placeholder. The data set must exist in SAS when you run the code, for example after you import mtcars from a CSV file.
options A character or numeric vector of options for the proc line. Unnamed values are written as they are, and named values are written as name=value. For example, c("n", "mean", alpha = 0.05) gives n mean alpha=0.05.

Details

Statements are written in this order, no matter the order you pass them:

  1. class

  2. The main statement: tables, var, model, time, or paired

  3. All other statements, in the order you pass them

  4. by, where, weight, freq, and id

  5. output

The code ends with quit; for the interactive procs (anova, catmod, glm, and reg), and with run; for all other procs.

Default statements

If you leave out a statement that the proc needs, sas_proc() adds it. Default statements use SAS macro variables, such as &row, as placeholders for the variable names.

Proc Default statements freq tables &row&col means, univariate var &var ttest class &group, var &var reg model &outcome = &x glm class &group, model &outcome = &group logistic model &outcome(event=‘1’) = &x mixed class &trt &subj, model &outcome = &trt lifetest time &time&censor(0) phreg model &time*&censor(0) = &trt

Other procs have no default statements. To leave out a default statement, set it to NULL, for example class = NULL.

Value

An object of class sas_proc. Printing it shows the code with syntax colors. Use as.character() to get the code as plain text, for example to write it to a .sas file.

Examples

# Default statements with placeholders
sas_proc("freq")
proc freq data=&dsn;
  tables &row*&col;
run;
# lm(mpg ~ wt + hp, data = mtcars), with confidence limits
sas_proc("reg", data = "mtcars", model = "mpg = wt hp / clb")
proc reg data=mtcars;
  model mpg = wt hp / clb;
quit;
# t.test(mpg ~ am, data = mtcars, conf.level = 0.90)
sas_proc(
  "ttest",
  data = "mtcars",
  class = "am",
  var = "mpg",
  options = c(alpha = 0.1)
)
proc ttest data=mtcars alpha=0.1;
  class am;
  var mpg;
run;