introduction to sas macro language

Upload: vkeyur

Post on 30-May-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Introduction to SAS Macro Language

    1/5

    I n t roduc t i on to

    SAS Mac ro Language

    Bios 524: Biostatistical Computing

    Bios 524: Introduction to SAS Macro Language 2

    Get t ing He lp !

    Use the SAS OnLine Documentation for

    help on this subject. Follow this path:

    l Base SAS Software

    l SAS Macro Language Reference

    l Introduction follow these pages to learn aboutthe macro facility

    l Macro Language Dictionary find help on allmacro statements, functions, etc.

    Bios 524: Introduction to SAS Macro Language 3

    What i s t he SAS Mac r o

    F ac i l i t y?

    From the OnLineDoc:

    l The macro facility is a tool for extending andcustomizing the SAS System and for reducing theamount of text you must enter to do commontasks.

    l The macro facility allows you to assign a name tocharacter strings or groups of SAS programmingstatements. From that point on, you can work with

    the names rather than with the text itself.

    Bios 524: Introduction to SAS Macro Language 4

    What i s t he SAS Mac ro

    Fac i l i t y?

    From the OnLine Doc:

    l When you use a macro facility name in aSAS program or from a command prompt,the macro facility generates SASstatements and commands as needed.

    l The rest of the SAS System receivesthose statements and uses them in the

    same way it uses the ones you enter in thestandard manner.

    Bios 524: Introduction to SAS Macro Language 5

    What i s t he SAS Mac r o

    F ac i l i t y?

    Two components

    l Macro Processor

    l This compiles your macro and integrates it withyour SAS job.

    l Macro Language

    l This is how you communicate with the macroprocessor.

    Bios 524: Introduction to SAS Macro Language 6

    T r igge ri ng t he Mac r o

    Pr ocesso r

    Two delimiters will trigger the macroprocessor in a SAS program

    l &namel This refers to a macro variable. The current

    value of the variable will replace &name.

    l %namel This refers to a macro, which may generate a

    section of a statement, one or more completeSAS statements, or even whole data or procsteps.

  • 8/14/2019 Introduction to SAS Macro Language

    2/5

    Bios 524: Introduction to SAS Macro Language 7

    Defin ing and Using Mac ro

    Variab les

    %let

    l Example%let keyvar = DOEntry;

    libname library "c:\bios524\classlib";

    proc print data=classlib.clinics;

    id clinicid;

    var &keyvar;

    proc freq data=classlib.clinics;

    tables &keyvar;

    run;

    DOEntry is assigned to

    macro variable keyvar.Leading and trailing

    blanks are ignored.

    As the proc

    step is

    compiled,

    &keyvar isreplaced with

    DOEntry.

    Bios 524: Introduction to SAS Macro Language 8

    Mac ro Var iab le Va lues

    Values are character strings

    No distinction is made between numeric and

    character type.l However, see the macro function %eval.

    Embedded special symbols require the use ofa macro quote function when assigning orusing macro variables.

    l See macro functions %str, %nstr, %quote,%nquote, to mention a few.

    Bios 524: Introduction to SAS Macro Language 9

    Rec ogn iz ing a Mac ro

    Var iab le

    The key is the leading &.

    SAS views &leadvar and &leadvar1as twodifferent macro variables.l %let leadvar = x;

    l &leadvar resolves to x.

    l &leadvar1 is not resolved to x1. An error message mayappear.

    When the end of the macro variable is not clear,

    delimit it with a .l &leadvar.1 resolves to x1.

    l Note: &leadvar..1 resolves to x.1.

    Bios 524: Introduction to SAS Macro Language 10

    Resolv ing Mac ro

    Var iab le w i th in Quo tesExamplel %Let project = Assignment 4;

    l Title Results for &project;l Resolves to Results for &project .

    l Title Results for &project;l Resolves to Results for Assignment 4 .

    Example%Let refd = 01JAN2000;

    %Let dob = 12APR1955;age = int((intck("month","&dob"d,"&refd"d) -

    (day("&refd"d)

  • 8/14/2019 Introduction to SAS Macro Language

    3/5

    Bios 524: Introduction to SAS Macro Language 13

    SAS Mac ros

    Define a SAS macro using the basic

    syntax

    Example

    Usage:

    %MACRO macro-name ;

    macro definition%MEND macro-name ;

    %macro whereby;

    where (age ge 18 and eligible=1);

    by ClinicId;

    %mend whereby;

    proc print data=Clinics;

    %whereby

    run;

    Bios 524: Introduction to SAS Macro Language 14

    Pr o d u c i n g SA S c o d e w i t h

    M a c r o s

    %DO%TO; %END;%macro loopit;

    %let var1 = Age;

    %let var2 = Height;%let var3 = Weight;

    %do i = 1 %to 3;proc means;

    var &&var&i;Title "Analysis for the Variable &&var&i";

    %end;

    %mend loopit;

    data one;input age height weight @@;datalines;

    34 60 130 45 70 201 50 68 188;

    %loopitrun;

    First time through the

    loop:

    1. Resolves to &var1

    2. Resolves to Age

    This generates:

    proc means;

    varAge;

    Title "Analysis for the

    Variable Age";

    proc means;

    var Height;Title "Analysis for the

    Variable Height";

    proc means;

    varWeight;

    Title "Analysis for the

    Variable Weight";

    Bios 524: Introduction to SAS Macro Language 15

    Produc i ng SAS c ode w i t h

    M a c r o s

    %IF%THEN; %ELSE;

    %macro wantrslt;

    %let results = %upcase(&giverslt);

    %if &results = YES %then %do;

    proc means;var _numeric_;Title "Results for Numeric Variables";

    %end;

    %else %put No results requested; %* Appears in log;

    %mend wantrslt;

    %let giverslt = NO;

    %wantrslt

    Convert to

    upper case

    Placestext in

    SAS log.

    Macrocomment

    Bios 524: Introduction to SAS Macro Language 16

    Pass ing Paramet e rs t o

    Macros

    Character values may be passed to

    parameters that are local macro

    variables.

    Syntax %MACRO macro-name (parm1, parm2, , pa rmk);macro definition

    %MEND macro-name ;

    Bios 524: Introduction to SAS Macro Language 17

    Pass ing Paramet e rs t o

    MacrosExample

    %macro wantrslt (giverslt);

    %let results = %upcase(&giverslt);

    %if &results = YES %then %do;

    proc means;var _numeric_;Title "Results for Numeric Variables";

    %end;

    %else %put No results requested; %* Appears in log;

    %mend wantrslt;

    %wantrslt(no);

    %wantrslt(yes);

    Local macro variable

    giverslt is defined.

    Values are passed to the

    local macro variable

    giverslt .

    Bios 524: Introduction to SAS Macro Language 18

    Pass ing Paramet e rs t o

    Macros : An A l te rna t i ve

    MethodCharacter values may be passed to

    named parameters.

    l The named parameters may be placed inany order.

    l If omitted, the parameter receives a defaultvalue (that may be null).

    %MACRO macro-name (parm1=deflt1, parm2=deflt2, , par mk=defltk);

    macro definition

    %MEND macro-name ;

  • 8/14/2019 Introduction to SAS Macro Language

    4/5

    Bios 524: Introduction to SAS Macro Language 19

    Pass ing Paramet e rs t o

    Macros : An A l te rna t i ve

    MethodExample

    %macro agecalc (dob=, refd=01JAN2000);

    %if &dob= %then %do;%put Date of birth is missing;

    age = .;

    %end;

    %else

    age = int((intck("month","&dob"d,"&refd"d) -

    (day("&refd"d)

  • 8/14/2019 Introduction to SAS Macro Language

    5/5

    Bios 524: Introduction to SAS Macro Language 25

    Macro Er ro r Messages

    and Debugg ing

    OnLine Documentation

    l Errorshttp://www.at.vcu.edu/manuals/sas/sashtml/macro/z1302436.htm

    l Debugginghttp://www.at.vcu.edu/manuals/sas/sashtml/macro/z1066200.htm

    Bios 524: Introduction to SAS Macro Language 26

    Se lec t Macro Func t ions

    and Ca l l Rout ines

    CALL SYMPUT(macro-variable,value);l http://www.at.vcu.edu/manuals/sas/sashtml/macro/z0210266.htm#znid-364

    l

    Cautions:l A macro reference resolves when the data or proc step is

    compiled, but symput assigns a value to the macro variable

    during execution. Thus you cannot refer to that macro

    variable in the same step.

    SYMGET(argument)l http://www.at.vcu.edu/manuals/sas/sashtml /macro/z0210322 .htm

    l Use this to assign the value of a macro variable to a data step variable. Thisassignment takes place during execution.