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

SAS Macros part 3

on

  • 1,787 views

 

Statistics

Views

Total Views
1,787
Views on SlideShare
1,779
Embed Views
8

Actions

Likes
4
Downloads
0
Comments
0

1 Embed 8

http://sasandstats.blogspot.com 8

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

SAS Macros part 3 SAS Macros part 3 Presentation Transcript

  • Chapter 3. Macro Facility Interfaces Venkata Maguluri
  • Creating Macro Variables in the DATA Step Creating Macro Variables in the DATA Step
  • Objectives
    • Create macro variables during DATA step execution.
    • Describe the difference between the SYMPUT routine and the %LET statement.
    Creating Macro Variables in the DATA Step
  • Introduction
    • The action of the %LET statements is performed during the word scanning phase.
    • During word scanning, the %LET statement following the ELSE DO statement replaces the value of FOOT assigned previously.
    • The DATA step program that reaches the compiler contains no statements that affect the value of the macro variable FOOT.
    • When the DATA step executes , the value of FOOT is always All Students Paid (or the value from the last %LET statement).
    Creating Macro Variables in the DATA Step
  • Introduction DATA REVENUE; SET PERM.ALL END=FINAL; WHERE COURSE_NUMBER=3; TOTAL+1; IF PAID=’Y’ THEN PAIDUP+1; IF FINAL THEN DO; PUT TOTAL= PAIDUP=; IF PAIDUP< TOTAL THEN DO; END; ELSE DO; END; END; RUN; PROC PRINT DATA=REVENUE; VAR STUDENT_NAME STUDENT_COMPANY PAID; TITLE &quot;Paid Status for Course 3&quot;; FOOTNOTE &quot;All Students Paid&quot;; RUN; DATA step program processed by the compiler: Creating Macro Variables in the DATA Step
  • The SYMPUT Routine The DATA step offers functions and routines that enable transfer of information between an executing DATA step and the macro processor. You can use the SYMPUT routine to assign any value available to the DATA step to a macro variable. DATA step variables DATA step expressions character literals Symbol Table SYMPUT Creating Macro Variables in the DATA Step
  • The SYMPUT routine creates a macro variable and assigns it a value. General form of the SYMPUT routine: CALL SYMPUT ( macro-variable, text ); macro-variable is assigned the character value of text . continued... The SYMPUT Routine Creating Macro Variables in the DATA Step
  • CALL SYMPUT ( macro-variabl e, text ); You can specify macro-variable or text as a
    • literal
    • DATA step variable
    • DATA step expression.
    If macro-variable already exists, the value of text replaces the old value. The SYMPUT Routine Creating Macro Variables in the DATA Step
  • The SYMPUT Routine with Literals
    • first argument to specify an exact name for the name of the macro variable
    • second argument to specify the exact character value to assign to the macro variable.
    In the SYMPUT routine, use a literal for the Using literals with the SYMPUT routine: CALL SYMPUT (' macro-variable ', ' text '); Creating Macro Variables in the DATA Step
  • The SYMPUT Routine with Literals
    • This demonstration illustrates conditionally assigning a value to a macro variable based on DATA step values.
    Creating Macro Variables in the DATA Step
  • You can copy the value of a DATA step variable into a macro variable by using the DATA step variable’s name as the second argument to the SYMPUT routine. Using a DATA step variable as the second argument of the SYMPUT routine: CALL SYMPUT (' macro-variable ', DATA-step-variable ); continued... Creating Macro Variables in the DATA Step
  • CALL SYMPUT (' macro-variable ', DATA-step-variable ); This form of the SYMPUT routine creates the macro variable named by macro-variable and assigns it the current value of DATA-step-variabl e. SYMPUT with a Literal and a Variable Creating Macro Variables in the DATA Step
    • a maximum of 32767 characters can be assigned to the receiving macro variable
    • any leading or trailing blanks that are part of the DATA step variable’s value are stored in the macro variable .
    When a DATA step variable is used as the second argument, SYMPUT with a Literal and a Variable Creating Macro Variables in the DATA Step
  • You need to generate this report. SYMPUT with a Literal and a Variable Creating Macro Variables in the DATA Step
  • SYMPUT with a Literal and a Variable
    • This demonstration illustrates using the SYMPUT routine with a literal and a variable.
    Creating Macro Variables in the DATA Step
  • SYMPUT with DATA Step Expressions
    • left-align character strings created by numeric-to-character conversions
    • remove extraneous leading and trailing blanks.
    CALL SYMPUT ( 'macro-variable ', expressio n); Before the SYMPUT routine executes, you might want to use DATA step functions to In these situations you can use a DATA step expression as the second argument of the SYMPUT routine: Creating Macro Variables in the DATA Step
    • numeric expressions are automatically converted to character using BEST12. format
    • you can assign a maximum of 32767 characters to the receiving macro variable
    • any leading or trailing blanks that are part of the expression are stored in the macro variable.
    When you use a DATA step expression as the second argument, its current value is evaluated to a character constant: SYMPUT with DATA Step Expressions Creating Macro Variables in the DATA Step
  • SYMPUT with DATA Step Expressions
    • This demonstration illustrates using the SYMPUT routine with DATA step expressions.
    Creating Macro Variables in the DATA Step
  • PUT Function
    • perform explicit numeric-to-character conversions
    • format the result of a numeric expression.
    You can use the PUT function to The format determines The PUT function returns the character string formed by writing a value with a specified format.
    • the width of the resulting string
    • whether the string is right- or left-aligned.
    Creating Macro Variables in the DATA Step
  • PUT Function General form of the PUT function: PUT ( sourc e, forma t) source is a constant, variable, or expression (numeric or character). format is any SAS format or user-defined format. Creating Macro Variables in the DATA Step
  • SYMPUT with DATA Step Expressions
    • This demonstration illustrates using the SYMPUT routine with DATA step expressions.
    Creating Macro Variables in the DATA Step
  • Computing Statistics for Later Use You want to generate the following chart with titles and footnotes containing current information. Creating Macro Variables in the DATA Step
  • Computing Statistics for Later Use You need to compute the mean enrollment for each location, as well as for all courses. To do this, you must first know how many students were in each course at each location. Which data set(s) do you need to use to get the relevant information? Creating Macro Variables in the DATA Step
  • Computing Statistics for Later Use
    • This demonstration illustrates computing statistics to be used in a bar chart with dynamically assigned title, footnote, and reference line position.
    Creating Macro Variables in the DATA Step
  • Indirect References to Macro Variables Creating Macro Variables in the DATA Step
  • Objectives
    • Create a series of macro variables during DATA step execution.
    • Reference macro variables indirectly using multiple ampersands for delayed resolution.
    Creating Macro Variables in the DATA Step
  • Recall these data sets from the course data: continued... Creating Macro Variables in the DATA Step
  • Creating Macro Variables in the DATA Step
  • Creating Multiple Macro Variables with SYMPUT
    • This demonstration illustrates how to use SYMPUT to create multiple macro variables.
    Creating Macro Variables in the DATA Step
    • efficiency
    • reduced coding.
    Instead of executing separate DATA steps to update the same macro variable, you can create related macro variables in one DATA step. Advantages include Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
  • Use the SYMPUT routine with DATA step expressions for both arguments to create multiple macro variables. General form of the SYMPUT routine with DATA step expressions: CALL SYMPUT ( expression1 , expression2 ); continued... Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
  • CALL SYMPUT ( expression1 , expression2 ); 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. Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
  • Example: Create one macro variable for each value of the DATA step variable COURSE. Assign the corresponding value of TITLE to each macro variable. data _null_; set perm.courses; call symput(course_code, trim(course_title)); run; %put _user_; Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
  • SAS Log Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
  • Example: Resolve the macro variables for specified values of COURSE_CODE. data _null_; set perm.courses; call symput(course_code,trim(course_title)); run; %let crsid=C005; proc print data=perm.schedule noobs label; where course_code=&quot;&crsid&quot;; var location begin_date teacher; title1 &quot;Schedule for &c005 &quot;; run; continued... Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
  • %let crsid=C002; proc print data=perm.schedule noobs label; where course_code=&quot;&crsid&quot;; var location begin_date teacher; title1 &quot;Schedule for &c002 &quot;; run; Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
  • The code for the PROC PRINT steps is almost identical. The only difference is the reference to different macro variables in the TITLE statement. This solution can be improved if the value of the CRSID macro variable could also reference one of the macro variables C001, C002, and so on. Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
  • You need the following code: %let crsid=C002; proc print data=perm.schedule noobs label; where course_code=&quot;&crsid&quot;; var location begin_date teacher; title1 &quot;Schedule for ?????&quot;; run; But what should the value of ????? be? Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
  • Indirect References to Macro Variables
    • resolve the macro variable CRSID (C002 )
    • attach & to the front of the resolved value (&C002 )
    • resolve the resulting macro variable reference (Structured Query Language ).
    To obtain the value Structured Query Language , you need the macro variable reference &c002 . If the value of the macro variable CRSID is C002 , then you need to proceed in several steps: Creating Macro Variables in the DATA Step
  • Indirect References to Macro Variables This sequence seems to imply that you should use the reference &&crsid to convert the value of the macro variable CRSID into the corresponding course description. Creating Macro Variables in the DATA Step
    • Multiple ampersands or percent signs preceding a name token cause the macro processor to rescan the reference.
    • Two ampersands (&&) resolve to one ampersand (&).
    Indirect References to Macro Variables There are two rules used for resolving macro variable references that show this is not the correct solution. Rescan Rule: Creating Macro Variables in the DATA Step
  • Indirect References to Macro Variables Forward Rescan Rule:
    • To rescan a reference, the macro processor scans and resolves until a token is encountered that cannot be part of the macro trigger being scanned.
    • After the initial scan, a second scan begins left to right from the point where multiple ampersands or percent signs are coded.
    • If a macro reference contains four or more &s, additional scans are performed from left to right until no more triggers can be resolved.
    Creating Macro Variables in the DATA Step
  • Indirect References to Macro Variables Use three ampersands in front of a macro variable name when its value matches the exact name of a second macro variable. This indirect reference resolves to the value of the second macro variable . Variable Value C001 Basic Telecommunications C002 Structured Query Language C003 Local Area Networks C004 Database Design C005 Artificial Intelligence C006 Computer Aided Design CRSID C002 Symbol Table Creating Macro Variables in the DATA Step
  • Indirect References to Macro Variables Variable Value C001 Basic Telecommunications C002 Structured Query Language C003 Local Area Networks C004 Database Design C005 Artificial Intelligence C006 Computer Aided Design CRSID C002 To get Structured Query Language , we need to resolve & C002 . Symbol Table Creating Macro Variables in the DATA Step
  • Indirect References to Macro Variables The C002 portion is obtained through a previous scan from the reference &crsid . This previous scan must also generate the preceding & ; to obtain & , we must code && . Therefore, the original reference must be &&&crsid . Creating Macro Variables in the DATA Step
  • Indirect References to Macro Variables Example: Illustrate the impact of && and &&& preceding a macro variable name. &crsid C002 1st scan &&crsid &crsid C002 1st scan 2nd scan &&&crsid &C002 Structured Query Language 1st scan 2nd scan Creating Macro Variables in the DATA Step
  • Indirect References to Macro Variables Therefore, the original reference must be &&&crsid . Example: Illustrate the impact of && and &&& preceding a macro variable name. &crsid &&crsid &&&crsid 1st scan C002 &crsid &C002 2nd scan C002 Structured Query Language reference Creating Macro Variables in the DATA Step
  • Indirect References to Macro Variables
    • This demonstration illustrates using indirect referencing to improve the last application.
    Creating Macro Variables in the DATA Step
  • Indirect referencing is especially useful in applications where you need a series of related macro variables. You can use two ampersands when the value of one macro variable matches part of the name of a second macro variable. For example COURSE1, COURSE2, and so on. Indirect References to Macro Variables Creating Macro Variables in the DATA Step
  • Example: Create a series of macro variables TEACH1 to TEACH n , each containing the name of the instructor assigned to a specific course. Reference one of these variables when a course number is designated. Indirect References to Macro Variables Creating Macro Variables in the DATA Step
  • Variable Value TEACH1 Hallis, Dr. George TEACH2 Wickam, Dr. Alice TEACH3 Forest, Mr. Peter . . . CRS 3 1st scan 2nd scan &&teach&crs &teach3 Forest,Mr. Peter Symbol Table Indirect References to Macro Variables Creating Macro Variables in the DATA Step
  • The SQL Interface
  • Objectives
    • Create macro variables during PROC SQL execution.
    • Store several values in one macro variable using the SQL procedure.
    The SQL Interface
  • In the SQL procedure, you use the INTO clause in a SELECT statement to specify one or more macro variables to create (or update). General form of the PROC SQL INTO clause: SELECT col1, col2, . . . INTO :mvar 1, :mvar 2,... FROM table-expression WHERE where-expression other clause s; This form of the INTO clause does not trim leading or trailing blanks. The SQL Interface
  • Example: Create a macro variable named TOTFEE that contains the total of all course fees. Use this macro variable in a later step. proc sql noprint; select sum(fee) format=dollar10. into :totfee from perm.all; quit; %let totfee=&totfee; The %LET statement removes any leading or trailing blanks that may be stored in the value of TOTFEE. The SQL Interface
  • The INTO Clause
    • This demonstration illustrates using the INTO clause to create a macro variable.
    The SQL Interface
  • One form of the INTO clause creates one new macro variable per row in the result of the SELECT statement: General form of the INTO clause for a range of macro variables: SELECT col1, . . . INTO :mvar1 - :mvarn ,... FROM table-expression WHERE where-expression other clause s; The SQL Interface
  • Create ranges of macro variables that contain the course code, location, and starting date of the first two courses scheduled in 2002. proc sql noprint; select course_code, location, begin_date format=mmddyy10. into :crsid1-:crsid2, :place1-:place2, :date1-:date2 from perm.schedule where year(begin_date)=2002 order by begin_date; quit; continued... Creating Variables with the INTO Clause The SQL Interface
  • Create ranges of macro variables that contain the course code, location, and starting date of the first two courses scheduled in 2002. SQL Result Set Creating Variables with the INTO Clause The SQL Interface
  • Display the macro variable values in the SAS log. %put First course of 1996: &crsid1 at &place1 on &date1; %put Second course of 1996: &crsid2 at &place2 on &date2; Creating Multiple Variables with the INTO Clause The SQL Interface
  • Partial SAS Log Creating Multiple Variables with the INTO Clause The SQL Interface
  • If you do not know how many macro variables will be created, you can perform a query to determine the number of macro variables needed and create a macro variable to store that number. You can then run the query using the macro variable as the suffix of the final macro variable in each set. The SQL Interface
  • Creating a Delimited List of Values Another form of the INTO clause takes the values of a column and concatenates them into one macro variable. General form of the INTO clause for combining values into one macro variable: SELECT col1, . . . INTO :mvar SEPARATED BY ’delimiter’,... FROM table-expression WHERE where-expression other clause s; The SQL Interface
  • Creating a Delimited List of Values Example: Use the SQL procedure to create one macro variable named SITES that contains the names of all training centers that appear in the PERM.SCHEDULE data set. The names should be separated by a blank. proc sql noprint; select distinct location into :sites separated by ' ' from perm.schedule; quit; SQL Result Set The SQL Interface
  • Creating a Delimited List of Values Use the new macro variable in a title. proc means data=perm.all sum maxdec=0; var fee; title &quot;Total Revenue&quot;; title2 ”from Course Sites: &sites&quot;; run; The SQL Interface
  • Obtaining Macro Variable Values During Execution
  • Objectives
    • Obtain the value of a macro variable during DATA step execution.
    • Describe the difference between the SYMGET function and macro variable references.
    Obtaining Macro Variable values during Execution
  • You can obtain a macro variable’s value during DATA step execution using the SYMGET function. Program Data Vector DATA Step Variables Symbol Table SYMGET The SYMGET function returns the value of an existing macro variable. Obtaining Macro Variable values during Execution
  • General form of the SYMGET function: SYMGET ( macro-variabl e) macro-variable can be specified as a
    • character literal
    • DATA step character expression.
    Obtaining Macro Variable values during Execution
  • In Release 6.12 and before , the SYMGET function truncated any macro variable value longer than 200 characters. This is no longer true with newer releases. However, a DATA step variable created by the SYMGET function is a character variable with a length of 200 bytes unless it has been previously defined . Obtaining Macro Variable values during Execution
  • What type of applications require the use of the SYMGET function instead of simple macro variable referencing? The SYMGET function can be used for table lookup applications. Example: Obtain the value of a different macro variable for each DATA step iteration. Obtaining Macro Variable values during Execution
  • data _null_; set perm.schedule; call symput(’teach’||left(course_number), trim(teacher)); run; data teachers; set perm.register; length teacher $ 20; teacher=symget(’teach’|| left(course_number)); run; Obtaining Macro Variable values during Execution
  • Variable Value TEACH1 Hallis, Dr. George TEACH2 Wickam, Dr. Alice TEACH3 Forest, Mr. Peter Symbol Table Obtaining Macro Variable values during Execution
  • proc print data=teachers; var student_name course_number teacher; title1 &quot;Teacher for Each Registered Student&quot;; run; Obtaining Macro Variable values during Execution
    • This demonstration illustrates how to use SYMGET to obtain the value of a different macro variable for each DATA step iteration.
    Applications for SYMGET Obtaining Macro Variable values during Execution
  • Using SYMGET with Views
    • compiled DATA steps stored with the Stored Program Facility
    • DATA step views
    • SQL views
    • Screen Control Language (SCL) programs.
    The SYMGET function is used when you want to obtain the value of a macro variable during run time instead of when a source program is compiled . This principle applies to Obtaining Macro Variable values during Execution
  • Using SYMGET with Views Frequently, the macro variable referenced by the SYMGET function has yet to be created or has no assigned value when the compiled DATA step, view, or SCL program is created. Obtaining Macro Variable values during Execution
  • SQL coding is placed into an input stack and word scanning is performed for macro triggers in the same process as other SAS programs. proc sql; create view subcrsid as select student_name, student_company,paid from perm.all where course_code= &quot;&crsid&quot;; quit; Input Stack Obtaining Macro Variable values during Execution
  • The macro variable reference &CRSID is resolved during the creation of the SQL view, resulting in a constant value whenever the view is used. Obtaining Macro Variable values during Execution
  • SQL supports the SYMGET function. This enables views to look up macro variable values when views are used . Obtaining Macro Variable values during Execution
  • SQL does not perform automatic data conversion. You must use the INPUT function to convert the macro variable value to numeric if it is compared to a numeric variable. Obtaining Macro Variable values during Execution
  • The SCL Interface (Self-Study)
  • Objectives
    • Describe how SCL interfaces with the macro facility.
    The SCL Interface
  • SAS/AF, SAS/FSP, and SAS/CALC software support SAS Component Language (SCL) programs. SCL programs are placed into an input stack and word scanning is performed for macro triggers in the same process as other SAS programs. MAIN: erroroff wage; if wage gt &max then erroron wage; return; The SCL Interface
  • MAIN: erroroff wage; if wage gt &max then erroron wage; return; The macro variable MAX is resolved during SCL compilation . A constant value will be compared to the SCL variable WAGE during SCL execution . The SCL Interface
  • SCL offers two routines to assign macro variable values during SCL execution . Rules regarding the use of literals or expressions for arguments are exactly the same as in the DATA step. General form of the SYMPUT and SYMPUTN SCL routines: CALL SYMPUT ( macro-variable,character valu e); CALL SYMPUTN ( macro-variable,numeric valu e); The SCL Interface
  • SCL offers two functions to obtain macro variable values during SCL execution . Rules regarding the use of literals or expressions for arguments are exactly the same as in the DATA step. General form of the SYMGET and SYMGETN SCL functions: cval = SYMGET ( macro-variabl e); nval = SYMGETN ( macro-variabl e); The SCL Interface
  • Example: Obtain the value of the MAX macro variable during execution of the SCL program. MAIN: erroroff wage; if wage gt symgetn(’max’) then erroron wage; return; The SCL Interface
  • One common application for these routines and functions is to pass information between entries. INIT: SCL code return; MAIN: SCL code return; TERM: call symput(’x’,dsn); return; INIT: dsn=symget(’x’); return; MAIN: return; SCL code TERM: SCL code return; Entry A Entry B The SCL Interface
  • INIT: SCL code return; MAIN: SCL code return; TERM: call symput(’x’,dsn); return; INIT: dsn=symget(’x’); return; MAIN: return; SCL code TERM: SCL code return;
    • In this diagram,
    • global macro variable X is given the value of the SCL variable DSN as the user leaves entry A
    • SCL variable DSN in entry is initialized with the value of the macro variable X.
    Entry A Entry B The SCL Interface