The Full Wiki

Data Analysis using the SAS Language/Macro Programming: Wikis

Advertisements

Note: Many of our articles have direct quotes from sources you can cite, within the Wikipedia article! This article doesn't yet, but we're working on it! See more info or our list of citable articles.

Study guide

Up to date as of January 14, 2010

From Wikiversity

50%.svg Completion status: this resource is ~50% complete.
Nuvola apps kalzium.svg Subject classification: this is a science resource .
Bellcurve.svg Subject classification: this is a statistics resource .
Gnome-fs-client.svg Subject classification: this is an information technology resource .
Sciences humaines.svg Educational level: this is a tertiary (university) resource.

Contents

Introduction

Macros and macro variables can be used to change a SAS program while it is running. Macro variables are also used to define SAS code that is used in several parts of the SAS program. The following demonstrates the use of macro variables and the resulting code.

     %let listStates='NY' 'NJ' 'CT';
     %let reportYear=2008;
     proc print data=sales(where=(state in (&listStates) and year=&reportYear));
          title "Sales Report for Selected States for and Year &reportYear";
          title2 "States in report: &listStates";
          id state;
          var Month SalesVolume Purchases;
          sum salesVolume Purchases;
     run;

When the macro variables are procesed above, the final SAS code looks as follows:

     proc print data=sales(where=(state in ('NY' 'NJ' 'CT') and year=2008));
          title "Sales Report for Selected States for and Year 2008";
          title2 "States in report: 'NY' 'NJ' 'CT'";
          id state;
          var Month SalesVolume Purchases;
          sum salesVolume Purchases;
     run;

The discussion will explain some of the finer points about macros and show more complex and useful examples.

Macro Variables

Macro variables are assigned using the %let statement. The assignment occurs during preprocess before the nonmacro code is parsed during processing. A macro variable can refer to a single value, a portion of a statement, or an entire piece of SAS code. It is refered to in the SAS code as &variablename. When it is encountered during preprocessing it is replaced in the code by the its contents.

Macro Statements

There are several macro statements similar to the SAS data step statemetns. These statements include assignments, logic and loops. The difference is that these statements operate on macro variable and produce SAS code. This can make a powerful SAS program dynamic and flexible. From a programming standpoint, a section of SAS code can be written once, debugged, placed in a macro and then used as often as needed by placing the macro reference in the code at each point it is needed.

Assignment

Advertisements

%let

The %let macro statment does not need to appear inside of a macro.

     %let mytitle="Apple Shipment as of &sysdate";
     title "&mytitle";
     proc print data=shipment;
     run;

symput symget

SQL :into

System Macro Variables

Macro Logic

%if %then %else

Macro Loops

%do %while %end

%do %until %end

Macro Definition %macro

Macro Modules

Macro modules and simple macros, are combination of macro statments that produce SAS code. Modules can take parameters which can be used to produce dynamic SAS code.


Advertisements






Got something to say? Make a comment.
Your name
Your email address
Message