adv. sas(macro)

Upload: ds

Post on 07-Aug-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/20/2019 Adv. SAS(Macro)

    1/25

    SAS Training

  • 8/20/2019 Adv. SAS(Macro)

    2/25

    Macro

    • Used to reduce the amount of code entered to perform common tasks

    • Used for tasks that are repeated in number of programs or number of different places within aprogram.

    • It is used to automatically generate

     – SAS statements and commands

     – write messages to the SAS log

     –  Accept input

     – Create and change the values of macro variables

    • Macro facility has two components

    1. Macro Processor – A part of SAS system does the Macro processing

    2. Macro Language – Syntax that communicates to Macro Processor 

  • 8/20/2019 Adv. SAS(Macro)

    3/25

    Two delimiters trigger macro processor activity:

    • &name - macro variable.

     – Replacing Text Strings Using Macro Variables

     – The form &name is called a macro variable reference.

    • %name - refers to a macro

     – Generating SAS Code Using Macros

     – The form %name is called a macro call

    • The text substitution produced by the macro processor is completed before the program text iscompiled and executed

    • Macro language elements can only trigger text subs t i tut ion and are not present during programor command execution

  • 8/20/2019 Adv. SAS(Macro)

    4/25

    Defining Macro

    •  A SAS program can contain any number of macros

    • can invoke a macro any number of times in a single program

    • Should always start with a %macro statement and end with a %mend statement

    Syntax

    %MACRO macro-name;

    macro definition

    %MEND macro-name;

    where,

    • macro-name is the name of the macro. Each macro should have a distinct name, which is subjectto the standard SAS naming conventions.

    • Macro definition can be any valid SAS step

  • 8/20/2019 Adv. SAS(Macro)

    5/25

    Example

    %MACRO print;

    PROC PRINT DATA = ipl.bowlers;

    RUN;

    %MEND;

    • Defining a macro called print for printing the data set bowlers.

  • 8/20/2019 Adv. SAS(Macro)

    6/25

    Calling a Macro

    • %name - refers to a macro

    • The form %name is called a macro call.

    Syntax

    %macro-name;

    where,

    • macro-name is the name of the macro, which is to be called.

    Example :- %print;

    • %print will call the previously defined macro and print the dataset candy.

  • 8/20/2019 Adv. SAS(Macro)

    7/25

    Example

    %print;

    Obs name category

    1 Bhatia BW

    2 Vettori BW

    3 Maharoof BW

    4 Geeves BW

    5 McGrath BW

  • 8/20/2019 Adv. SAS(Macro)

    8/25

    Passing Information into a Macro Using Parameters

    • Macro parameters are local macro variables whose values are specified while invoking themacro

    • Macro variables defined in parentheses in %MACRO statement

    • Enable to pass information into a macro

    • Two types

    • Positional Parameters

    • Keyword Parameters

    Syntax

    %macro macro-name(var1 ,var2,….varn );

    macro definition

    %mend macro-name;

    where,

    • var1, var2 - macro variables – stores the values of the parameters passed.Refer to a macrovariable as &name in the macro definition. The form &name is called a macro variable reference.

  • 8/20/2019 Adv. SAS(Macro)

    9/25

    Keyword Parameters

    • names one or more macro parameters followed by equal signs

    • can specify default values after the equal signs or specify the macro variable name followed byan equal sign and the value in the macro invocation

    Syntax

    %macro macro-name (var1 = [val1] ,var2 = ,….varn = );

    macro definition%mend macro-name;

    where,

    • var1, var2 - macro parameters for holding the values passed.• val1 is the default value to be stored in the parameter, to be used if no value is passed

  • 8/20/2019 Adv. SAS(Macro)

    10/25

    Example

    %macro players(match=1, innings=);

    proc print data=ipl.players noobs;

    var name type;where match=&match and innings=&innings;

    title “The names of the players for match &match and innings &innings”;

    run;

    %mend players;

    %players(innings=1);

    %players(innings=1,match=2);

    The names of the players for match 1 and innings 1

    name category

    Gambhir BT

    Dhawan BT

    The names of the players for match 2 and innings 1

    name category

    Karthik BT

  • 8/20/2019 Adv. SAS(Macro)

    11/25

    Positional Parameters

    • Names one or more macro parameters whose values will be specified while macro invocation

    • Specify the values in the same order its defined in the macro definition

    • If no value is specified while invocation, null value will be taken

    Syntax

    %macro macro-name (var1,var2 ,….varn );macro definition

    %mend macro-name;

    where,

    • var1, var2 - macro parameters for holding the values passed.

  • 8/20/2019 Adv. SAS(Macro)

    12/25

    Example

    %macro players(plname,cat);

    proc print data=ipl.players noobs;

    where name=&plname and category=&cat;tilte "&plname is a &cat";

    run;

    %mend players;

    %players ("Bhatia“,"BW");

    name category

    Bhatia BW

  • 8/20/2019 Adv. SAS(Macro)

    13/25

    Macro Variables

    •  Are tools that enable you to dynamically modify the text in a SAS program through symbolicsubstitution

    • Can assign large or small amounts of text to macro variables

    • Maximum length of 65,534 characters

    •  Are independent of SAS data set variables

    • Use the variable by referencing it with an ampersand preceding its name (&variable-name),anywhere in a SAS program

    • Macro variable references that are enclosed in single quotation marks are not resolved

    • Classified into two based on the scope of the variable

    • Global Macro Variable

    • Local Macro Variable

  • 8/20/2019 Adv. SAS(Macro)

    14/25

    • Can create a macro variable by using a let statement

    • Using a let statement inside a macro definition creates a local macro variable

    • Using a let statement outside a macro definition creates a global macro variable

    Syntax

    %let = ;

    where,

    • is a name for the macro variable, satisfying SAS rules

    • is the value to be assigned to the macro variable

    Example:

    %let playername = Tendulkar ;

    %let sum = (100 + 400) ;

  • 8/20/2019 Adv. SAS(Macro)

    15/25

    Global Macro Variables

    • Exist till the duration of the SAS session

    • Can be used within or outside a Macro

    • Created during anytime of the SAS session

    • Can assign a value using a let statement later 

    Syntax

    %gobal ;

    where,

    • is a name for the macro variable, satisfying SAS rules

    Example: %global playername;

  • 8/20/2019 Adv. SAS(Macro)

    16/25

    Local Macro Variables

    • Can be declared within the sas macro using let statement or using the keyword local

    • Exists till the execution of that macro in which the variable is created.

    • It can be accessed/changed within the creation of macro.

    Syntax

    %local ;

    where,

    • is a name for the macro variable, satisfying SAS rules

    Example

    %local player;

  • 8/20/2019 Adv. SAS(Macro)

    17/25

    Defining Arithmetic and Logical Expressions

    • can use arithmetic and logical expressions in specific macro functions and statements

    • Macro Language Elements that Evaluate Arithmetic and Logical Expressions• %DOmacro-variable=expression %TO expression;

    • %DO %UNTIL(expression);

    • %DO %WHILE(expression);

    • %EVAL (expression);

    • %IF expression %THEN statement ;

    • %SCAN(argument,expression,)• %SUBSTR(argument,expression)

    • %SYSEVALF(expression,conversion-type)

  • 8/20/2019 Adv. SAS(Macro)

    18/25

    Example

    %let a=%eval(1+2);

    %let b=%eval(10*3);

    %let a=%sysevalf(10.0*3.0);%let b=%sysevalf(10.5+20.8);

    %let address=123 maple avenue;

    %let frstword=%scan(&address,1);

    %let lincoln=Four score and seven;%let secondwd=%substr(&lincoln,6,5);

  • 8/20/2019 Adv. SAS(Macro)

    19/25

    Example

    %macro whatstep(info=,mydata=);

    %if &info=print %then

    %do;proc print data=&mydata;

    run;

    %end;

    %else %if &info=report %then

    %do;

    proc report data=&mydata nowd;

    column name category;title "DLF IPL 2008";

    run;

    %end;

    %mend whatstep;

    %whatstep (info=report, mydata=player)

  • 8/20/2019 Adv. SAS(Macro)

    20/25

    SYMPUT Routine

    •  Assigns a value produced in a DATA step to a macro variable

    Syntax

    CALL SYMPUT(macro-variable, value);

    where,

    • macro-variable can be a character string that is a SAS name, enclosed in quotation marks orname of a character variable whose values are SAS names

    • data team1;

    • input position : $8. player : $12.;

    • call symput(position,player);

    • datalines;

    • shortstp Ann• pitcher Tom

    • frstbase Bill

    • ;

  • 8/20/2019 Adv. SAS(Macro)

    21/25

    Example

    %let sr_cit = no;

    data senior;

    set census;if age > 65 then

    do;

    call symput ("sr_cit", yes);

    output;

    end;

    run;

    data team1;

    input position : $8. player : $12.;

    call symput(position,player);

    datalines;

    shortstp Ann

    pitcher Tomfrstbase Bill

    ;

    run;

  • 8/20/2019 Adv. SAS(Macro)

    22/25

     Automatic Macro Variable

    • Macro processor creates automatic macro variables that supply information related to the SASsession when SAS is invoked

    • Use %PUT _AUTOMATIC_ to view all available automatic macro variables

    Example

    SYSDAY

    SYSDATE

    SYSMACRONAME

  • 8/20/2019 Adv. SAS(Macro)

    23/25

    User-Defined Macro Variable

    • Macro variables created by the user 

    • Use %PUT _USER_ to view all available user-defined macro variables

    Example

    %let city = Bangalore;

    %let player = Tendulkar;

    %let sum = %eval(100 + 400);

    • City, player and sum are macro variables

  • 8/20/2019 Adv. SAS(Macro)

    24/25

    Displaying Macro Variable Values

    • Use the %PUT statement to display macro variable values

    • Values will be displayed in log window

    Syntax

    %put &macro-variable;

    Example

    %put &player;

    %put ∑

  • 8/20/2019 Adv. SAS(Macro)

    25/25

    Including external macros

    • Macros can be stored in external file can be used again

    %include “c:\files\name.sas”;

    %players

    %players (print=0)