SlideShare a Scribd company logo
1 of 90
Chapter 3. Macro Facility Interfaces Venkata Maguluri
Creating Macro Variables in the DATA Step Creating Macro Variables in the DATA Step
Objectives ,[object Object],[object Object],Creating Macro Variables in the DATA Step
Introduction ,[object Object],[object Object],[object Object],[object Object],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 ,[object Object],[object Object],[object Object],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 ,[object Object],[object Object],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 ,[object Object],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
[object Object],[object Object],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 ,[object Object],Creating Macro Variables in the DATA Step
SYMPUT with DATA Step Expressions ,[object Object],[object Object],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
[object Object],[object Object],[object Object],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 ,[object Object],Creating Macro Variables in the DATA Step
PUT Function ,[object Object],[object Object],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. ,[object Object],[object Object],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 ,[object Object],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 ,[object Object],Creating Macro Variables in the DATA Step
Indirect References to Macro Variables Creating Macro Variables in the DATA Step
Objectives ,[object Object],[object Object],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 ,[object Object],Creating Macro Variables in the DATA Step
[object Object],[object Object],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 ,[object Object],[object Object],[object Object],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
[object Object],[object Object],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: ,[object Object],[object Object],[object Object],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 ,[object Object],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 ,[object Object],[object Object],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 ,[object Object],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 ,[object Object],[object Object],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 ,[object Object],[object Object],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
[object Object],Applications for SYMGET Obtaining Macro Variable values during Execution
Using SYMGET with Views ,[object Object],[object Object],[object Object],[object Object],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 ,[object Object],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; ,[object Object],[object Object],[object Object],Entry A Entry B The SCL Interface

More Related Content

What's hot

Sas macros part 4.1
Sas macros part 4.1Sas macros part 4.1
Sas macros part 4.1
venkatam
 
Base sas interview questions
Base sas interview questionsBase sas interview questions
Base sas interview questions
Dr P Deepak
 
Conditional statements in sas
Conditional statements in sasConditional statements in sas
Conditional statements in sas
venkatam
 
Introduction To Sas
Introduction To SasIntroduction To Sas
Introduction To Sas
halasti
 
Base SAS Exam Questions
Base SAS Exam QuestionsBase SAS Exam Questions
Base SAS Exam Questions
guestc45097
 

What's hot (20)

Data Match Merging in SAS
Data Match Merging in SASData Match Merging in SAS
Data Match Merging in SAS
 
Utility Procedures in SAS
Utility Procedures in SASUtility Procedures in SAS
Utility Procedures in SAS
 
Base SAS Statistics Procedures
Base SAS Statistics ProceduresBase SAS Statistics Procedures
Base SAS Statistics Procedures
 
Sas macros part 4.1
Sas macros part 4.1Sas macros part 4.1
Sas macros part 4.1
 
SAS Proc SQL
SAS Proc SQLSAS Proc SQL
SAS Proc SQL
 
SAS cheat sheet
SAS cheat sheetSAS cheat sheet
SAS cheat sheet
 
Basics Of SAS Programming Language
Basics Of SAS Programming LanguageBasics Of SAS Programming Language
Basics Of SAS Programming Language
 
Base sas interview questions
Base sas interview questionsBase sas interview questions
Base sas interview questions
 
Sas array statement
Sas array statementSas array statement
Sas array statement
 
Conditional statements in sas
Conditional statements in sasConditional statements in sas
Conditional statements in sas
 
Introduction To Sas
Introduction To SasIntroduction To Sas
Introduction To Sas
 
Where Vs If Statement
Where Vs If StatementWhere Vs If Statement
Where Vs If Statement
 
Sas practice programs
Sas practice programsSas practice programs
Sas practice programs
 
MYSQL
MYSQLMYSQL
MYSQL
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Pl sql-ch1
Pl sql-ch1Pl sql-ch1
Pl sql-ch1
 
Base SAS Exam Questions
Base SAS Exam QuestionsBase SAS Exam Questions
Base SAS Exam Questions
 
Estrategia de difusión de información estadística basada en PC-Axis
Estrategia de difusión de información estadística basada en PC-AxisEstrategia de difusión de información estadística basada en PC-Axis
Estrategia de difusión de información estadística basada en PC-Axis
 
Understanding SAS Data Step Processing
Understanding SAS Data Step ProcessingUnderstanding SAS Data Step Processing
Understanding SAS Data Step Processing
 
Proc report
Proc reportProc report
Proc report
 

Similar to SAS Macros part 3

Power of call symput data
Power of call symput dataPower of call symput data
Power of call symput data
Yash Sharma
 
The following steps illustrate how to write a simple Level-2 MATLAB .pdf
The following steps illustrate how to write a simple Level-2 MATLAB .pdfThe following steps illustrate how to write a simple Level-2 MATLAB .pdf
The following steps illustrate how to write a simple Level-2 MATLAB .pdf
anonakitchen
 
SAS Macros part 4.1
SAS Macros part 4.1SAS Macros part 4.1
SAS Macros part 4.1
venkatam
 
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdfDeclaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
malavshah9013
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdf
acsmadurai
 

Similar to SAS Macros part 3 (20)

Power of call symput data
Power of call symput dataPower of call symput data
Power of call symput data
 
Module 5.pdf
Module 5.pdfModule 5.pdf
Module 5.pdf
 
BAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 LectureBAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 Lecture
 
handout6.pdf
handout6.pdfhandout6.pdf
handout6.pdf
 
224-2009
224-2009224-2009
224-2009
 
The following steps illustrate how to write a simple Level-2 MATLAB .pdf
The following steps illustrate how to write a simple Level-2 MATLAB .pdfThe following steps illustrate how to write a simple Level-2 MATLAB .pdf
The following steps illustrate how to write a simple Level-2 MATLAB .pdf
 
Php Map Script Class Reference
Php Map Script Class ReferencePhp Map Script Class Reference
Php Map Script Class Reference
 
Progamming Primer Polymorphism (Method Overloading) VB
Progamming Primer Polymorphism (Method Overloading) VBProgamming Primer Polymorphism (Method Overloading) VB
Progamming Primer Polymorphism (Method Overloading) VB
 
SAS Macros part 4.1
SAS Macros part 4.1SAS Macros part 4.1
SAS Macros part 4.1
 
06 procedures
06 procedures06 procedures
06 procedures
 
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdfDeclaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdf
 
SAS_and_R.pdf
SAS_and_R.pdfSAS_and_R.pdf
SAS_and_R.pdf
 
Vba and macro creation (using excel)
Vba and macro creation (using excel)Vba and macro creation (using excel)
Vba and macro creation (using excel)
 
Company segmentation - an approach with R
Company segmentation - an approach with RCompany segmentation - an approach with R
Company segmentation - an approach with R
 
Module04
Module04Module04
Module04
 
Interface Python with MySQL.pdf
Interface Python with MySQL.pdfInterface Python with MySQL.pdf
Interface Python with MySQL.pdf
 
Stop hardcoding follow parameterization
Stop hardcoding  follow parameterizationStop hardcoding  follow parameterization
Stop hardcoding follow parameterization
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro calls
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

SAS Macros part 3

  • 1. Chapter 3. Macro Facility Interfaces Venkata Maguluri
  • 2. Creating Macro Variables in the DATA Step Creating Macro Variables in the DATA Step
  • 3.
  • 4.
  • 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. 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
  • 24.
  • 25. Indirect References to Macro Variables Creating Macro Variables in the DATA Step
  • 26.
  • 27. Recall these data sets from the course data: continued... Creating Macro Variables in the DATA Step
  • 28. Creating Macro Variables in the DATA Step
  • 29.
  • 30.
  • 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=&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. %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. 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=&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.
  • 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
  • 53.
  • 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 &quot;Total Revenue&quot;; title2 ”from Course Sites: &sites&quot;; run; The SQL Interface
  • 66. Obtaining Macro Variable Values During Execution
  • 67.
  • 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 &quot;Teacher for Each Registered Student&quot;; 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= &quot;&crsid&quot;; 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
  • 82. The SCL Interface (Self-Study)
  • 83.
  • 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
  • 90.