Introduction to sas macros

Solo disponible en BuenasTareas
  • Páginas : 10 (2395 palabras )
  • Descarga(s) : 0
  • Publicado : 15 de noviembre de 2011
Leer documento completo
Vista previa del texto
Introduction to SAS Macros
By Andy Leone
December 4, 2006

SAS macros can be very handy when you are doing certain things over and over again. We are going to learn macros by first learning the basic functions and then by working through two applications. The first application generates some basic univariate statistics that are in a nice format for exporting to Excel. In the next class, wewill use the data to generate some tables in excel. The second application will be to winsorize data. I use this macro (or a variation on it) for virtually all the projects I work on.

1.0 Macro Basics

1. Macro statements – Most macro statements begin with %. Following are some commonly used macro statements.

%let – The let statement assigns a value or characters to a macro variable.For example:

%let count=5; *** This assigns the value 5, to the macro variable count. If I later want to reference the macro variable, count, I reference it as &count. At this point &count is equal to 5.

%let list=john bill fred; ** this assigns john bill fred to the variable list. Note that you do not need to put the names in quotes eventhough it is a string. SAS treats everythingas a string.

%eval – The eval function allows you to evaluate or compute a mathematical operation. For example:

%let count=%eval(&count+1); *The operation &count+1 is evaluated and the value is assigned to the variable count. If the variable count was equal to 5 before the let statement was executed, it now is equal to 6 (5+1).

%qscan - QSCAN searches for a word in a list that isspecified by its position in that list.
Syntax
%QSCAN(argument, n)

Argument - the list you are searching across (it is usually a macro variable).
N – the number of positions to move over
Delimiters – This describes how each word or expression will be delimited. If you want to delimit the words by a space, you should enclose the space with %str. %str is like putting something in quotes.Your delimiter will normally look like %str( ). Note that there is one space within the parentheses.

Example:

Suppose we want to obtain the second word in the macro variable %list (that we defined above to be “john bill fred” and then put that in the variable called firstname.

%let firstname=%qscan(&list,2,%str( )) ; *here the contents of the variable firstname is bill.

%put – the putstatement prints the contents to the log window. For example:

%put &list; ** this will print the contents of the variable list to the log window.

2. Macro names and execution

The functions discussed above can be executed anywhere in a SAS program. Often though, you will be executing macro statements that are contained within a macro. A macro is defined as follows:

%macro name;Macro statements

%mend;

The macro can then be “executed” with the statement %name;

For example, suppose we want to print the contents of the variable, list, one name at a time. We will use a %do loop (similar to the do loops we used in perl).

%do – Do loops often come in handy. For example suppose we want to print the contents of the variable &list, one name at a time.

%letlist=John Bill Fred;

%macro printnames; ** This marks the start of the macro printnames;
%do i=1 %to 3; ** This is the start of a do loop with i as the counter;
/** following we print "Name: " followed by a word from the macro variable list.
It searches for the ith word (which is the counter, delimited by a space";*/
%put Name: %qscan(&list,&i,%str( ));
%end;
%mend;
/** this next statementexecutes the macro**/
%printnames;
Output from the macro printnames;

238 %let list=John Bill Fred;
239
240 %macro printnames;
241 %do i=1 %to 3;
242 %put Name: %qscan(&list,&i,%str( ));
243 %end;
244 %mend;
245 %printnames;
Name: John
Name: Bill
Name: Fred

%if – You can use if then statements (like in Perl!). We will use these in the examples below.

2.0 Macro...
tracking img