Jump to content

SAS/Programming

From Wikibooks, open books for an open world
< SAS

SAS Programming Modes

[edit | edit source]

SAS uses a variety of programming modes to manipulate data. Among them are :

  • Global statements, which define or alter the program environment
  • SAS data steps, which use the data step language to create, input, modify, and output data in rectangular database tables (datasets).
    • A SAS datastep commences with the DATA keyword, and is generally terminated with a RUN statement.
  • Procedures, which are pre compiled structures designed to produce defined outcomes given (usually) the input of SAS datasets and one or more control parameters
    • A SAS procedure commences with the PROC keyword followed by the procedure name, and is generally terminated with a RUN or QUIT statement.
  • SQL statements, issued within PROC SQL, that provide an alternative (and more standard) method of creating and modifying database tables
  • IML (Interactive Matrix Language) statements, issued within PROC IML, that provide a method of manipulating matrix data.
  • SAS Graph statements, which use a special vocabulary of statements to control graphical output.
  • ODS statements, which use another special vocabulary of statements to control output to external media
  • The SAS Macro Language, which modifies program statements prior to execution, and allows generic code to be adapted for use each time it is run, by substitution of control parameters.
    • Macro code is generally identified by statements which contain keywords prefaced by % or &.

Macros

[edit | edit source]

A simple example

[edit | edit source]

The following macro computes frequencies, plots them and store the graph in a file. Instead of repeating all those steps each time, you just need one line to perform all these tasks together.

%macro plot_dist(var=,title=,name=); 
proc freq data = lib.fic_dad_06_1 ; 
table &var /out = _freq_ ; 
run ; 
goptions reset=all;
filename output &name;
goptions device=png gsfname=output gsfmode=replace;
proc gplot data = _freq_ (where = (&var > 1 & &var < 70)) ;
title &title ;  
symbol v = circle c = black i = none; 
plot count * &var / grid href=10 20 50 ; 
run; 
quit ; 
%mend ; 

%plot_dist(var = EFFECTIF , title = "Effectif", name = "W:/…/hist_eff.png");
  • The declaration of a macro begins with %macro and ends with %mend.
  • Arguments in the macro are preceded by the & symbol.