1 / 36

why use macros

2. INTRODUCTION. The SAS

Jims
Download Presentation

why use macros

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1: WHY USE MACROS?

    2: 2

    3: 3 INTRODUCTION Macro programming is generally considered an advanced topic But, while macros certainly can be challenging, it is also true that the basic concepts are not difficult to learn

    4: 4 INTRODUCTION This course is designed for students who know the basics of SAS programming, but know nothing about SAS macro programming As we won’t start from the very beginning, if you are not a SAS user at all, then it is not a course for you

    5: 5 INTRODUCTION We explain how the macro processor works, and how to use macros and macro variables Using these techniques you can create flexible, reusable code that can save you time and effort

    6: 6 INTRODUCTION Because macro code takes longer to write and debug than standard SAS code, you generally won’t use macros in programs that will be run only a few times But if you find yourself writing similar code over and over again, then macros may make your job easier

    7: 7 MACROS CAN HELP First, with macros you can make one small change in your program and have SAS echo that change throughout your program Second, macros can allow you to write a piece of code and use it over and over again in the same program or in different programs Third, you can make your programs data driven, letting SAS decide what to do based on actual data values

    8: 8 The most important concept to keep in mind whenever you write macro code is that THE MACRO PROCESSOR

    9: 9 INTRODUCTION When you submit a standard SAS program, SAS compiles and then immediately executes it But when you write macrocode, there is an extra step

    10: 10 INTRODUCTION Before SAS can compile and execute your program, SAS must pass your macro statements to the macro processor which then “resolves” your macros generating standard SAS code Because you are writing a program that writes a program, this is sometimes called meta-programming

    11: 11 Let’s do exercises Scenario data sets with one same variable named TEMP for year 1981 – 2010 YR1981, YR1982, …. , and YR2010 Find mean TEMP for each year PROC MEANS DATA=yr1981; VAR temp; RUN;

    12: 12 INTRODUCTION The SAS macro language consists of macro variables, macro programs, macro facility interfaces, and macro storage techniques

    13: 13 MACRO VARIABLES SAS macro variables are the basic units that are used by macro facility interfaces and macro programs They can be created and resolved anywhere in a SAS program The name and value of a macro variable is stored in memory in a Symbol Table, either local or global in scope; the value is always simply a string of characters

    14: 14 MACRO VARIABLES Macro variables come in two varieties: either local or global A macro variable’s scope is local if it is defined inside a macro Its scope is global if it is defined in “open code” which is everything outside a macro. You can use a global macro variable anywhere in your program, but you can use a local macro variable only inside its own macro.

    15: 15 MACRO VARIABLES If you keep this in mind as you write your programs, you will avoid two common mistakes: trying to use a local macro variable outside its own macro, and accidentally creating local and global macro variables having the same name

    16: 16 MACRO VARIABLES A macro variable is like a standard data variable except that it does not belong to a data set and has only a single value which is always character The value of a macro variable could be a variable name, a numeral, or any text you want substituted in your program

    17: 17 MACROS vs. MACRO VARIABLES The names of macro variables start with an ampersand (&), while the names of macros start with a percent sign (%) The % and & characters are called macro triggers; the character following the macro trigger must not be a space %let dset = MASTER ; PROC PRINT DATA=&dset ; RUN;

    18: 18 USER-DEFINED MACRO VARIABLES the %LET statement enables you to define a macro variable and assign it a value %LET variable = value ; variable can be any name following SAS naming conventions value can be any string numeric tokens are stored as character strings, quotes are stored as part of the value the case of value is preserved numeric tokens are stored as character strings

    19: 19 USER-DEFINED MACRO VARIABLES the %LET statement enables you to define a macro variable and assign it a value %LET variable = value ; length range is 0-32K characters mathematical expressions are not evaluated leading and trailing blanks are removed from value before the assignment is made if variable already exists, value replaces the current value

    20: 20 SYSTEM-DEFINED MACRO VARIABLES System-defined macro variables are created by the SAS Supervisor created at SAS invocation are global(always available) can be assigned values by the user in some cases

    21: 21 SYSTEM-DEFINED MACRO VARIABLES some system-defined macro variables

    22: 22 SYSTEM-DEFINED MACRO VARIABLES NOTE: the value of macro variables SYSDATE and SYSTIME are character strings, not SAS date or time values. Example: %let job = HW1 ; Footnote "&job by YUFEN LI on &SYSDATE &SYSTIME";

    23: 23 MACRO PROGRAMS similar to a subroutine or function in a procedural programming language e.g. Fortran, C/C+/C++ with a name which is used to call it, and it can accept parameters the names of macros start with a percent sign (%)

    24: 24 MACRO PROGRAMS Syntax %MACRO macro-name (parameters); … macro-text … %MEND macro-name; parameters are separated by commas there are two type of macro parameters: positional and keyword

    25: 25 TYPE OF MACRO PARAMETERS positional parameters receive their values in the order (position) in which they are specified in the macro invocation positional parameters, if any, must appear first in the parameter list keyword parameters are those which start with the name of the macro variable followed by an = symbol and optionally followed by an initial value keyword parameters may appear in any order after positional parameters %macro means(procopt,vars,dsn=_last_) ; proc means &procopt data = &dsn; var &vars ; %mend means;

    26: 26 MORE FLEXIBILITIES The following macro definition has one positional parameter ‘opts’ (with no = sign) and one keyword parameter ‘filenm’ (with an = sign) %macro mname (opts,filenm=file5); proc print data=&filenm &opts; title " Print &filenm data "; run; %mend;

    27: 27 MORE FLEXIBILITIES This macro can be called with any of these formats, specifying either or both or neither of the parameters: %mname () %mname (double) %mname (filenm=file6) %mname (double noobs, filenm=file7)

    28: 28 MACRO PROGRAMS Example %macro mname; proc print data=data.all; where var1=&newvalue; title " Print &syslast data "; run; %mend; OPTIONS MPRINT MLOGIC; %let newvalue=17; %mname

    29: 29 MACRO PROGRAMS Notice that there is NO SEMICOLON after ‘%mname’ This line calls the macro named ‘mname’; there is no semicolon there because that line will be replaced by the code from the macro program itself, which has the required semicolons Specify the MPRINT and MLOGIC options to generate as much information as possible from the macro processor

    30: 30 OPTION MPRINT You can see that SAS has inserted into the regular SAS log the MPRINT lines The statements generated by the macro processor are all labeled with the word MPRINT followed by the name of the macro that generated the statements By using the MPRINT system option it is easy to see the standard SAS statements your macro is generating

    31: 31 OPTION MLOGIC Use MLOGIC to debug macros If MLOGIC is in effect and the macro processor encounters a macro invocation, the macro processor displays messages that identify the beginning of macro execution the values of macro parameters at that point the execution of each macro program statement whether each %IF condition is true or false each iteration of the %DO loop the end of macro execution

    32: 32 MACRO FACILITY INTERFACES Macro Facility Interfaces are ways to use macro variables. They are : 1) CALL SYMPUT (macro-variable, text); This creates a macro variable named ‘macrovariable’ with a value of ‘text’.

    33: 33 MACRO FACILITY INTERFACES 2) Indirect References using multiple ampersands. A double ampersand (&&) in SAS code is resolved to a single ampersand If we have a macro variable ‘var1’ with a value of ‘char1’, and another macro variable ‘char1’ with a value of ‘Taiwan’, then a string in the SAS code of ‘&&&var1’ is resolved to ‘&char1’ on the first pass, and that is resolved to ‘Taiwan’ on the second pass

    34: 34 MACRO FACILITY INTERFACES 3) PROC SQL; SELECT … INTO: macrovariable …. ; proc sql; select sum(amount) into:total from mydata; run; This defines a macro variable ‘total’ with a value that is the string representing the sum of variable ‘amount’ in SAS data set mydata

    35: 35 In-Class Assignment Start with a general SAS code to print out the solutions of X2 + 4X + 4 = 0 the solutions of aX2 + bX + c = 0

    36: 36 HW Assignment Write a SAS macro program to provide the solutions of aX2 + bX + c = 0 for varied a, b, and c Convert the previous code as a macro program with three parameters to take values of a, b, and c Celebrate with your first macro code What expected to see in the OUTPUT is Question Solution1 Solution2 X2 + 4X + 4 = 0 -2 -2 X2 + 5X + 4 = 0 -1 -4 X2 - 5X + 4 = 0 4 1

More Related