• Save
SAS Macros part 1
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to like this
No Downloads

Views

Total Views
2,787
On Slideshare
2,753
From Embeds
34
Number of Embeds
1

Actions

Shares
Downloads
0
Comments
6
Likes
0

Embeds 34

http://sasandstats.blogspot.com 34

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. SAS/MACROS
  • 2.
    • Chapter 1.
    • Overview of the Macro Facility
    • Venkata Maguluri
  • 3. Applications of the Macro Facility Section 1.1 Applications of the Macro Facility
  • 4. Objectives
    • Identify different applications of the SAS macro facility.
    Applications of the Macro Facility
  • 5. Purpose of the Macro Facility
    • Using the macro language, you can write SAS programs that are dynamic , or capable of self-modification.
    • Specifically, the macro language enables you to
      • create and resolve macro variables anywhere in a SAS program
      • write special programs ( macros ) that generate tailored SAS code.
    Applications of the Macro Facility
  • 6. Displaying System Information
    • Using the macro language, you can utilize automatic macro variables that contain information regarding your system environment.
    • For example, some of these variables contain
      • date and time of SAS session
      • version of SAS
      • operating system.
    Applications of the Macro Facility
  • 7. Displaying System Information 1 2 2 2 2 1 3 5 4 5 2 3 4 1 time of day day of week date (day, month, and year) operating system release of SAS software Applications of the Macro Facility
  • 8. Substitute Information Multiple Times The macro facility can substitute the same user-defined information into multiple locations within a single program. Example: Substitute a four-digit year of interest into multiple locations in a program. proc print data=perm.schedule; where year(begin_date)= Year-of-Interest ; title "Scheduled Classes for Year-of-Interest "; run; proc means data=perm.all sum; where year(begin_date)= Year-of-Interest ; class location; var fee; title "Total Fees for Year-of-Interest Classes"; title2 "by Training Center"; run; Applications of the Macro Facility
  • 9. Conditional Processing The macro facility controls whether certain portions of a SAS program are processed based on specific conditions. Example: Generate the detailed registration report on a daily basis, but generate the revenue summary report only on Friday. Applications of the Macro Facility
  • 10. Conditional Processing Is it Friday? Yes Always Print the Daily Report Applications of the Macro Facility
  • 11. Repetitive Processing The macro facility generates portions of a SAS program repetitively, making each iteration perform differently. Example: Generate the same summary report for each year between 2000 and 2002. Applications of the Macro Facility
  • 12. Repetitive Processing Applications of the Macro Facility
  • 13. Repetitive Processing Applications of the Macro Facility
  • 14. Data-driven Applications To summarize a different time period on an enrollment report, certain portions of the report require modification : Starting date - explicitly specified by user Ending date - explicitly specified by user Total number of students during time period - dynamically determined Average class size during time period - dynamically determined. 1 2 3 4 Applications of the Macro Facility
  • 15. Data-driven Applications 4 1 2 3 Applications of the Macro Facility
  • 16. Tips on Writing Macro-based Programs
    • If a macro-based program is used to generate SAS code,
      • write and debug the desired SAS program without any macro coding
      • make sure the SAS program runs with hard-coded programming constants on a fixed set of data.
    Program Flow
  • 17. Section 1.2 Program Flow
  • 18. Objectives
    • Identify the tokens in a SAS program.
    • Describe how a SAS program is tokenized, compiled, and executed.
    Program Flow
  • 19. Compilation and Execution
    • A SAS program can be any combination of
      • DATA steps and PROC steps
      • global statements
      • Screen Control Language (SCL)
      • Structured Query Language (SQL)
      • SAS macro language.
    When you submit a program, it goes to an area of memory called the input stack . Program Flow
  • 20. Compilation and Execution Input Stack data new; set perm.mast; bonus=wage*1.1; run; proc print; run; libname perm ‘.’; options nodate; proc sql; select * from perm.mast; MAIN: erroroff wage; if wage gt 20 then erroron wage; return; Display Manager SUBMIT Command SCL COMPILE Command Batch or Noninteractive Submission Program Flow
  • 21. Compilation and Execution
    • Once SAS code is in the input stack, the SAS System
      • reads the text in the input stack (left-to-right, top-to-bottom)
      • routes text to the appropriate compiler upon demand
      • suspends this activity when a step boundary is reached
      • executes the compiled code if there are no compilation errors
      • repeats this process as necessary.
    Program Flow
  • 22. Compilation and Execution data new; set perm.mast; bonus=wage *1.1; run; proc print; run; Compiler SAS System Input Stack Program Flow
  • 23. Tokenization
    • A component of SAS known as the word scanner breaks program text into fundamental units called token s .
      • Tokens are passed on demand to the compiler.
      • The compiler requests tokens until it receives a semicolon.
      • The compiler performs a syntax check on the statement.
    Program Flow
  • 24. Tokenization
    • How does SAS know when to stop sending statements to the compiler?
    • How would processing be affected if the RUN statement were omitted for
      • a noninteractive submission
      • an interactive submission?
    Compiler data new; SAS Word Scanner Input Stack bonus=wage*1.1; proc print; run; set perm . mast ; Program Flow
  • 25. Tokenization The word scanner recognizes four classes of tokens: literals are a string of characters treated as a unit. The string is enclosed in single quotes or double quotes. ’ Any text’ "Any text" numbers are a string of digits (integers). Date, time, datetime constants, and hexadecimal constants are also integer tokens. 23 3 100 ’01jan2002’d are strings of digits that also include a period or E-notation (real numbers). 23.5 3. .11 5E8 7.2E-4 Program Flow
  • 26.
    • names consist of a string of characters beginning with a letter or underscore and continuing with underscores, letters, or digits. (A period can sometimes be part of a name.)
    • infile _n_ item3 univariate dollar10.2
    • special is any character or group of characters that have reserved meaning to the compiler.
    • * / + - ** ; $ ( ) . & %
    • A token ends when the word scanner detects
      • the beginning of another token
      • a blank after a token.
    • The maximum length of any token is 32767 characters.
    Tokenization Program Flow
  • 27. Tokenization: Examples 1. Blanks are not tokens. One or more blanks only serve to separate tokens. 2. The text of a literal is treated as a unit by the word scanner when it is enclosed in single quotes. Input Stack Tokens var x1-x10 z ; (1) VAR (2) X1 (3) - (4) X10 (5) Z (6) ; Input Stack Tokens title 'Report for May'; (1) TITLE (2) 'Report for May' (3) ; Program Flow
  • 28. Tokenization: Exercise 1.1 How many tokens are present in each of these statements? input @10 ssn comma11. name $30-50; bonus=3.2*(wage-2000); plot date*revenue='$'/vref='30jun2001'd; Program Flow
  • 29. Tokenization: Exercise Answers How many tokens are present in each of these statements? input @10 ssn comma11. name $30-50; bonus=3.2*(wage-2000); plot date*revenue=‘$’/vref=‘30jun2001’d; 11 10 11 Program Flow
  • 30. Section 1.3 Macro Processing
  • 31. Objectives
    • Describe how the macro processor affects program flow.
    Micro Processing
  • 32. Macro Triggers The macro processor is a part of the macro facility that acts upon certain token sequences detected during word scanning: % followed by a name token (example: %let ) & followed by a name token (example: &amt ) Each of these token sequences is called a macro trigge r . Micro Processing
  • 33. Macro Triggers
    • When the word scanner encounters a macro trigger, the macro processor
      • examines those tokens
      • requests additional tokens if necessary
      • performs the action indicated.
    Micro Processing
  • 34. How the Macro Processor Works Macro Processor Input Stack SYSDAY SYSLAST Tuesday _NULL_ Symbol Table ... Compiler Word Scanner %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; proc print; run; Micro Processing
  • 35. Macro Processor SYSDAY SYSLAST Tuesday _NULL_ Symbol Table When a macro trigger is encountered, it is passed to the macro processor for evaluation. %let ... Input Stack How the Macro Processor Works Compiler Word Scanner amt=1.1; data new; set perm.mast; bonus=wage*&amt; proc print; run; Micro Processing
  • 36. How the Macro Processor Works Macro Processor data new; set perm.mast; bonus=wage*&amt; proc print; run; %let amt=1.1; %LET is the keyword of a macro statement that creates a macro variable. The macro processor requests tokens until a semicolon is encountered. ... Compiler Word Scanner Input Stack Micro Processing SYSDAY SYSLAST Tuesday _NULL _ Symbol Table
  • 37. When the %LET statement executes, a macro variable AMT is given the value 1.1 and stored in a memory location called a symbol tabl e . ... Macro Processor Input Stack Compiler Word Scanner data new; set perm.mast; bonus=wage*&amt; proc print; run; How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL _ 1.1 Symbol Table
  • 38. ; proc print; run; Word scanning continues until another macro trigger is found. data new; set perm.mast; bonus=wage* &amt ... Macro Processor Compiler Word Scanner Input Stack How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
  • 39. The trigger &amt is called a macro variable reference . The macro processor attempts to find the AMT variable in the symbol table. ... Macro Processor Compiler Word Scanner Input Stack data new; set perm.mast; bonus=wage* ; proc print; run; &amt How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
  • 40. Word scanning continues. If the AMT variable is found, its value is passed back to the input stack. ... Compiler Word Scanner Input Stack Macro Processor data new; set perm.mast; bonus=wage* 1.1 ; proc print; run; How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
  • 41. Macro Processor When a step boundary is recognized, the DATA step compilation phase ends and execution begins. ; run; proc print ... Compiler Word Scanner Input Stack data new; set perm.mast; bonus=wage*1.1 How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
  • 42. The macro processor may write messages to the SAS log if it cannot act upon a macro trigger. Example: Suppose the macro variable reference was coded as &ant instead of &amt . Macro Processor &ant WARNING: Apparent symbolic reference ANT not resolved. Input Stack data new; set perm.mast; bonus=wage* Compiler Word Scanner ; proc print; run; How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
  • 43. If the macro processor cannot interpret the trigger, 1. it passes the tokens back to the word scanner 2. the word scanner passes them to the compiler 3. the DATA step compiler writes an error message. ERROR: Expecting a variable name. ... Compiler Word Scanner Input Stack data new; set perm.mast; bonus=wage *&amt ; ; proc print; run; Macro Processor How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
  • 44. The %INCLUDE Statement The special token pair %include requests that SAS statements stored in an external file be inserted at that location in the input stack. External File %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; Input Stack %include ' external-file '; proc print; run; Micro Processing
  • 45.
    • file-specification describes the location of the SAS code to be inserted: ' external-file ' is the physical name of the file.
      • fileref is the file reference supplied through a host command or FILENAME statement.
    • SOURCE2 causes the inserted SAS statements to appear in the SAS log.
    The %INCLUDE statement retrieves SAS source code from an external file. General form of the %INCLUDE statement: %INCLUDE file-specification < / SOURCE2 >; The %INCLUDE Statement Micro Processing
  • 46. The word scanner encounters the % and include tokens, passes them to the macro processor that passes the tokens to the %INCLUDE handling routines. External File %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; Word Scanner Macro Processor %INCLUDE Handling Routines ‘ external file’; proc print; run; %include Input Stack ... The %INCLUDE Statement Micro Processing
  • 47. The %INCLUDE handling routines obtain the external file specification. External File %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; Word Scanner Macro Processor %INCLUDE Handling Routines proc print; run; %include ‘ external file ’; Input Stack ... The %INCLUDE Statement Micro Processing
  • 48. The contents of the external file are placed into the input stack. The word scanner begins to read the newly inserted statements. External File %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; Word Scanner Macro Processor %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; proc print; run; Input Stack Symbol Table SYSDAY Tuesday SYSLAST _NULL_ ... The %INCLUDE Statement Micro Processing
  • 49.