5. 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 "Paid Status for Course 3"; FOOTNOTE "All Students Paid"; RUN; DATA step program processed by the compiler: Creating Macro Variables in the DATA Step
6. 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
7. 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
8.
9.
10.
11. 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
12. 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
13.
14. You need to generate this report. SYMPUT with a Literal and a Variable Creating Macro Variables in the DATA Step
15.
16.
17.
18.
19.
20. 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
21.
22. 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
23. 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
31. 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
32. 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
33. 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
34. SAS Log Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
35. 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="&crsid"; var location begin_date teacher; title1 "Schedule for &c005 "; run; continued... Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
36. %let crsid=C002; proc print data=perm.schedule noobs label; where course_code="&crsid"; var location begin_date teacher; title1 "Schedule for &c002 "; run; Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
37. 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
38. You need the following code: %let crsid=C002; proc print data=perm.schedule noobs label; where course_code="&crsid"; var location begin_date teacher; title1 "Schedule for ?????"; run; But what should the value of ????? be? Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
39.
40. 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
41.
42.
43. 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
44. 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
45. 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
46. 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
47. 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
48.
49. 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
50. 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
51. 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
54. 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
55. 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
56.
57. 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
58. 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
59. 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
60. 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
61. Partial SAS Log Creating Multiple Variables with the INTO Clause The SQL Interface
62. 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
63. 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
64. 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
65. 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 "Total Revenue"; title2 ”from Course Sites: &sites"; run; The SQL Interface
68. 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
69.
70. 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
71. 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
72. 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
73. Variable Value TEACH1 Hallis, Dr. George TEACH2 Wickam, Dr. Alice TEACH3 Forest, Mr. Peter Symbol Table Obtaining Macro Variable values during Execution
74. proc print data=teachers; var student_name course_number teacher; title1 "Teacher for Each Registered Student"; run; Obtaining Macro Variable values during Execution
75.
76.
77. 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
78. 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= "&crsid"; quit; Input Stack Obtaining Macro Variable values during Execution
79. 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
80. SQL supports the SYMGET function. This enables views to look up macro variable values when views are used . Obtaining Macro Variable values during Execution
81. 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
84. 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
85. 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
86. 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
87. 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
88. 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
89. 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