Why Use Macros

download Why Use Macros

of 38

Transcript of Why Use Macros

  • 8/8/2019 Why Use Macros

    1/38

  • 8/8/2019 Why Use Macros

    2/38

    2

    INTRODUCTION

    The SAS Macro Language allows you

    to extend and customize the SAS System

    to simplify large programs

    to reduce the amount of text you enter

    to invoke code several times

    to minimize the mistakes you might make

    The macro language is a powerful additionto SAS, allowing increased efficiency and

    customization of SAS jobs

  • 8/8/2019 Why Use Macros

    3/38

    3

    INTRODUCTION

    Macro programming is generally

    considered an advanced topic

    But, while macros certainly can bechallenging, it is also true that the basic

    concepts are not difficult to learn

  • 8/8/2019 Why Use Macros

    4/38

    4

    INTRODUCTION

    This course is designed for students

    who know the basics of SAS

    programming, but know nothing aboutSAS macro programming

    As we wont start from the very

    beginning, if you are not a SAS user at

    all, then it is not a course for you

  • 8/8/2019 Why Use Macros

    5/38

    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

  • 8/8/2019 Why Use Macros

    6/38

    6

    INTRODUCTION

    Because macro code takes longer to

    write and debug than standard SAS

    code, you generally wont use macrosin 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

  • 8/8/2019 Why Use Macros

    7/38

    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 datadriven, letting SAS decide what to do based

    on actual data values

  • 8/8/2019 Why Use Macros

    8/38

    8

    The most important concept to keep in

    mind whenever you write macro code

    is that

    THE MACRO PROCESSOR

    macro

    statementsstandard

    SAS statements

    macro

    processor

    You are writing a program

    that writes a program

  • 8/8/2019 Why Use Macros

    9/38

    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

  • 8/8/2019 Why Use Macros

    10/38

    10

    INTRODUCTION

    Before SAS can compile and execute

    your program, SAS must pass your

    macro statements to the macroprocessor 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

  • 8/8/2019 Why Use Macros

    11/38

    11

    Lets do exercises

    Scenario

    25 data sets with one same variable named

    TEMP for year 1981 2006

    YR1981, YR1982, . , and YR2006

    Find mean TEMP for each year

    PROC MEANS DATA=yr1981;

    VAR temp;

    RUN;

  • 8/8/2019 Why Use Macros

    12/38

    12

    INTRODUCTION

    The SAS macro language consists of

    macro variables,

    macro programs, macro facility interfaces, and

    macro storage techniques

  • 8/8/2019 Why Use Macros

    13/38

    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, eitherlocal or global in scope; the value is always

    simply a string of characters

  • 8/8/2019 Why Use Macros

    14/38

    14

    MACRO VARIABLES

    Macro variables come in two varieties: eitherlocal or global

    A macro variables 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 usea local macro variable only inside its ownmacro.

  • 8/8/2019 Why Use Macros

    15/38

    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

  • 8/8/2019 Why Use Macros

    16/38

    16

    MACROS vs. MACRO

    VARIABLES

    A macro variable is like a standard

    data variable except that it does not

    belong to a data set and has only asingle 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

  • 8/8/2019 Why Use Macros

    17/38

    17

    MACROS vs. MACRO

    VARIABLES

    The names of macro variables start with anampersand (&), while the names of macrosstart with a percent sign (%)

    The % and & characters are called macrotriggers; the character following the macrotrigger must not be a space

    %let dset = MASTER ;PROC PRINT DATA=&dset ;

    RUN;

  • 8/8/2019 Why Use Macros

    18/38

  • 8/8/2019 Why Use Macros

    19/38

    19

    USER-DEFINED MACRO

    VARIABLES

    the %LET statement enables you to define a

    macro variable and assign it a value %LETvariable = 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 thecurrent value

  • 8/8/2019 Why Use Macros

    20/38

    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

  • 8/8/2019 Why Use Macros

    21/38

    21

    SYSTEM-DEFINED MACRO

    VARIABLES

    some system-defined macro variables

    NAME VALUE

    SYSDATE date of SAS invocation

    SYSDAY day of the week of SAS invocation

    SYSTIME time of SAS invocation

    SYSSCP operation system being used

    SYSVER release of SAS system being used

    SYSERR return code set by last DATA or PROC step

    SYSLAST name of most recently created SAS data set in the form oflibref.name. If no data set was created then the value is

    _NULL_ .

  • 8/8/2019 Why Use Macros

    22/38

    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";

    Footnote " HW1 by YUFEN LI on 06NOV06 18:45 "

  • 8/8/2019 Why Use Macros

    23/38

    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 apercent sign (%)

  • 8/8/2019 Why Use Macros

    24/38

    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

  • 8/8/2019 Why Use Macros

    25/38

    25

    TYPE OF MACRO

    PARAMET

    ERS positional parameters receive their values in the

    order (position) in which they are specified in themacro invocation positional parameters, if any, must appear first in the

    parameter list

    keyword parameters are those which start with thename of the macro variable followed by an = symboland 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;

  • 8/8/2019 Why Use Macros

    26/38

    26

    MORE FLEXIBILITIES

    The following macro definition has onepositional parameteropts (with no = sign)and one keyword parameterfilenm (with an

    = sign)

    %macro mname (opts,filenm=file5);

    proc print data=&filenm &opts;

    title " Print &filenm data ";run;

    %mend;

  • 8/8/2019 Why Use Macros

    27/38

    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)

  • 8/8/2019 Why Use Macros

    28/38

    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

  • 8/8/2019 Why Use Macros

    29/38

    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 linewill be replaced by the code from the macroprogram itself, which has the requiredsemicolons

    Specify the MPRINT and MLOGIC options togenerate as much information as possiblefrom the macro processor

  • 8/8/2019 Why Use Macros

    30/38

    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 iseasy to see the standard SAS statements

    your macro is generating

  • 8/8/2019 Why Use Macros

    31/38

    31

    OPTION MLOGIC

    Use MLOGIC to debug macros

    If MLOGIC is in effect and the macroprocessor encounters a macro invocation,

    the macro processor displays messages thatidentify 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

  • 8/8/2019 Why Use Macros

    32/38

    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.

  • 8/8/2019 Why Use Macros

    33/38

    33

    MACRO FACILITY

    INTERFACES

    2) Indirect References using multipleampersands. A double ampersand (&&) inSAS code is resolved to a single ampersand

    If we have a macro variable var1 with avalue of char1, and another macro variablechar1 with a value of Taiwan, then a string

    in the SAS code of &&&var1 is resolved to&char1 on the first pass, and that isresolved to Taiwan on the second pass

  • 8/8/2019 Why Use Macros

    34/38

    34

    MACRO FACILITY

    INTERFACES

    3) PROC SQL; SELECT INTO :macrovariable;

    proc sql;

    select sum(amount) into:total from data.overall;

    run;

    This defines a macro variable total with a

    value that is the string representing the sum of

    variable amount in SAS data set data.overall

  • 8/8/2019 Why Use Macros

    35/38

    35

    STORAGE TECHNIQUES

    There are three ways to store macro programsfor future use:

    1. Store the source code in an external file

    and use this statement to pull it into a SASprogram:

    %include(file-pathname);

    2. Use the Autocall Facility to searchpredefined source libraries for macrodefinitions. These libraries can consist ofexternal files or SAS catalogs.

  • 8/8/2019 Why Use Macros

    36/38

    36

    STORAGE TECHNIQUES

    3. Store the compiled macros. This does NOTsave the SOURCE code. Always maintainthe source code separately

    To store the compiled macro :libname dlib library-path;

    options mstored sasmstore=dlib;

    %macro mname(parameters) / store

    des=description;

    %mend;

    To access this stored macro :libname dlib library-path;

    options mstored sasmstore=dlib;

    %mname(parameters)

  • 8/8/2019 Why Use Macros

    37/38

    37

    In-Class Assignment

    Write a SAS macro program to provide the

    solutions of aX2 + bX + c = 0 for varied a, b,

    and c

    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

  • 8/8/2019 Why Use Macros

    38/38

    38

    In-Class Assignment

    1. Start with a general SAS code to print

    out the solutions of X2 + 4X + 4 = 0

    2. Convert the previous code as amacro program with three parameters

    to take values of a, b, and c

    3. Celebrate with your first macro code