Your SlideShare is downloading. ×
Chapter 3. Macro Facility Interfaces Venkata Maguluri
Creating Macro Variables in the DATA Step Creating Macro Variables in the DATA Step
Objectives <ul><li>Create macro variables during DATA step execution. </li></ul><ul><li>Describe the difference between th...
Introduction <ul><li>The action of the %LET statements is performed  during  the word scanning phase.  </li></ul><ul><li>D...
Introduction DATA REVENUE; SET PERM.ALL END=FINAL; WHERE COURSE_NUMBER=3; TOTAL+1; IF PAID=’Y’ THEN PAIDUP+1; IF FINAL THE...
The SYMPUT Routine The DATA step offers functions and routines that enable transfer of information between an  executing  ...
The SYMPUT routine creates a macro variable and assigns it a value. General form of the SYMPUT routine: CALL SYMPUT ( macr...
CALL SYMPUT ( macro-variabl e,  text ); You can specify  macro-variable  or   text   as a <ul><li>literal </li></ul><ul><l...
The SYMPUT Routine with Literals <ul><li>first argument to specify an exact name for the  name  of the macro variable </li...
The SYMPUT Routine with Literals <ul><li>This demonstration illustrates conditionally assigning a value to a macro variabl...
You can  copy the value  of a DATA step variable into a macro variable by using the  DATA step variable’s name  as the sec...
CALL SYMPUT (' macro-variable ',  DATA-step-variable ); This form of the SYMPUT routine  creates  the macro variable named...
<ul><li>a maximum of  32767  characters can be assigned to the receiving macro variable </li></ul><ul><li>any leading or t...
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 <ul><li>This demonstration illustrates using the SYMPUT routine with a literal and a ...
SYMPUT with DATA Step Expressions <ul><li>left-align character strings created by numeric-to-character conversions </li></...
<ul><li>numeric expressions are  automatically converted  to character using BEST12. format </li></ul><ul><li>you can assi...
SYMPUT with DATA Step Expressions <ul><li>This demonstration illustrates using the SYMPUT routine with DATA step expressio...
PUT Function <ul><li>perform  explicit  numeric-to-character conversions </li></ul><ul><li>format the result of a numeric ...
PUT Function General form of the PUT function: PUT ( sourc e,  forma t) source is a constant, variable, or expression (num...
SYMPUT with DATA Step Expressions <ul><li>This demonstration illustrates using the SYMPUT routine with DATA step expressio...
Computing Statistics for Later Use You want to generate the following chart with titles and footnotes containing current i...
Computing Statistics for Later Use You need to compute the mean enrollment for each location, as well as for all courses. ...
Computing Statistics for Later Use <ul><li>This demonstration illustrates computing statistics to be used in a bar chart w...
Indirect References to Macro Variables Creating Macro Variables in the DATA Step
Objectives <ul><li>Create a series of macro variables during DATA step execution. </li></ul><ul><li>Reference macro variab...
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 <ul><li>This demonstration illustrates how to use SYMPUT to create multiple ...
<ul><li>efficiency </li></ul><ul><li>reduced coding. </li></ul>Instead of executing separate DATA steps to update the same...
Use the SYMPUT routine with DATA step expressions for both arguments to create multiple macro variables. General form of t...
CALL SYMPUT ( expression1 , expression2 ); expression1  evaluates to a character value that    is a valid macro variable n...
Example: Create one macro variable for each value of the DATA step variable COURSE. Assign the corresponding value of TITL...
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(co...
%let crsid=C002; proc print data=perm.schedule noobs label; where course_code=&quot;&crsid&quot;; var location begin_date ...
The code for the  PROC PRINT  steps is almost identical.  The only difference is the  reference  to different macro variab...
You need the following code: %let crsid=C002; proc print data=perm.schedule noobs label; where course_code=&quot;&crsid&qu...
Indirect References to Macro Variables <ul><li>resolve the macro variable CRSID  (C002 ) </li></ul><ul><li>attach  &  to t...
Indirect References to Macro Variables This sequence seems to imply that you should use the reference  &&crsid  to convert...
<ul><li>Multiple ampersands or percent signs preceding a name token cause the macro processor to rescan the reference. </l...
Indirect References to Macro Variables Forward Rescan Rule: <ul><li>To rescan a reference, the macro processor scans and r...
Indirect References to Macro Variables Use  three ampersands  in front of a  macro variable name  when its value matches t...
Indirect References to Macro Variables Variable  Value C001  Basic Telecommunications C002   Structured Query Language C00...
Indirect References to Macro Variables The  C002  portion is obtained through a previous scan from the reference  &crsid ....
Indirect References to Macro Variables Example: Illustrate the impact of  &&  and  &&&  preceding a macro variable name. &...
Indirect References to Macro Variables Therefore, the original reference must be  &&&crsid . Example: Illustrate the impac...
Indirect References to Macro Variables <ul><li>This demonstration illustrates using indirect referencing to improve the la...
Indirect referencing is especially useful in applications where you need a  series  of related macro variables. You can us...
Example: Create a series of macro variables    TEACH1 to TEACH n , each containing    the name of the instructor assigned ...
Variable  Value TEACH1  Hallis, Dr. George TEACH2  Wickam, Dr. Alice TEACH3  Forest, Mr. Peter . . . CRS  3 1st scan  2nd ...
The SQL Interface
Objectives <ul><li>Create macro variables during PROC SQL execution. </li></ul><ul><li>Store several values in one macro v...
In the SQL procedure, you use the INTO clause in a SELECT statement to specify one or more macro variables to create (or u...
Example: Create a macro variable named TOTFEE that contains the total of all course fees.  Use this macro variable in a la...
The INTO Clause <ul><li>This demonstration illustrates using the INTO clause to create a macro variable. </li></ul>The SQL...
One form of the INTO clause creates one new macro variable per row in the result of the SELECT statement: General form of ...
Create ranges of macro variables that contain the course code, location, and starting date of the first two courses schedu...
Create ranges of macro variables that contain the course code, location, and starting date of the first two courses schedu...
Display the macro variable values in the SAS log. %put First course of 1996: &crsid1 at &place1 on &date1; %put Second cou...
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 vari...
Creating a Delimited List of Values Another form of the INTO clause takes the values of a column and concatenates them int...
Creating a Delimited List of Values Example: Use the SQL procedure to create one macro variable named SITES that contains ...
Creating a Delimited List of Values Use the new macro variable in a title. proc means data=perm.all sum maxdec=0;  var fee...
Obtaining Macro Variable Values During Execution
Objectives <ul><li>Obtain the value of a macro variable during DATA step execution. </li></ul><ul><li>Describe the differe...
You can obtain a macro variable’s value during DATA step execution using the SYMGET function. Program Data Vector DATA Ste...
General form of the SYMGET function: SYMGET ( macro-variabl e) macro-variable  can be specified as a <ul><li>character lit...
In Release 6.12 and  before , the SYMGET function  truncated any macro variable value longer than 200 characters.  This is...
What type of applications require the use of the SYMGET function instead of simple macro variable referencing? The SYMGET ...
data _null_; set perm.schedule; call   symput(’teach’||left(course_number), trim(teacher)); run; data teachers; set perm.r...
Variable  Value TEACH1  Hallis, Dr. George TEACH2  Wickam, Dr. Alice TEACH3   Forest, Mr. Peter Symbol Table Obtaining Mac...
proc print data=teachers; var student_name course_number teacher; title1 &quot;Teacher for Each Registered Student&quot;; ...
<ul><li>This demonstration illustrates how to use SYMGET to obtain the value of a different macro variable for each DATA s...
Using SYMGET with Views <ul><li>compiled DATA steps stored with the Stored Program Facility </li></ul><ul><li>DATA step vi...
Using SYMGET with Views Frequently, the macro variable referenced by the SYMGET function has  yet to be created  or has  n...
SQL coding is placed into an input stack and word scanning is performed for macro triggers in the same process as other SA...
The macro variable reference  &CRSID  is resolved  during the creation  of the SQL view, resulting in a  constant value  w...
SQL supports the SYMGET function. This enables views to  look up  macro variable values when   views are used . Obtaining ...
SQL does not perform automatic data conversion.  You must use the INPUT function to convert the macro variable value to nu...
The SCL Interface (Self-Study)
Objectives <ul><li>Describe how SCL interfaces with the macro facility. </li></ul>The SCL Interface
SAS/AF, SAS/FSP, and SAS/CALC software support SAS Component Language (SCL) programs. SCL programs are placed into an inpu...
MAIN: erroroff wage; if wage gt &max then erroron wage; return; The macro variable MAX is resolved during SCL  compilation...
SCL offers two routines to assign macro variable values during SCL  execution . Rules regarding the use of literals or exp...
SCL offers two functions to obtain macro variable values during SCL  execution . Rules regarding the use of literals or ex...
Example: Obtain the value of the MAX macro  variable during  execution  of the SCL program. MAIN: erroroff wage; if wage g...
One common application for these routines and functions is to pass information between entries. INIT: SCL code return; MAI...
INIT: SCL code return; MAIN: SCL code return; TERM: call symput(’x’,dsn); return; INIT: dsn=symget(’x’); return; MAIN: ret...
Upcoming SlideShare
Loading in...5
×

SAS Macros part 3

1,690

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,690
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "SAS Macros part 3"

  1. 1. Chapter 3. Macro Facility Interfaces Venkata Maguluri
  2. 2. Creating Macro Variables in the DATA Step Creating Macro Variables in the DATA Step
  3. 3. Objectives <ul><li>Create macro variables during DATA step execution. </li></ul><ul><li>Describe the difference between the SYMPUT routine and the %LET statement. </li></ul>Creating Macro Variables in the DATA Step
  4. 4. Introduction <ul><li>The action of the %LET statements is performed during the word scanning phase. </li></ul><ul><li>During word scanning, the %LET statement following the ELSE DO statement replaces the value of FOOT assigned previously. </li></ul><ul><li>The DATA step program that reaches the compiler contains no statements that affect the value of the macro variable FOOT. </li></ul><ul><li>When the DATA step executes , the value of FOOT is always All Students Paid (or the value from the last %LET statement). </li></ul>Creating Macro Variables in the DATA Step
  5. 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 &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
  6. 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. 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. 8. CALL SYMPUT ( macro-variabl e, text ); You can specify macro-variable or text as a <ul><li>literal </li></ul><ul><li>DATA step variable </li></ul><ul><li>DATA step expression. </li></ul>If macro-variable already exists, the value of text replaces the old value. The SYMPUT Routine Creating Macro Variables in the DATA Step
  9. 9. The SYMPUT Routine with Literals <ul><li>first argument to specify an exact name for the name of the macro variable </li></ul><ul><li>second argument to specify the exact character value to assign to the macro variable. </li></ul>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
  10. 10. The SYMPUT Routine with Literals <ul><li>This demonstration illustrates conditionally assigning a value to a macro variable based on DATA step values. </li></ul>Creating Macro Variables in the DATA Step
  11. 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. 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. 13. <ul><li>a maximum of 32767 characters can be assigned to the receiving macro variable </li></ul><ul><li>any leading or trailing blanks that are part of the DATA step variable’s value are stored in the macro variable . </li></ul>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
  14. 14. You need to generate this report. SYMPUT with a Literal and a Variable Creating Macro Variables in the DATA Step
  15. 15. SYMPUT with a Literal and a Variable <ul><li>This demonstration illustrates using the SYMPUT routine with a literal and a variable. </li></ul>Creating Macro Variables in the DATA Step
  16. 16. SYMPUT with DATA Step Expressions <ul><li>left-align character strings created by numeric-to-character conversions </li></ul><ul><li>remove extraneous leading and trailing blanks. </li></ul>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
  17. 17. <ul><li>numeric expressions are automatically converted to character using BEST12. format </li></ul><ul><li>you can assign a maximum of 32767 characters to the receiving macro variable </li></ul><ul><li>any leading or trailing blanks that are part of the expression are stored in the macro variable. </li></ul>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
  18. 18. SYMPUT with DATA Step Expressions <ul><li>This demonstration illustrates using the SYMPUT routine with DATA step expressions. </li></ul>Creating Macro Variables in the DATA Step
  19. 19. PUT Function <ul><li>perform explicit numeric-to-character conversions </li></ul><ul><li>format the result of a numeric expression. </li></ul>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. <ul><li>the width of the resulting string </li></ul><ul><li>whether the string is right- or left-aligned. </li></ul>Creating Macro Variables in the DATA Step
  20. 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. 21. SYMPUT with DATA Step Expressions <ul><li>This demonstration illustrates using the SYMPUT routine with DATA step expressions. </li></ul>Creating Macro Variables in the DATA Step
  22. 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. 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
  24. 24. Computing Statistics for Later Use <ul><li>This demonstration illustrates computing statistics to be used in a bar chart with dynamically assigned title, footnote, and reference line position. </li></ul>Creating Macro Variables in the DATA Step
  25. 25. Indirect References to Macro Variables Creating Macro Variables in the DATA Step
  26. 26. Objectives <ul><li>Create a series of macro variables during DATA step execution. </li></ul><ul><li>Reference macro variables indirectly using multiple ampersands for delayed resolution. </li></ul>Creating Macro Variables in the DATA Step
  27. 27. Recall these data sets from the course data: continued... Creating Macro Variables in the DATA Step
  28. 28. Creating Macro Variables in the DATA Step
  29. 29. Creating Multiple Macro Variables with SYMPUT <ul><li>This demonstration illustrates how to use SYMPUT to create multiple macro variables. </li></ul>Creating Macro Variables in the DATA Step
  30. 30. <ul><li>efficiency </li></ul><ul><li>reduced coding. </li></ul>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
  31. 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. 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. 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. 34. SAS Log Creating Multiple Macro Variables with SYMPUT Creating Macro Variables in the DATA Step
  35. 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=&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
  36. 36. %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
  37. 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. 38. 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
  39. 39. Indirect References to Macro Variables <ul><li>resolve the macro variable CRSID (C002 ) </li></ul><ul><li>attach & to the front of the resolved value (&C002 ) </li></ul><ul><li>resolve the resulting macro variable reference (Structured Query Language ). </li></ul>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
  40. 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. 41. <ul><li>Multiple ampersands or percent signs preceding a name token cause the macro processor to rescan the reference. </li></ul><ul><li>Two ampersands (&&) resolve to one ampersand (&). </li></ul>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
  42. 42. Indirect References to Macro Variables Forward Rescan Rule: <ul><li>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. </li></ul><ul><li>After the initial scan, a second scan begins left to right from the point where multiple ampersands or percent signs are coded. </li></ul><ul><li>If a macro reference contains four or more &s, additional scans are performed from left to right until no more triggers can be resolved. </li></ul>Creating Macro Variables in the DATA Step
  43. 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. 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. 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. 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. 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. 48. Indirect References to Macro Variables <ul><li>This demonstration illustrates using indirect referencing to improve the last application. </li></ul>Creating Macro Variables in the DATA Step
  49. 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. 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. 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
  52. 52. The SQL Interface
  53. 53. Objectives <ul><li>Create macro variables during PROC SQL execution. </li></ul><ul><li>Store several values in one macro variable using the SQL procedure. </li></ul>The SQL Interface
  54. 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. 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. 56. The INTO Clause <ul><li>This demonstration illustrates using the INTO clause to create a macro variable. </li></ul>The SQL Interface
  57. 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. 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. 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. 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. 61. Partial SAS Log Creating Multiple Variables with the INTO Clause The SQL Interface
  62. 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. 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. 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. 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 &quot;Total Revenue&quot;; title2 ”from Course Sites: &sites&quot;; run; The SQL Interface
  66. 66. Obtaining Macro Variable Values During Execution
  67. 67. Objectives <ul><li>Obtain the value of a macro variable during DATA step execution. </li></ul><ul><li>Describe the difference between the SYMGET function and macro variable references. </li></ul>Obtaining Macro Variable values during Execution
  68. 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. 69. General form of the SYMGET function: SYMGET ( macro-variabl e) macro-variable can be specified as a <ul><li>character literal </li></ul><ul><li>DATA step character expression. </li></ul>Obtaining Macro Variable values during Execution
  70. 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. 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. 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. 73. Variable Value TEACH1 Hallis, Dr. George TEACH2 Wickam, Dr. Alice TEACH3 Forest, Mr. Peter Symbol Table Obtaining Macro Variable values during Execution
  74. 74. 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
  75. 75. <ul><li>This demonstration illustrates how to use SYMGET to obtain the value of a different macro variable for each DATA step iteration. </li></ul>Applications for SYMGET Obtaining Macro Variable values during Execution
  76. 76. Using SYMGET with Views <ul><li>compiled DATA steps stored with the Stored Program Facility </li></ul><ul><li>DATA step views </li></ul><ul><li>SQL views </li></ul><ul><li>Screen Control Language (SCL) programs. </li></ul>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
  77. 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. 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= &quot;&crsid&quot;; quit; Input Stack Obtaining Macro Variable values during Execution
  79. 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. 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. 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
  82. 82. The SCL Interface (Self-Study)
  83. 83. Objectives <ul><li>Describe how SCL interfaces with the macro facility. </li></ul>The SCL Interface
  84. 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. 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. 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. 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. 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. 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
  90. 90. 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; <ul><li>In this diagram, </li></ul><ul><li>global macro variable X is given the value of the SCL variable DSN as the user leaves entry A </li></ul><ul><li>SCL variable DSN in entry is initialized with the value of the macro variable X. </li></ul>Entry A Entry B The SCL Interface

×