Upload
nightcrawler191
View
223
Download
0
Embed Size (px)
Citation preview
8/6/2019 SAS Macro Processing
1/7
SAS Macro Processing
Prof. David Mendona
1 Overview
The macro facility is a tool for extending and customizing SAS and for reducing the amount of text you
must enter to do common tasks. The macro facility enables you to assign a name to character strings or
groups of SAS programming statements. From that point on, you can work with the names rather than with
the text itself (SAS Doc).
Macros may therefore be used to automate repetitive tasks, or to create customized data management or
analysis procedures.
2 Basics
The macro facility is documented in SAS Products>Base SAS>SAS Macro Processing. There are also
some SAS Institute publications that concern macro processing, and lots of free code examples on the SAS
web site.
A macro is defined as follows:
%MACRO macro-name;
macro definition
%MEND macro-name;
It is invoked as follows:
%macro-name
The macro facility provides alot of the functionality we are accustomed to seeing in standard
procedural programming languages such as C.
A common use of macros for controlling program flow. This can be a powerful way to reduce the
amount of interactivity required in conducting an integrated analysis (e.g., data management,processing, analysis, visualization).
2.1 Example%macro plot;
proc plot;plot income*age;
run;%mend plot;
This may be invoked as follows:
data temp;set in.permdata;
if age>=20;run;%plotproc print;run;
The result is that SAS runs the following statements at run-time:
data temp;set in.permdata;
8/6/2019 SAS Macro Processing
2/7
if age>=20;run;proc plot;
plot income*age;run;proc print;run;
2.2 Other Useful Features
macro statements
In addition to %MACRO and %IF-%THEN, many other macro statements exist.
macro functions
Macro functions are functions defined by the macro facility. They process one or more arguments and
produce a result. For example, the %SUBSTR function creates a substring of another string, while the
%UPCASE function converts characters to uppercase.
autocall macros
Autocall macros are macros defined by SAS that perform common tasks, such as trimming leading or
trailing blanks from a macro variable's value or returning the data type of a value.
automatic macro variables
Automatic macro variables are macro variables created by the macro processor. For example, SYSDATE
contains the date SAS is invoked.
macro facility interfaces
Interfaces with the macro facility provide a dynamic connection between the macro facility and other parts
of SAS, such as the DATA step. For example, you can create macro variables based on values within the
DATA step using CALL SYMPUT and retrieve the value of a macro variable stored on a remote host using
the %SYSRPUT macro statement.
3 Details
3.1 Macro Variables
Macro variables are tools that enable you to dynamically modify the text in a SAS program through
symbolic substitution. You can assign large or small amounts of text to macro variables, and after that, you
can use that text by simply referencing the variable that contains it.
Macro variables defined by macro programmers are called user-defined macro variables. Those defined by
SAS are called automatic macro variables. You can define and use macro variables anywhere in SASprograms, except within data lines.
The simplest way to create and assign a value to a macro variable is to use the macro program statement
%LET.
The statement
%let num=100+200;
assigns the string 100+200 to num. If you want to assign a number to a variable, you need the %EVAL
statement:
%let num=%eval(100+200); / * produces 300 * /
To access the value of a macro variable, use the & symbol:
%let dsn=Newdata;
8/6/2019 SAS Macro Processing
3/7
title1 "Contents of Data Set &dsn";
3.2 Macro Processing
Macros are compiled programs that you can call in a submitted SAS program or from a SAS command
prompt. Like macro variables, you generally use macros to generate text. However, macros provide
additional capabilities:
Macros can contain programming statements that enable you to control how and when text is generated.
Macros can accept parameters. This enables you to write generic macros that can serve a number of uses.
To compile a macro, you must submit a macro definition. The general form of a macro definition is
%MACRO macro-name;%MEND ;
where macro_name is a unique SAS name that identifies the macro and macro_text is any combination of
macro statements, macro calls, text expressions, or constant text.
When you want to call the macro, you use the form
%macro_name
3.3 Scope of Macro Variables
Every macro variable has a scope. A macro variable's scope determines how it is assigned values and how
the macro processor resolves references to it.
Two types of scopes exist for macro variables:globaland local:
Global macro variables exist for the duration of the SAS session and can be referenced anywhere in the
program--either inside or outside a macro. They are defined outside the macro.
Local macro variables exist only during the execution of the macro in which the variables are createdand have no meaning outside the defining macro.
For more detail, see the documentation on the %GLOBAL and %LOCAL statements.
3.4 Macro Expressions
There are three types of macro expressions: text, logical, and arithmetic. A textexpression is any
combination of text, macro variables, macro functions, or macro calls. Text expressions are resolved to
generate text.Logicalexpressions and arithmetic expressions are sequences of operators and operandsforming sets of instructions that are evaluated to produce a result. An arithmetic expression contains an
arithmetic operator. A logical expression contains a logical operator.
3.5 Storing and Using Macros
When you submit a macro definition, by default, the macro processor compiles and stores the macro in a
SAS catalog in the WORK library. These macros, referred to as session compiled macros, exist only during
the current SAS session. To save frequently used macros between sessions, you can use either the autocallmacro facility or the stored compiled macro facility.
The autocall macro facility stores the source for SAS macros in a collection of external files called an
autocall library. The autocall facility is useful when you want to create a pool of easily maintained macros
in a location that can be accessed by different applications and users. Autocall libraries can be concatenated
together. The primary disadvantage of the autocall facility is that the first time that an autocall macro is
called in a session, the macro processor compiles it. This compilation is overhead that you can avoid by
using the stored compiled macro facility.
8/6/2019 SAS Macro Processing
4/7
The stored compiled macro facility stores compiled macros in a SAS catalog in a SAS data library that you
specify. By using stored compiled macros, you might save macro compilation time in your production-level
jobs. However, because these stored macros are compiled, you must save and maintain the source for the
macro definitions in a different location.
Data InputTwo approaches are presented here. Complete information is found in discussion of SAS DATA step.
Fig. 1. Input Approach 1: General (import from delimited file)
DATA Tutor.fham;/* note: if the variable is text, put the dollar sign after its
name */INFILE"C:\Documents and Settings\mendonca\My Documents\My SAS
Files\SAS Demos\Tutor\framingham.csv" delimiter = ',' ;/* this is the Framingham study data set */
INPUT cause $ age chol sex $;RUN;
Fig. 2. Input Approach 2: Specific (import Excel via OBDC)
PROCIMPORT OUT= Tutor.GENLDATAFILE= "C:\Documents and Settings\mendonca\My Documents\SAS
Demos\Tutor\General.xls"DBMS=EXCEL2000 REPLACE;GETNAMES=YES;
RUN;
See SAS Help for PROC IMPORT.
Fig. 3. Output Approach
PROCEXPORT DATA= Tutor.GenlOUTFILE= "C:\Documents and Settings\mendonca\My
Documents\SAS Demos\Tutor\GeneralCopy.xls"DBMS=EXCEL2000 REPLACE;
RUN;
For other ideas, see SAS Help for PROC EXPORT.
8/6/2019 SAS Macro Processing
5/7
4 Statistical Operations
Objectives: Understand how to generate descriptive statistics, perform a t-test and run a generalized linear
model.
4.1 Descriptive Statistics
To have a look at some basic summary statistics, use PROC MEANS.
Fig. 4. PROC MEANS
/*Basic data description*/DATA heart;SET Tutor.fham;RUN;
/*The data must first be sorted by the vars in the BY statement of PROCMEANS*/PROCSORTDATA = heart;
by cause sex;RUN;
PROCMEANSDATA = heart; /*the data step is redundant*/
VAR age chol;BY cause sex;
RUN;
/*the output is visible in the Results window*/
For other options, see SAS Help for The Means Procedure at Base SAS>SAS Procedures>Procedures.
4.2 Statistical Tests
SAS has the capability of performing a very wide variety of parametric and non-parametric statistical tests.
Tests based on the Students tdistribution are common and so are discussed here. The TTEST procedure
performs t tests for one sample, two samples, and paired observations. The one-sample t test compares the
mean of the sample to a given number. The two-sample t test compares the mean of the first sample minus
the mean of the second sample to a given number. The paired observations t test compares the mean of thedifferences in the observations to a given number [PROC TTEST documentation].
Fig. 5. PROC TTEST
procttestdata=graze; class GrazeType;
var WtGain;run;
Is this a one- or two-sample test? What is the null hypothesis?Should the results be based on the assumption of equal or unequal variance? What is the result?
Help: SAS/STAT Users Guide>The PROC TTEST procedure
4.3 General Linear ModelsThe GLM procedure uses the method of least squares to fit general linear models. Among the statistical
methods available in PROC GLM are regression, analysis of variance, analysis of covariance, multivariateanalysis of variance, and partial correlation [PROC GLM documentation]. So, it does all analyses that
PROC REG does and more:
simple regression
multiple regression
analysis of variance (ANOVA), especially for unbalanced data
analysis of covariance
8/6/2019 SAS Macro Processing
6/7
response-surface models
weighted regression
polynomial regression
partial correlation
multivariate analysis of variance (MANOVA)
repeated measures analysis of variance
A limitation is that it does not have a built-in PLOT option.
Fig. 6. PROC GLM
procglmdata=fitness;model Oxygen=Age Weight RunTime RunPulse RestPulse MaxPulse;
run;
Help: SAS/STAT Users Guide>The PROC GLM procedure
5 Graphing Procedures
Objective: Understand how to generate a scatter plot.
Key SAS procedure: PROC PLOT
Fig. 7. PROC PLOT
procglmdata=fitness;model Oxygen=Age Weight RunTime RunPulse RestPulse MaxPulse;/*need access to output data*/outputout=Tutor.mfitness predicted=Oxygenhat r=resid stdr=eresid
run;
DATA mfitness;SET Tutor.mfitness;
RUN;
PROCPLOTDATA = mfitness;
plot resid*Oxygenhat; title'Physical Fitness Analysis';
title2'Model: Oxygen=Age Weight RunTime RunPulse RestPulseMaxPulse'; title3'Residuals vs. Predicted';run;
For other options, see SAS Help for The PLOT Procedure at Base SAS>SAS Procedures>Procedures.
8/6/2019 SAS Macro Processing
7/7
6 Links
http://www.sas.com
http://gsbapp2.uchicago.edu/sas/sashtml/main.htm (look under Base SAS, SAS/STAT andSAS/GRAPH)
http://www.stat.wisc.edu/computing/sas/intro.html
http://www.stat.wisc.edu/computing/sas/ (miscellaneous links)
http://www.sas-jobs.com/
http://www.sasusers.com/
7 Contact Information
David Mendona
Email: [email protected]
Phone: x5212
mailto:[email protected]:[email protected]