SlideShare a Scribd company logo
Derive Value from Excellence…
G -
SAS Macro – Part 1
By- Kavita Dargan
Derive Value from Excellence…
Agenda
Purpose of Macro Facility
Types of macro variables – Automatic vs User defined
Different ways of creating macro variable
Global vs. Local symbol table
Combining macro variable reference with text
Questions?
Derive Value from Excellence…
Purpose of Macro Facility
• Using the macro language, you can write SAS programs that
are dynamic, or capable of self modification.
• Write special programs called macros that generate tailored
SAS code.
• You can create and resolve macro variables anywhere in a SAS
program.
• You can utilize automatic macro variables that contain
information regarding your system environment.
Derive Value from Excellence…
Types of Macro Variables
 Automatic variables
• Created at SAS invocation
• Global (always available)
• Usually assigned by SAS but some
can be by user
• Example- Sysdate, Systime, Sysver,
Sysday etc
• Use %put _automatic_ in program
to view all available automatic
macro variables in log.
 User-defined variables
• Create own macro variable.
• If already exists then replaces the
current value.
• Leading and trailing blanks are
removed before assignment.
• Can be created using %Let, Into
clause, Call symput etc
• Use %put _user_ in program to
view all available user-defined
macro variables in log.
Derive Value from Excellence…
Types of Macro Variables Contd.
 Example: Substitute system information in footnote.
 Output:
footnote1 "Created &systime &sysday, &sysdate9";
footnote2
"on the &sysscp system using Release &sysver";
Derive Value from Excellence…
Different ways of creating macro
variables
 %LET
- Variable can be any name following the SAS naming convention.
- If variable already exists in the symbol table, value replaces the current
value.
- If either variable or value contains a macro trigger (&), it is evaluated
before the assignment is made.
- Rules for value:
• Maximum length is 32 characters and minimum length is 0 characters.
• Numeric tokens are stored as character strings and case is reserved.
• Mathematical expressions not evaluated.
• Quotes are stored as values.
• Leading and trailing blanks are removed before assignment which are not in
quotes.
%LET variable=value;
Derive Value from Excellence…
Different ways of creating macro
variables Contd.
 Examples of %LET statement
%let name= Ed Norton ;
%let name2=’ Ed Norton ’;
%let title="Joan’s Report";
%let start=;
%let total=0;
%let sum=3+4;
%let total=&total+∑
%let x=varlist;
%let &x=name age height;
Value
Ed Norton
’ Ed Norton ’
“Joan’s Report”
0
3+4
varlist
name age height
Derive Value from Excellence…
Different ways of creating macro
variables Contd.
 SYMPUT Routine using Literal
- First argument specify name for the macro variable
- Second argument specify the character value to be assigned to the
macro variable.
- If macro-variable already exists, the value of the text replaces the old
value.
CALL SYMPUT('macro-variable', 'text');
Derive Value from Excellence…
Different ways of creating macro
variables Contd.
 SYMPUT Routine using Data-step-variable
- Current value stored in data-step-variable to be assigned to the macro
variable.
- If macro-variable already exists, the value of the data-step-variable
replaces the old value.
- Maximum of 32767 characters can be assigned to the receiving macro
variable.
- Any leading or trailing blanks stored in value are assigned to macro
variable.
CALL SYMPUT(‘macro-variable’, Data-step-variable);
Derive Value from Excellence…
Different ways of creating macro
variables Contd.
 SYMPUT Routine using Data-step-expression
- Value of the expression to be assigned to the macro variable.
- If macro-variable already exists, the value of the expression replaces the
old value.
- Maximum 32767 characters can be assigned to the receiving macro
variable.
- Numeric expression are automatically converted to character using
BEST12. format.
- One can remove leading and trailing blanks in a variable by this method.
CALL SYMPUT(‘macro-variable’, expression);
Derive Value from Excellence…
Different ways of creating macro
variables Contd.
 SYMPUT Routine to create multiple macro variables
- 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.
- This way, we can create multiple macro variables in one data step.
- Advantage
• Efficiency
• Reduced coding
CALL SYMPUT(expression1, expression2);
Derive Value from Excellence…
Different ways of creating macro
variables Contd.
 Example
data _null_;
set courses;
call symput(course_code,trim(course_title));
run;
%put _user_;
Note: There are 6 observations in dataset courses.
Derive Value from Excellence…
Different ways of creating macro
variables Contd.
 INTO Clause
- One can specify one or more macro variables to create.
- Create new macro variables per row in the result of select statement.
- This method does not trim leading or trailing blanks.
SELECT col1, col2,. . . INTO :mvar1 - :mvarn,
:nvar1 - :nvarn,...
FROM table-expression
WHERE where-expression
other clauses;
Derive Value from Excellence…
Different ways of creating macro
variables Contd.
 INTO Clause example
- 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 schedule
where year(begin_date)=2002
order by begin_date;
quit;
SQL Result
Derive Value from Excellence…
Different ways of creating macro
variables Contd.
 Another form of INTO Clause
- It takes the values of a column and concatenates them into one macro
variable, there by creating delimited list of values. Example,
SELECT col1, . . .
INTO :mvar SEPARATED BY ’delimiter’,...
FROM table-expression
WHERE where-expression
other clauses;
proc sql noprint;
select distinct location into :sites
separated by ‘ ‘ from schedule;
quit;
Result: Macro variable sites resolves to Boston Dollas Seattle
Derive Value from Excellence…
Global symbol table vs. Local symbol table
 Macro defined by %global, variable
is held in global symbol table.
Whenever the SAS System is
invoked, a global symbol table is
created and initialized with
automatic or system-defined macro
variables.
 You can create user-defined global
macro variables with the %LET, Call
SYMPUT, INTO clause statement if it
is defined outside a macro definition
(called open code).
 Macro defined by %local, variable
is held in local symbol table.
Whenever a macro variable is
defined within a macro and is not
defined as global, variable is held in
local symbol table and it exists only
during execution of the macro in
which it is defined.
Variables declared in a parameter
list on a macro call are always
stored in a local symbol table.
Note: When you define macro with no arguments, local symbol table is not created and all the variables
defined inside the macro will be global and can be accessible inside as well as outside the macro.
Derive Value from Excellence…
Global vs. Local symbol table Contd.
 Example
%let x=0;
%macro outer;
%local x;
%let x=1;
%inner;
%mend outer;
%macro inner;
%local y;
%let y=&x;
%mend local;
Derive Value from Excellence…
Combining macro variable with text
 In general, macro variable reference is made by preceding the macro
variable name with an ampersand (&).
 Referencing a nonexistent macro variable results in a warning message.
WARNING: Apparent symbolic reference xxxxx not
resolved.
 Macro variable can be reference adjacent to leading and/or trailing text.
i.e. text&variable, &variable.text, text&variable.text
 Macro variable can be refence adjacent to another macro variable
reference. i.e. &variable1&variable2
Derive Value from Excellence…
Combining macro variable with text
 Example
%let lib=perm; %let year=90;
Libname &lib ‘SAS data library’;
Proc &graphics.chart
data=&lib..y&year&month;
Plot &var*day;
Run;
%let graphics=g; %let month=jan; %let var=sale;
Libname perm ‘SAS data library’;
Proc gchart data=perm.y90jan;
Plot sale*day;
Run;
Derive Value from Excellence…
Questions

More Related Content

What's hot

Introduction to SAS
Introduction to SASIntroduction to SAS
Introduction to SAS
izahn
 
Understanding SAS Data Step Processing
Understanding SAS Data Step ProcessingUnderstanding SAS Data Step Processing
Understanding SAS Data Step Processing
guest2160992
 
Sas Plots Graphs
Sas Plots GraphsSas Plots Graphs
Sas Plots Graphs
guest2160992
 
Base SAS Statistics Procedures
Base SAS Statistics ProceduresBase SAS Statistics Procedures
Base SAS Statistics Procedures
guest2160992
 
Introduction To Sas
Introduction To SasIntroduction To Sas
Introduction To Sas
halasti
 
Utility Procedures in SAS
Utility Procedures in SASUtility Procedures in SAS
Utility Procedures in SAS
guest2160992
 
SAS Proc SQL
SAS Proc SQLSAS Proc SQL
SAS Proc SQL
guest2160992
 
Sas practice programs
Sas practice programsSas practice programs
Sas practice programs
gowthami marreddy
 
Sas cheat
Sas cheatSas cheat
Sas cheat
imaduddin91
 
Arrays in SAS
Arrays in SASArrays in SAS
Arrays in SAS
guest2160992
 
Sas Functions INDEX / INDEXC / INDEXW
Sas Functions INDEX / INDEXC / INDEXWSas Functions INDEX / INDEXC / INDEXW
Sas Functions INDEX / INDEXC / INDEXW
THARUN PORANDLA
 
Proc SQL in SAS Enterprise Guide 4.3
Proc SQL in SAS Enterprise Guide 4.3Proc SQL in SAS Enterprise Guide 4.3
Proc SQL in SAS Enterprise Guide 4.3
Mark Tabladillo
 
SAS basics Step by step learning
SAS basics Step by step learningSAS basics Step by step learning
SAS basics Step by step learning
Venkata Reddy Konasani
 
INTRODUCTION TO SAS
INTRODUCTION TO SASINTRODUCTION TO SAS
INTRODUCTION TO SAS
Bhuwanesh Rawat
 
Base sas interview questions
Base sas interview questionsBase sas interview questions
Base sas interview questions
Dr P Deepak
 
SAS - overview of SAS
SAS - overview of SASSAS - overview of SAS
SAS - overview of SAS
Vibrant Technologies & Computers
 
Data Match Merging in SAS
Data Match Merging in SASData Match Merging in SAS
Data Match Merging in SAS
guest2160992
 
Report procedure
Report procedureReport procedure
Report procedure
MaanasaS
 
Where Vs If Statement
Where Vs If StatementWhere Vs If Statement
Where Vs If Statement
Sunil Gupta
 
SAS Access / SAS Connect
SAS Access / SAS ConnectSAS Access / SAS Connect
SAS Access / SAS Connect
guest2160992
 

What's hot (20)

Introduction to SAS
Introduction to SASIntroduction to SAS
Introduction to SAS
 
Understanding SAS Data Step Processing
Understanding SAS Data Step ProcessingUnderstanding SAS Data Step Processing
Understanding SAS Data Step Processing
 
Sas Plots Graphs
Sas Plots GraphsSas Plots Graphs
Sas Plots Graphs
 
Base SAS Statistics Procedures
Base SAS Statistics ProceduresBase SAS Statistics Procedures
Base SAS Statistics Procedures
 
Introduction To Sas
Introduction To SasIntroduction To Sas
Introduction To Sas
 
Utility Procedures in SAS
Utility Procedures in SASUtility Procedures in SAS
Utility Procedures in SAS
 
SAS Proc SQL
SAS Proc SQLSAS Proc SQL
SAS Proc SQL
 
Sas practice programs
Sas practice programsSas practice programs
Sas practice programs
 
Sas cheat
Sas cheatSas cheat
Sas cheat
 
Arrays in SAS
Arrays in SASArrays in SAS
Arrays in SAS
 
Sas Functions INDEX / INDEXC / INDEXW
Sas Functions INDEX / INDEXC / INDEXWSas Functions INDEX / INDEXC / INDEXW
Sas Functions INDEX / INDEXC / INDEXW
 
Proc SQL in SAS Enterprise Guide 4.3
Proc SQL in SAS Enterprise Guide 4.3Proc SQL in SAS Enterprise Guide 4.3
Proc SQL in SAS Enterprise Guide 4.3
 
SAS basics Step by step learning
SAS basics Step by step learningSAS basics Step by step learning
SAS basics Step by step learning
 
INTRODUCTION TO SAS
INTRODUCTION TO SASINTRODUCTION TO SAS
INTRODUCTION TO SAS
 
Base sas interview questions
Base sas interview questionsBase sas interview questions
Base sas interview questions
 
SAS - overview of SAS
SAS - overview of SASSAS - overview of SAS
SAS - overview of SAS
 
Data Match Merging in SAS
Data Match Merging in SASData Match Merging in SAS
Data Match Merging in SAS
 
Report procedure
Report procedureReport procedure
Report procedure
 
Where Vs If Statement
Where Vs If StatementWhere Vs If Statement
Where Vs If Statement
 
SAS Access / SAS Connect
SAS Access / SAS ConnectSAS Access / SAS Connect
SAS Access / SAS Connect
 

Similar to SAS Macro

BAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 LectureBAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 Lecture
Wake Tech BAS
 
SAS Macros part 3
SAS Macros part 3SAS Macros part 3
SAS Macros part 3
venkatam
 
Ss4
Ss4Ss4
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
 
Module 5.pdf
Module 5.pdfModule 5.pdf
Module 5.pdf
SE14Darshan
 
Unit 2
Unit 2Unit 2
Unit 2
pm_ghate
 
Macro-processor
Macro-processorMacro-processor
Macro-processor
Temesgen Molla
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
Anand Kumar
 
Getting started with CATIA V5 Macros
Getting started with CATIA V5 MacrosGetting started with CATIA V5 Macros
Getting started with CATIA V5 Macros
Emmett Ross
 
handout6.pdf
handout6.pdfhandout6.pdf
handout6.pdf
ssuser700533
 
Sas macros part 4.1
Sas macros part 4.1Sas macros part 4.1
Sas macros part 4.1
venkatam
 
E learning excel vba programming lesson 3
E learning excel vba programming  lesson 3E learning excel vba programming  lesson 3
E learning excel vba programming lesson 3
Vijay Perepa
 
Intro to tsql unit 13
Intro to tsql   unit 13Intro to tsql   unit 13
Intro to tsql unit 13
Syed Asrarali
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
Akhil Kaushik
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
Deepmala Sharma
 
VBA
VBAVBA
Robust and declarative machine learning pipelines for predictive buying at Ba...
Robust and declarative machine learning pipelines for predictive buying at Ba...Robust and declarative machine learning pipelines for predictive buying at Ba...
Robust and declarative machine learning pipelines for predictive buying at Ba...
Gianmario Spacagna
 
Vba and macro creation (using excel)
Vba and macro creation (using excel)Vba and macro creation (using excel)
Vba and macro creation (using excel)
Javier Morales Cauna
 
Power of call symput data
Power of call symput dataPower of call symput data
Power of call symput data
Yash Sharma
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxINTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
DeepasCSE
 

Similar to SAS Macro (20)

BAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 LectureBAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 Lecture
 
SAS Macros part 3
SAS Macros part 3SAS Macros part 3
SAS Macros part 3
 
Ss4
Ss4Ss4
Ss4
 
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
 
Module 5.pdf
Module 5.pdfModule 5.pdf
Module 5.pdf
 
Unit 2
Unit 2Unit 2
Unit 2
 
Macro-processor
Macro-processorMacro-processor
Macro-processor
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
 
Getting started with CATIA V5 Macros
Getting started with CATIA V5 MacrosGetting started with CATIA V5 Macros
Getting started with CATIA V5 Macros
 
handout6.pdf
handout6.pdfhandout6.pdf
handout6.pdf
 
Sas macros part 4.1
Sas macros part 4.1Sas macros part 4.1
Sas macros part 4.1
 
E learning excel vba programming lesson 3
E learning excel vba programming  lesson 3E learning excel vba programming  lesson 3
E learning excel vba programming lesson 3
 
Intro to tsql unit 13
Intro to tsql   unit 13Intro to tsql   unit 13
Intro to tsql unit 13
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
 
VBA
VBAVBA
VBA
 
Robust and declarative machine learning pipelines for predictive buying at Ba...
Robust and declarative machine learning pipelines for predictive buying at Ba...Robust and declarative machine learning pipelines for predictive buying at Ba...
Robust and declarative machine learning pipelines for predictive buying at Ba...
 
Vba and macro creation (using excel)
Vba and macro creation (using excel)Vba and macro creation (using excel)
Vba and macro creation (using excel)
 
Power of call symput data
Power of call symput dataPower of call symput data
Power of call symput data
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxINTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
 

Recently uploaded

Emotional and Behavioural Problems in Children - Counselling and Family Thera...
Emotional and Behavioural Problems in Children - Counselling and Family Thera...Emotional and Behavioural Problems in Children - Counselling and Family Thera...
Emotional and Behavioural Problems in Children - Counselling and Family Thera...
PsychoTech Services
 
Sexual Disorders.gender identity disorderspptx
Sexual Disorders.gender identity  disorderspptxSexual Disorders.gender identity  disorderspptx
Sexual Disorders.gender identity disorderspptx
Pupayumnam1
 
Daughter's of Dr Ranjit Jagtap (Poulami & Aditi)
Daughter's of Dr Ranjit Jagtap (Poulami & Aditi)Daughter's of Dr Ranjit Jagtap (Poulami & Aditi)
Daughter's of Dr Ranjit Jagtap (Poulami & Aditi)
Aditi Jagtap Pune
 
2024 Media Preferences of Older Adults: Consumer Survey and Marketing Implica...
2024 Media Preferences of Older Adults: Consumer Survey and Marketing Implica...2024 Media Preferences of Older Adults: Consumer Survey and Marketing Implica...
2024 Media Preferences of Older Adults: Consumer Survey and Marketing Implica...
Media Logic
 
nurs fpx 4050 assessment 4 final care coordination plan.pdf
nurs fpx 4050 assessment 4 final care coordination plan.pdfnurs fpx 4050 assessment 4 final care coordination plan.pdf
nurs fpx 4050 assessment 4 final care coordination plan.pdf
Carolyn Harker
 
Monopoly PCD Pharma Franchise in Tripura
Monopoly PCD Pharma Franchise in TripuraMonopoly PCD Pharma Franchise in Tripura
Monopoly PCD Pharma Franchise in Tripura
SKG Internationals
 
nursing management of patient with Empyema ppt
nursing management of patient with Empyema pptnursing management of patient with Empyema ppt
nursing management of patient with Empyema ppt
blessyjannu21
 
Digital Health in India_Health Informatics Trained Manpower _DrDevTaneja_15.0...
Digital Health in India_Health Informatics Trained Manpower _DrDevTaneja_15.0...Digital Health in India_Health Informatics Trained Manpower _DrDevTaneja_15.0...
Digital Health in India_Health Informatics Trained Manpower _DrDevTaneja_15.0...
DrDevTaneja1
 
3. User Guide Activity Budget Tracking App Steps to apply.pptx
3. User Guide Activity Budget Tracking App Steps to apply.pptx3. User Guide Activity Budget Tracking App Steps to apply.pptx
3. User Guide Activity Budget Tracking App Steps to apply.pptx
habtegirma
 
English Drug and Alcohol Commissioners June 2024.pptx
English Drug and Alcohol Commissioners June 2024.pptxEnglish Drug and Alcohol Commissioners June 2024.pptx
English Drug and Alcohol Commissioners June 2024.pptx
MatSouthwell1
 
Health Tech Market Intelligence Prelim Questions -
Health Tech Market Intelligence Prelim Questions -Health Tech Market Intelligence Prelim Questions -
Health Tech Market Intelligence Prelim Questions -
Gokul Rangarajan
 
Mohali Call Girls 7742996321 Call Girls Mohali
Mohali Call Girls  7742996321 Call Girls  MohaliMohali Call Girls  7742996321 Call Girls  Mohali
Mohali Call Girls 7742996321 Call Girls Mohali
Digital Marketing
 
Sectional dentures for microstomia patients.pptx
Sectional dentures for microstomia patients.pptxSectional dentures for microstomia patients.pptx
Sectional dentures for microstomia patients.pptx
SatvikaPrasad
 
Satisfying Spa Massage Experience at Just 99 AED - Malayali Kerala Spa Ajman
Satisfying Spa Massage Experience at Just 99 AED - Malayali Kerala Spa AjmanSatisfying Spa Massage Experience at Just 99 AED - Malayali Kerala Spa Ajman
Satisfying Spa Massage Experience at Just 99 AED - Malayali Kerala Spa Ajman
Malayali Kerala Spa Ajman
 
Know Latest Hiranandani Hospital Powai News.pdf
Know Latest Hiranandani Hospital Powai News.pdfKnow Latest Hiranandani Hospital Powai News.pdf
Know Latest Hiranandani Hospital Powai News.pdf
Dr. Sujit Chatterjee CEO Hiranandani Hospital
 
The Ultimate Guide in Setting Up Market Research System in Health-Tech
The Ultimate Guide in Setting Up Market Research System in Health-TechThe Ultimate Guide in Setting Up Market Research System in Health-Tech
The Ultimate Guide in Setting Up Market Research System in Health-Tech
Gokul Rangarajan
 
EXAMINATION OF HUMAN URINE AND FAECES.pdf
EXAMINATION OF HUMAN URINE AND FAECES.pdfEXAMINATION OF HUMAN URINE AND FAECES.pdf
EXAMINATION OF HUMAN URINE AND FAECES.pdf
Madhusmita Sahoo
 
一比一原版(UoA毕业证)昆士兰科技大学毕业证如何办理
一比一原版(UoA毕业证)昆士兰科技大学毕业证如何办理一比一原版(UoA毕业证)昆士兰科技大学毕业证如何办理
一比一原版(UoA毕业证)昆士兰科技大学毕业证如何办理
xkute
 
The crucial role of mathematics in ai development.pptx
The crucial role of mathematics in ai development.pptxThe crucial role of mathematics in ai development.pptx
The crucial role of mathematics in ai development.pptx
priyabhojwani1200
 
Test bank advanced health assessment and differential diagnosis essentials fo...
Test bank advanced health assessment and differential diagnosis essentials fo...Test bank advanced health assessment and differential diagnosis essentials fo...
Test bank advanced health assessment and differential diagnosis essentials fo...
rightmanforbloodline
 

Recently uploaded (20)

Emotional and Behavioural Problems in Children - Counselling and Family Thera...
Emotional and Behavioural Problems in Children - Counselling and Family Thera...Emotional and Behavioural Problems in Children - Counselling and Family Thera...
Emotional and Behavioural Problems in Children - Counselling and Family Thera...
 
Sexual Disorders.gender identity disorderspptx
Sexual Disorders.gender identity  disorderspptxSexual Disorders.gender identity  disorderspptx
Sexual Disorders.gender identity disorderspptx
 
Daughter's of Dr Ranjit Jagtap (Poulami & Aditi)
Daughter's of Dr Ranjit Jagtap (Poulami & Aditi)Daughter's of Dr Ranjit Jagtap (Poulami & Aditi)
Daughter's of Dr Ranjit Jagtap (Poulami & Aditi)
 
2024 Media Preferences of Older Adults: Consumer Survey and Marketing Implica...
2024 Media Preferences of Older Adults: Consumer Survey and Marketing Implica...2024 Media Preferences of Older Adults: Consumer Survey and Marketing Implica...
2024 Media Preferences of Older Adults: Consumer Survey and Marketing Implica...
 
nurs fpx 4050 assessment 4 final care coordination plan.pdf
nurs fpx 4050 assessment 4 final care coordination plan.pdfnurs fpx 4050 assessment 4 final care coordination plan.pdf
nurs fpx 4050 assessment 4 final care coordination plan.pdf
 
Monopoly PCD Pharma Franchise in Tripura
Monopoly PCD Pharma Franchise in TripuraMonopoly PCD Pharma Franchise in Tripura
Monopoly PCD Pharma Franchise in Tripura
 
nursing management of patient with Empyema ppt
nursing management of patient with Empyema pptnursing management of patient with Empyema ppt
nursing management of patient with Empyema ppt
 
Digital Health in India_Health Informatics Trained Manpower _DrDevTaneja_15.0...
Digital Health in India_Health Informatics Trained Manpower _DrDevTaneja_15.0...Digital Health in India_Health Informatics Trained Manpower _DrDevTaneja_15.0...
Digital Health in India_Health Informatics Trained Manpower _DrDevTaneja_15.0...
 
3. User Guide Activity Budget Tracking App Steps to apply.pptx
3. User Guide Activity Budget Tracking App Steps to apply.pptx3. User Guide Activity Budget Tracking App Steps to apply.pptx
3. User Guide Activity Budget Tracking App Steps to apply.pptx
 
English Drug and Alcohol Commissioners June 2024.pptx
English Drug and Alcohol Commissioners June 2024.pptxEnglish Drug and Alcohol Commissioners June 2024.pptx
English Drug and Alcohol Commissioners June 2024.pptx
 
Health Tech Market Intelligence Prelim Questions -
Health Tech Market Intelligence Prelim Questions -Health Tech Market Intelligence Prelim Questions -
Health Tech Market Intelligence Prelim Questions -
 
Mohali Call Girls 7742996321 Call Girls Mohali
Mohali Call Girls  7742996321 Call Girls  MohaliMohali Call Girls  7742996321 Call Girls  Mohali
Mohali Call Girls 7742996321 Call Girls Mohali
 
Sectional dentures for microstomia patients.pptx
Sectional dentures for microstomia patients.pptxSectional dentures for microstomia patients.pptx
Sectional dentures for microstomia patients.pptx
 
Satisfying Spa Massage Experience at Just 99 AED - Malayali Kerala Spa Ajman
Satisfying Spa Massage Experience at Just 99 AED - Malayali Kerala Spa AjmanSatisfying Spa Massage Experience at Just 99 AED - Malayali Kerala Spa Ajman
Satisfying Spa Massage Experience at Just 99 AED - Malayali Kerala Spa Ajman
 
Know Latest Hiranandani Hospital Powai News.pdf
Know Latest Hiranandani Hospital Powai News.pdfKnow Latest Hiranandani Hospital Powai News.pdf
Know Latest Hiranandani Hospital Powai News.pdf
 
The Ultimate Guide in Setting Up Market Research System in Health-Tech
The Ultimate Guide in Setting Up Market Research System in Health-TechThe Ultimate Guide in Setting Up Market Research System in Health-Tech
The Ultimate Guide in Setting Up Market Research System in Health-Tech
 
EXAMINATION OF HUMAN URINE AND FAECES.pdf
EXAMINATION OF HUMAN URINE AND FAECES.pdfEXAMINATION OF HUMAN URINE AND FAECES.pdf
EXAMINATION OF HUMAN URINE AND FAECES.pdf
 
一比一原版(UoA毕业证)昆士兰科技大学毕业证如何办理
一比一原版(UoA毕业证)昆士兰科技大学毕业证如何办理一比一原版(UoA毕业证)昆士兰科技大学毕业证如何办理
一比一原版(UoA毕业证)昆士兰科技大学毕业证如何办理
 
The crucial role of mathematics in ai development.pptx
The crucial role of mathematics in ai development.pptxThe crucial role of mathematics in ai development.pptx
The crucial role of mathematics in ai development.pptx
 
Test bank advanced health assessment and differential diagnosis essentials fo...
Test bank advanced health assessment and differential diagnosis essentials fo...Test bank advanced health assessment and differential diagnosis essentials fo...
Test bank advanced health assessment and differential diagnosis essentials fo...
 

SAS Macro

  • 1. Derive Value from Excellence… G - SAS Macro – Part 1 By- Kavita Dargan
  • 2. Derive Value from Excellence… Agenda Purpose of Macro Facility Types of macro variables – Automatic vs User defined Different ways of creating macro variable Global vs. Local symbol table Combining macro variable reference with text Questions?
  • 3. Derive Value from Excellence… Purpose of Macro Facility • Using the macro language, you can write SAS programs that are dynamic, or capable of self modification. • Write special programs called macros that generate tailored SAS code. • You can create and resolve macro variables anywhere in a SAS program. • You can utilize automatic macro variables that contain information regarding your system environment.
  • 4. Derive Value from Excellence… Types of Macro Variables  Automatic variables • Created at SAS invocation • Global (always available) • Usually assigned by SAS but some can be by user • Example- Sysdate, Systime, Sysver, Sysday etc • Use %put _automatic_ in program to view all available automatic macro variables in log.  User-defined variables • Create own macro variable. • If already exists then replaces the current value. • Leading and trailing blanks are removed before assignment. • Can be created using %Let, Into clause, Call symput etc • Use %put _user_ in program to view all available user-defined macro variables in log.
  • 5. Derive Value from Excellence… Types of Macro Variables Contd.  Example: Substitute system information in footnote.  Output: footnote1 "Created &systime &sysday, &sysdate9"; footnote2 "on the &sysscp system using Release &sysver";
  • 6. Derive Value from Excellence… Different ways of creating macro variables  %LET - Variable can be any name following the SAS naming convention. - If variable already exists in the symbol table, value replaces the current value. - If either variable or value contains a macro trigger (&), it is evaluated before the assignment is made. - Rules for value: • Maximum length is 32 characters and minimum length is 0 characters. • Numeric tokens are stored as character strings and case is reserved. • Mathematical expressions not evaluated. • Quotes are stored as values. • Leading and trailing blanks are removed before assignment which are not in quotes. %LET variable=value;
  • 7. Derive Value from Excellence… Different ways of creating macro variables Contd.  Examples of %LET statement %let name= Ed Norton ; %let name2=’ Ed Norton ’; %let title="Joan’s Report"; %let start=; %let total=0; %let sum=3+4; %let total=&total+∑ %let x=varlist; %let &x=name age height; Value Ed Norton ’ Ed Norton ’ “Joan’s Report” 0 3+4 varlist name age height
  • 8. Derive Value from Excellence… Different ways of creating macro variables Contd.  SYMPUT Routine using Literal - First argument specify name for the macro variable - Second argument specify the character value to be assigned to the macro variable. - If macro-variable already exists, the value of the text replaces the old value. CALL SYMPUT('macro-variable', 'text');
  • 9. Derive Value from Excellence… Different ways of creating macro variables Contd.  SYMPUT Routine using Data-step-variable - Current value stored in data-step-variable to be assigned to the macro variable. - If macro-variable already exists, the value of the data-step-variable replaces the old value. - Maximum of 32767 characters can be assigned to the receiving macro variable. - Any leading or trailing blanks stored in value are assigned to macro variable. CALL SYMPUT(‘macro-variable’, Data-step-variable);
  • 10. Derive Value from Excellence… Different ways of creating macro variables Contd.  SYMPUT Routine using Data-step-expression - Value of the expression to be assigned to the macro variable. - If macro-variable already exists, the value of the expression replaces the old value. - Maximum 32767 characters can be assigned to the receiving macro variable. - Numeric expression are automatically converted to character using BEST12. format. - One can remove leading and trailing blanks in a variable by this method. CALL SYMPUT(‘macro-variable’, expression);
  • 11. Derive Value from Excellence… Different ways of creating macro variables Contd.  SYMPUT Routine to create multiple macro variables - 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. - This way, we can create multiple macro variables in one data step. - Advantage • Efficiency • Reduced coding CALL SYMPUT(expression1, expression2);
  • 12. Derive Value from Excellence… Different ways of creating macro variables Contd.  Example data _null_; set courses; call symput(course_code,trim(course_title)); run; %put _user_; Note: There are 6 observations in dataset courses.
  • 13. Derive Value from Excellence… Different ways of creating macro variables Contd.  INTO Clause - One can specify one or more macro variables to create. - Create new macro variables per row in the result of select statement. - This method does not trim leading or trailing blanks. SELECT col1, col2,. . . INTO :mvar1 - :mvarn, :nvar1 - :nvarn,... FROM table-expression WHERE where-expression other clauses;
  • 14. Derive Value from Excellence… Different ways of creating macro variables Contd.  INTO Clause example - 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 schedule where year(begin_date)=2002 order by begin_date; quit; SQL Result
  • 15. Derive Value from Excellence… Different ways of creating macro variables Contd.  Another form of INTO Clause - It takes the values of a column and concatenates them into one macro variable, there by creating delimited list of values. Example, SELECT col1, . . . INTO :mvar SEPARATED BY ’delimiter’,... FROM table-expression WHERE where-expression other clauses; proc sql noprint; select distinct location into :sites separated by ‘ ‘ from schedule; quit; Result: Macro variable sites resolves to Boston Dollas Seattle
  • 16. Derive Value from Excellence… Global symbol table vs. Local symbol table  Macro defined by %global, variable is held in global symbol table. Whenever the SAS System is invoked, a global symbol table is created and initialized with automatic or system-defined macro variables.  You can create user-defined global macro variables with the %LET, Call SYMPUT, INTO clause statement if it is defined outside a macro definition (called open code).  Macro defined by %local, variable is held in local symbol table. Whenever a macro variable is defined within a macro and is not defined as global, variable is held in local symbol table and it exists only during execution of the macro in which it is defined. Variables declared in a parameter list on a macro call are always stored in a local symbol table. Note: When you define macro with no arguments, local symbol table is not created and all the variables defined inside the macro will be global and can be accessible inside as well as outside the macro.
  • 17. Derive Value from Excellence… Global vs. Local symbol table Contd.  Example %let x=0; %macro outer; %local x; %let x=1; %inner; %mend outer; %macro inner; %local y; %let y=&x; %mend local;
  • 18. Derive Value from Excellence… Combining macro variable with text  In general, macro variable reference is made by preceding the macro variable name with an ampersand (&).  Referencing a nonexistent macro variable results in a warning message. WARNING: Apparent symbolic reference xxxxx not resolved.  Macro variable can be reference adjacent to leading and/or trailing text. i.e. text&variable, &variable.text, text&variable.text  Macro variable can be refence adjacent to another macro variable reference. i.e. &variable1&variable2
  • 19. Derive Value from Excellence… Combining macro variable with text  Example %let lib=perm; %let year=90; Libname &lib ‘SAS data library’; Proc &graphics.chart data=&lib..y&year&month; Plot &var*day; Run; %let graphics=g; %let month=jan; %let var=sale; Libname perm ‘SAS data library’; Proc gchart data=perm.y90jan; Plot sale*day; Run;
  • 20. Derive Value from Excellence… Questions