1 / 20

SAS Macro

Introduction to SAS Macro. It consists of purpose, creation and application of macro in SAS.

Download Presentation

SAS Macro

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. G - SAS Macro – Part 1 By- Kavita Dargan

  2. Agenda • Purpose of Macro Facility • Types of macro variables – Automatic vs User defined • Different ways of creating macro variable • Global vs. Local symbol table • Combining macro variable reference with text • Questions?

  3. Purpose of Macro Facility • Using the macro language, you can write SAS programs that are dynamic, or capable of self modification. • Write special programs called macros that generate tailored SAS code. • You can create and resolve macro variables anywhere in a SAS program. • You can utilize automatic macro variables that contain information regarding your system environment.

  4. Types of Macro Variables • User-defined variables • Create own macro variable. • If already exists then replaces the current value. • Leading and trailing blanks are removed before assignment. • Can be created using %Let, Into clause, Call symput etc • Use %put _user_ in program to view all available user-defined macro variables in log. • Automatic variables • Created at SAS invocation • Global (always available) • Usually assigned by SAS but some can be by user • Example- Sysdate, Systime, Sysver, Sysday etc • Use %put _automatic_ in program to view all available automatic macro variables in log.

  5. Types of Macro Variables Contd. • Example: Substitute system information in footnote. • Output: footnote1 "Created &systime &sysday, &sysdate9"; footnote2 "on the &sysscp system using Release &sysver";

  6. Different ways of creating macro variables • %LET • Variable can be any name following the SAS naming convention. • If variable already exists in the symbol table, value replaces the current value. • If either variable or value contains a macro trigger (&), it is evaluated before the assignment is made. • Rules for value: • Maximum length is 32 characters and minimum length is 0 characters. • Numeric tokens are stored as character strings and case is reserved. • Mathematical expressions not evaluated. • Quotes are stored as values. • Leading and trailing blanks are removed before assignment which are not in quotes. %LET variable=value;

  7. Different ways of creating macro variables Contd. • Examples of %LET statement Value Ed Norton ’ Ed Norton ’ “Joan’s Report” 0 3+4 varlist name age height %let name= Ed Norton ; %let name2=’ Ed Norton ’; %let title="Joan’s Report"; %let start=; %let total=0; %let sum=3+4; %let total=&total+∑ %let x=varlist; %let &x=name age height;

  8. Different ways of creating macro variables Contd. • SYMPUT Routine using Literal • First argument specify name for the macro variable • Second argument specify the character value to be assigned to the macro variable. • If macro-variable already exists, the value of the text replaces the old value. CALL SYMPUT('macro-variable', 'text');

  9. Different ways of creating macro variables Contd. • SYMPUT Routine using Data-step-variable • Current value stored in data-step-variable to be assigned to the macro variable. • If macro-variable already exists, the value of the data-step-variable replaces the old value. • Maximum of 32767 characters can be assigned to the receiving macro variable. • Any leading or trailing blanks stored in value are assigned to macro variable. CALL SYMPUT(‘macro-variable’, Data-step-variable);

  10. Different ways of creating macro variables Contd. • SYMPUT Routine using Data-step-expression • Value of the expression to be assigned to the macro variable. • If macro-variable already exists, the value of the expression replaces the old value. • Maximum 32767 characters can be assigned to the receiving macro variable. • Numeric expression are automatically converted to character using BEST12. format. • One can remove leading and trailing blanks in a variable by this method. CALL SYMPUT(‘macro-variable’, expression);

  11. Different ways of creating macro variables Contd. • SYMPUT Routine to create multiple macro variables • Expression1 evaluates to a character value that is a valid macro variable name. This value should change each time you want to create another macro variable. • Expression2 is the value you want to assign to a specific macro variable. • This way, we can create multiple macro variables in one data step. • Advantage • Efficiency • Reduced coding CALL SYMPUT(expression1, expression2);

  12. Different ways of creating macro variables Contd. • Example data _null_; set courses; call symput(course_code,trim(course_title)); run; %put _user_; Note: There are 6 observations in dataset courses.

  13. Different ways of creating macro variables Contd. • INTO Clause • One can specify one or more macro variables to create. • Create new macro variables per row in the result of select statement. • This method does not trim leading or trailing blanks. SELECT col1, col2,. . . INTO :mvar1 - :mvarn, :nvar1 - :nvarn,... FROM table-expression WHERE where-expression other clauses;

  14. Different ways of creating macro variables Contd. • INTO Clause example • Create ranges of macro variables that contain the course code, location, and starting date of the first two courses scheduled in 2002. proc sqlnoprint; select course_code,location,begin_date format=mmddyy10. into :crsid1-:crsid2, :place1-:place2, :date1-:date2 from schedule where year(begin_date)=2002 order by begin_date; quit; SQL Result

  15. Different ways of creating macro variables Contd. • Another form of INTO Clause • It takes the values of a column and concatenates them into one macro variable, there by creating delimited list of values. Example, SELECT col1, . . . INTO :mvarSEPARATED BY ’delimiter’,... FROM table-expression WHERE where-expression other clauses; proc sqlnoprint; select distinct location into :sites separated by ‘ ‘ from schedule; quit; Result: Macro variable sites resolves to Boston Dollas Seattle

  16. Global symbol table vs. Local symbol table • Macro defined by %global, variable is held in global symbol table. • Whenever the SAS System is invoked, a global symbol table is created and initialized with automatic or system-defined macro variables. • You can create user-defined global macro variables with the %LET, Call SYMPUT, INTO clause statement if it is defined outside a macro definition (called open code). • Macro defined by %local, variable is held in local symbol table. • Whenever a macro variable is defined within a macro and is not defined as global, variable is held in local symbol table and it exists only during execution of the macro in which it is defined. • Variables declared in a parameter list on a macro call are always stored in a local symbol table. Note: When you define macro with no arguments, local symbol table is not created and all the variables defined inside the macro will be global and can be accessible inside as well as outside the macro.

  17. Global vs. Local symbol table Contd. • Example %let x=0; %macro outer; %local x; %let x=1; %inner; %mend outer; %macro inner; %local y; %let y=&x; %mend local;

  18. Combining macro variable with text • In general, macro variable reference is made by preceding the macro variable name with an ampersand (&). • Referencing a nonexistent macro variable results in a warning message. WARNING: Apparent symbolic reference xxxxx not resolved. • Macro variable can be reference adjacent to leading and/or trailing text. i.e. text&variable, &variable.text, text&variable.text • Macro variable can be refence adjacent to another macro variable reference. i.e. &variable1&variable2

  19. Combining macro variable with text %let graphics=g; %let month=jan; %let var=sale; Libnameperm ‘SAS data library’; Proc gchart data=perm.y90jan; Plot sale*day; Run; • Example %let lib=perm; %let year=90; Libname &lib ‘SAS data library’; Proc &graphics.chart data=&lib..y&year&month; Plot &var*day; Run;

  20. Questions

More Related