117
When Best to Use the %LET Statement, the SYMPUT Routine, or the INTO Clause to Create Macro Variables Arthur Li Department of Information Science City of Hope Comprehensive Cancer Center Duarte, CA

When best to use the %let statement, the symput routine, or the into clause to create macro variables

Embed Size (px)

DESCRIPTION

Macro variables are the most essential part of the SAS macro facility. One can create macro variables by using the %LET statement, the SYMPUT routine, or the INTO clause in the SELECT statement from the SQL procedure. Sometimes a SAS programmer is often unsure when best to use which method due to a lack in understanding each step of macro language processing. This lack in understanding includes how SAS statements are transferred from the input stack to the macro processor and DATA step compiler, what role the macro processor plays during this process, and when best to utilize the interface to interact with the macro facility during the DATA or SQL execution. Once one grasps the intricacies of macro language processing, one will best know how to accurately create a macro variable.

Citation preview

Page 1: When best to use the %let statement, the symput routine, or the into clause to create macro variables

When Best to Use the %LET Statement, the SYMPUT Routine, or the INTO Clause to Create Macro Variables

Arthur LiDepartment of Information Science

City of Hope Comprehensive Cancer Center Duarte, CA

Page 2: When best to use the %let statement, the symput routine, or the into clause to create macro variables

DATA Step PROC Steps

SAS Macro Facility

BASE SAS

INTRODUCTION

Use to generate SAS codes

more…

PROC SQL

Page 3: When best to use the %let statement, the symput routine, or the into clause to create macro variables

DATA Step PROC Steps

SAS Macro Facility

BASE SAS

INTRODUCTION

more…

PROC SQL

SAS Macro Facility -Has its own language

SAS MacroVariables

SAS Macro Programs

Prerequisites:

Focus of this talk

Page 4: When best to use the %let statement, the symput routine, or the into clause to create macro variables

DATA Step PROC Steps

SAS Macro Facility

BASE SAS

INTRODUCTION

more…

PROC SQL

SAS Macro Facility -Has its own language

SAS MacroVariables

SAS Macro Programs

Prerequisites:

SAS MacroVariables

SAS Macro Variables

AutomaticUser-

Defined

Focus of this talk

Page 5: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE %LET STATEMENT

One way to create a macro variable is to use the %LET statement

%LET MACRO-VARIABLE = VALUE;

%let var1 = 4 + 3;

The VALUE is stored as character strings

MACRO-VARIABLE var1

VALUE 4 + 3

Mathematical expressions are not evaluated

Page 6: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE %LET STATEMENT

One way to create a macro variable is to use the %LET statement

%LET MACRO-VARIABLE = VALUE;

%let var2 = leading blank;

The VALUE is stored as character strings

MACRO-VARIABLE var2

VALUE leading blank

Leading and trailing blanks are removed

Page 7: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE %LET STATEMENT

One way to create a macro variable is to use the %LET statement

%LET MACRO-VARIABLE = VALUE;

%let var3 = " quotations ";

The VALUE is stored as character strings

MACRO-VARIABLE var3

VALUE “ quotations ”

Quotation marks are part of the values

Page 8: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE %LET STATEMENT

One way to create a macro variable is to use the %LET statement

%LET MACRO-VARIABLE = VALUE;

If MACRO-VARIABLE and/or VALUE contain references to another macro variable, the reference will be evaluated first before the assignment

Page 9: When best to use the %let statement, the symput routine, or the into clause to create macro variables

REFERENCING MACRO VARIABLES

Once a macro variable is defined, the value of the macro variable is stored in the global symbol table

%let ht = 63;

Global Symbol Table

SAS Automatic Macro Variables:

SYSDATE 19JUN06

SYSDAY Monday

SYSTIME 10:34

User-Defined Macro Variables:ht 63

Page 10: When best to use the %let statement, the symput routine, or the into clause to create macro variables

REFERENCING MACRO VARIABLES

To substitute a macro variable in the SAS program, you must reference the macro variable:

%let ht = 63;data ex1; set height; tall = height > &ht;run;

Global Symbol Table

SAS Automatic Macro Variables:

SYSDATE 19JUN06

SYSDAY Monday

SYSTIME 10:34

User-Defined Macro Variables:ht 63

& MACRO-VARIABLE

63

If the reference of a macro variable is within quotations, the double quotation marks must be used

“& MACRO-VARIABLE”

Page 11: When best to use the %let statement, the symput routine, or the into clause to create macro variables

UNDERSTANDING SAS PROCESSING

In order to understand how macro variables are processed and stored, one needs to understand how SAS processing works

Compilation phaseEach statement is scanned for syntax

errors

Execution phaseThe DATA step reads

and processes the input data

the descriptor portion is created

If there is no syntax error

Done in the compiler

How are the SAS statements transferred to the compiler?

A DATA step is processed in sequence in two phases

Page 12: When best to use the %let statement, the symput routine, or the into clause to create macro variables

UNDERSTANDING SAS PROCESSING

INPUT STACK When a program is submitted, all the codes are placed in a memory area, called the input stack

data ex1; set height; tall = height > 63;run;

Page 13: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKdata ex1; set height; tall = height > 63;run;

The word scanner takes the statements from the input stack and tokenizes the statements into tokens

The word scanner then directs the tokens to the right location

DATA step compiler Macro processor, etc.

UNDERSTANDING SAS PROCESSING

Page 14: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKex1; set height; tall = height > 63;run;

data

UNDERSTANDING SAS PROCESSING

Page 15: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK; set height; tall = height > 63;run;

ex1

data

UNDERSTANDING SAS PROCESSING

Page 16: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKset height; tall = height > 63;run;

;

ex1 data

UNDERSTANDING SAS PROCESSING

Page 17: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKheight; tall = height > 63;run;

set

; ex1

data

UNDERSTANDING SAS PROCESSING

Page 18: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK; tall = height > 63;run;

height

set ;

data ex1

UNDERSTANDING SAS PROCESSING

Page 19: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKtall = height > 63;run;

;

height set

data ex1;

UNDERSTANDING SAS PROCESSING

Page 20: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK= height > 63;run;

tall

; height

data ex1; set

UNDERSTANDING SAS PROCESSING

Page 21: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKheight > 63;run; =

tall ;

data ex1; setheight

UNDERSTANDING SAS PROCESSING

Page 22: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK> 63;run; height

= tall

data ex1; setheight;

UNDERSTANDING SAS PROCESSING

Page 23: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK63;run; >

height =

data ex1; setheight; tall

UNDERSTANDING SAS PROCESSING

Page 24: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK;run; 63

> height

data ex1; setheight; tall =

UNDERSTANDING SAS PROCESSING

Page 25: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKrun;

;

63 >

data ex1; setheight; tall =height

UNDERSTANDING SAS PROCESSING

Page 26: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK;

run

; 63

data ex1; setheight; tall =height >

UNDERSTANDING SAS PROCESSING

Page 27: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK

;

run ;

data ex1; setheight; tall =height > 63

UNDERSTANDING SAS PROCESSING

Page 28: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK

; run

data ex1; setheight; tall =height > 63;

UNDERSTANDING SAS PROCESSING

Page 29: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK

;

data ex1; setheight; tall =height > 63; run

UNDERSTANDING SAS PROCESSING

Page 30: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK

data ex1; setheight; tall =height > 63; run;

When the compiler receives the semicolon following the RUN statement, it stops taking tokens from the word scanner

UNDERSTANDING SAS PROCESSING

Compilation phaseEach statement is

scanned for syntax errors.

Execution phaseThe DATA step reads and processes the input data.

If there is no syntax error

Page 31: When best to use the %let statement, the symput routine, or the into clause to create macro variables

UNDERSTANDING SAS PROCESSING

Types of Token Contains … Examples

Literal Characters enclosed in quotation marks “John” ‘John’

Numerals including decimals, E-notation, date, time, datetime constants, and hexadecimal constants

555‘01mar2010’d30e4

Number

Characters that begin with a letter or underscore and that continues with underscores, letters, or numbers. A period can sometimes be part of a name

_n_meansdollar9.2Descending

Name

Characters other than a letter, number, or underscore that have a special meaning to the SAS system

Special character

/ + % & . ;

Page 32: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK%let ht =63; data ex1; set height; tall =height >&ht; run;

The macro facility includes a macro processor that is responsible for processing all macro language elements

MACRO PROCESSOR

Macro variable references and %LET statements are part of the macro language

MACRO PROCESSING

Page 33: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK%let ht =63; data ex1; set height; tall =height >&ht; run;

Macro trigger: %LET name-token & name-token

MACRO PROCESSOR

The word scanner needs to recognize the macro language and direct them to the macro processor

MACRO PROCESSING

Page 34: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKlet ht =63; data ex1; set height; tall =height >&ht; run;

%

MACRO PROCESSOR

MACRO PROCESSING

Page 35: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKht =63; data ex1; set height; tall =height >&ht; run;

let

%

MACRO PROCESSORWhen the word

scanner detects %LET (a macro trigger), it starts to direct the tokens to the macro processor

MACRO PROCESSING

Page 36: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK=63; data ex1; set height; tall =height >&ht; run;

ht

let

MACRO PROCESSOR

%

MACRO PROCESSING

Page 37: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK63; data ex1; set height; tall =height >&ht; run;

=

ht

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables

let

%

MACRO PROCESSING

Page 38: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKdata ex1; set height; tall =height >&ht; run;

;

63

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables

=

ht

MACRO PROCESSING

Page 39: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKex1; set height; tall =height >&ht; run;

data

;

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht

63

=

MACRO PROCESSING

Page 40: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK; set height; tall =height >&ht; run;

ex1

data

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht

;

63

The macro processor stops processing the tokens when the semicolon is encountered

MACRO PROCESSING

Page 41: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKset height; tall =height >&ht; run;

;

ex1 data

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

MACRO PROCESSING

Page 42: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKheight; tall =height >&ht; run;

set

; ex1

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data

MACRO PROCESSING

Page 43: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK; tall =height >&ht; run;

height

set ;

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1

MACRO PROCESSING

Page 44: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKtall =height >&ht; run; ;

height set

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1;

MACRO PROCESSING

Page 45: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK=height >&ht; run; tall

; height

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; set

MACRO PROCESSING

Page 46: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKheight >&ht; run; =

tall ;

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight

MACRO PROCESSING

Page 47: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK>&ht; run;

height

= tall

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setHeight;

MACRO PROCESSING

Page 48: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK&ht; run;

>

height =

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight; tall

MACRO PROCESSING

Page 49: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKht; run;

&

> height

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight; tall =

MACRO PROCESSING

Page 50: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK; run;

ht

& >

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight; tall =height

When the word scanner encounters the ampersand followed by HT, it directs them to the macro processor

MACRO PROCESSING

Page 51: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK; run;

ht

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

&

data ex1; setheight; tall =Height >

MACRO PROCESSING

Page 52: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK; run;

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

ht

data ex1; setheight; tall =Height >

MACRO PROCESSING

Page 53: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK; run;

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

ht

data ex1; setheight; tall =Height >

The macro processor looks up the macro variable HT and takes its value from the symbol take

MACRO PROCESSING

Page 54: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK; run;

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

ht

data ex1; setheight; tall =Height >

63

MACRO PROCESSING

Page 55: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK; run;

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight; tall =Height >

63

MACRO PROCESSING

Page 56: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK63; run;

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight; tall =Height >

MACRO PROCESSING

Page 57: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK; run;

63

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight; tall =Height >

MACRO PROCESSING

Page 58: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACKrun;

;

63

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight; tall =Height >

MACRO PROCESSING

Page 59: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK;

run

; 63

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight; tall =Height >

MACRO PROCESSING

Page 60: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK

;

run ;

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight; tall =Height > 63

MACRO PROCESSING

Page 61: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK

; run

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight; tall =Height > 63;

MACRO PROCESSING

Page 62: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK

;

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight; tall =Height > 63; run

MACRO PROCESSING

Page 63: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILER

INPUT STACK

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63

data ex1; setheight; tall =Height > 63; run;

MACRO PROCESSING

Page 64: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES AT EXECUTION TIME

In many applications, we need to create macro variables during DATA step execution

For example, we might need to create macro variables and assign values to them based on

data values in SAS data sets or in external filesprogramming logiccomputed values

Page 65: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES AT EXECUTION TIME

name sex height tall

1 John m 65 1

2 Tom m 60 0

3 Mary f 62 0

4 Helen f 64 1

Create macro variable TITL - serves as a title for printing the dataset ex1

If there are some students > 63 inches (when tall = 1), TITL = “Some students are taller than 63 inches” If none of the students > than 63 inches, TITL = “None of the students are taller than 63 inches”

Ex1:

Page 66: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES AT EXECUTION TIME

name sex height tall

1 John m 65 1

2 Tom m 60 0

3 Mary f 62 0

4 Helen f 64 1

Ex1:

data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do; %let titl = "Some students are taller than 63 inches"; end; else do; %let titl = "None of the students are taller than 63 inches"; end; end;run;

Page 67: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES AT EXECUTION TIME

name sex height tall

1 John m 65 1

2 Tom m 60 0

3 Mary f 62 0

4 Helen f 64 1

Ex1:

proc print data=ex1; title &titl;run;

None of the students are taller than 63 inches

Obs name sex height tall 1 John m 65 1 2 Tom m 60 0 3 Mary f 62 0 4 Helen f 64 1

Page 68: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILERINPUT STACK

data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do; %let titl ="Some students are taller than 63 inches"; end; else do; %let titl = "None of the students are taller than 63 inches"; end; end; run;

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables

PROCESSING MACRO VARIABLES AT EXECUTION TIME

Page 69: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILERINPUT STACK

="Some students are taller than 63 inches"; end; else do; %let titl = "None of the students are taller than 63 inches"; end; end; run;

titl

let

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables

%

data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;

PROCESSING MACRO VARIABLES AT EXECUTION TIME

Page 70: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILERINPUT STACK

%let titl = "None of the students are taller than 63 inches"; end; end; run;

;

do else

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables Titl Some students are taller than 63 inches

data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;end;

PROCESSING MACRO VARIABLES AT EXECUTION TIME

Page 71: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILERINPUT STACK

let titl = "None of the students are taller than 63 inches"; end; end; run;

%

; do

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables Titl Some students are taller than 63 inches

data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;end; else

PROCESSING MACRO VARIABLES AT EXECUTION TIME

Page 72: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILERINPUT STACK

= "None of the students are taller than 63 inches"; end; end; run;

titl

let

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables Titl Some students are taller than 63 inches

%

data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;end; else do;

PROCESSING MACRO VARIABLES AT EXECUTION TIME

Page 73: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILERINPUT STACK

titl = "None of the students are taller than 63 inches"; end; end; run;

let

% ;

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables Titl Some students are taller than 63 inches

data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;end; else do

PROCESSING MACRO VARIABLES AT EXECUTION TIME

Page 74: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILERINPUT STACK

end; run;;

end

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables Titl None of the students are taller than 63 inches

data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;end; else do;

PROCESSING MACRO VARIABLES AT EXECUTION TIME

Page 75: When best to use the %let statement, the symput routine, or the into clause to create macro variables

WORD SCANNER

TOKENNIZERDETERMINE

DESTINATION

COMPILERINPUT STACK

MACRO PROCESSOR GLOBAL SYMBOL TABLE

SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables Titl None of the students are taller than 63 inches

data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;end; else do; end; end; run;

PROCESSING MACRO VARIABLES AT EXECUTION TIME

Page 76: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES AT EXECUTION TIME

name sex height tall

1 John m 65 1

2 Tom m 60 0

3 Mary f 62 0

4 Helen f 64 1

Ex1:

data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do; %let titl = "Some students are taller than 63 inches"; end; else do; %let titl = "None of the students are taller than 63 inches"; end; end;run;

Here is what the program will look like when it is processed by the compiler

Page 77: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

To create a macro variable during the DATA step execution, use the SYMPUT routine

CALL SYMPUT (MACRO-VARIABLE, VALUE);

Both MACRO-VARIABLE and VALUE can be specified as

literal (text in quotations) a DATA step variable a DATA step expression

Page 78: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

CALL SYMPUT (MACRO-VARIABLE, VALUE);

Both arguments are literals

‘MACRO-VARIABLE’:Text enclosed in the quotation marks is the exact macro variable name

‘VALUE’:The exact value that is assigned to the MACRO-VARIABLE

Page 79: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do; call symput('titl', 'Some students are taller than 63 inches'); end; else do; call symput('titl', 'None of the students are taller than 63 inches'); end; end;run;

Use the SUMPUT routine to fix the previous program

Page 80: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

proc print data=ex1; title &titl;run;

Some students are taller than 63 inches

Obs name sex height tall 1 John m 65 1 2 Tom m 60 0 3 Mary f 62 0 4 Helen f 64 1

Page 81: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

CALL SYMPUT (MACRO-VARIABLE, VALUE);

When VALUE is a DATA step variable

A DATA step variable; not in quotation marks

You are assigning the value of a DATA step variable to the MACRO-VARIABLE

Any leading or trailing blanks that are part of the values of a DATA step variable will be part of the macro variables

For a numeric variable, the values will be converted to character variables by using the BEST12. format

Page 82: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

name sex height

1 John m 65

2 Tom m 60

3 Mary f 62

4 Helen f 64

Suppose that you would like to create four macro variables:

Height:

Macro Variable Names Values

John_ht 65

Tom_ht 60

Mary_ht 62

Helen_ht 64

Page 83: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

name sex height

1 John m 65

2 Tom m 60

3 Mary f 62

4 Helen f 64

data _null_; set height; if name = 'John' then call symput ('John_ht', height); else if name = 'Tom' then call symput ('Tom_ht', height); else if name = 'Mary' then call symput ('Mary_ht', height); else if name = 'Helen' then call symput ('Helen_ht', height);run;

Height:

HEIGHT is not in quotation marks

Page 84: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

name sex height

1 John m 65

2 Tom m 60

3 Mary f 62

4 Helen f 64

%put macro variable John_ht: &John_ht;%put macro variable Tom_ht: &Tom_ht;%put macro variable Mary_ht: &Mary_ht;%put macro variable Helen_ht: &Helen_ht;

Height:

359 %put macro variable John_ht: &John_ht;macro variable John_ht: 65360 %put macro variable Tom_ht: &Tom_ht;macro variable Tom_ht: 60361 %put macro variable Mary_ht: &Mary_ht;macro variable Mary_ht: 62362 %put macro variable Helen_ht: &Helen_ht;macro variable Helen_ht: 64

Log:

Page 85: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

CALL SYMPUT (MACRO-VARIABLE, VALUE);

When MACRO-VARIABLE is a DATA step variable

A DATA step variable; not in quotation marks

You are creating multiple macro variables The names of the macro variables are the values of a

DATA step variable

Page 86: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

name sex height

1 John m 65

2 Tom m 60

3 Mary f 62

4 Helen f 64

Suppose that you would like to create four macro variables:

Height:

Macro Variable Names Values

John 65

Tom 60

Mary 62

Helen 64

Page 87: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

name sex height

1 John m 65

2 Tom m 60

3 Mary f 62

4 Helen f 64

Height:

data _null_; set height; call symput (name, height);run;

NAME is not in quotation marks

Page 88: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

name sex height

1 John m 65

2 Tom m 60

3 Mary f 62

4 Helen f 64

Height:

%put macro variable John: &John;%put macro variable Tom: &Tom;%put macro variable Mary: &Mary;%put macro variable Helen: &Helen;

368 %put macro variable John: &John;macro variable John: 65369 %put macro variable Tom: &Tom;macro variable Tom: 60370 %put macro variable Mary: &Mary;macro variable Mary: 62371 %put macro variable Helen: &Helen;macro variable Helen: 64

Log:

Notice that “blanks” are part of the macro variables

Page 89: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

CALL SYMPUT (MACRO-VARIABLE, VALUE);

DATA step expressions can be used in one/both arguments

A DATA step expression

A DATA step expression

Page 90: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

name sex height

1 John m 65

2 Tom m 60

3 Mary f 62

4 Helen f 64

Suppose that you would like to create four macro variables:

Height:

Macro Variable Names Values

height_John 65

height_Tom 60

height_Mary 62

height_Helen 64

Also, remove the leading blanks

Page 91: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

name sex height

1 John m 65

2 Tom m 60

3 Mary f 62

4 Helen f 64

Height:

data _null_; set height; call symput ("height_" ||name, trim(left(height)));run;

Page 92: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUT ROUTINE

name sex height

1 John m 65

2 Tom m 60

3 Mary f 62

4 Helen f 64

Height:

%put macro variable height_John: &height_John;%put macro variable height_Tom: &height_Tom;%put macro variable height_Mary: &height_Mary;%put macro variable height_Helen: &height_Helen;

411 %put macro variable height_John: &height_John;macro variable height_John: 65412 %put macro variable height_Tom: &height_Tom;macro variable height_Tom: 60413 %put macro variable height_Mary: &height_Mary;macro variable height_Mary: 62414 %put macro variable height_Helen: &height_Helen;macro variable height_Helen: 64

Log:

Page 93: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUTX ROUTINE

The SYMPUTX routine is an improved version of the SYMPUT routine

The SYMPUTX routine can remove leading and trailing blanks from both arguments

For example, the previous program can be re-written as below:

data _null_; set height; call symputx ("height_" ||name, height);run;

Page 94: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUTX ROUTINE

%put macro variable height_John: &height_John;%put macro variable height_Tom: &height_Tom;%put macro variable height_Mary: &height_Mary;%put macro variable height_Helen: &height_Helen;

429 %put macro variable height_John: &height_John;macro variable height_John: 65430 %put macro variable height_Tom: &height_Tom;macro variable height_Tom: 60431 %put macro variable height_Mary: &height_Mary;macro variable height_Mary: 62432 %put macro variable height_Helen: &height_Helen;macro variable height_Helen: 64

Log:

Page 95: When best to use the %let statement, the symput routine, or the into clause to create macro variables

THE SYMPUTX ROUTINE

Difference between CALL SYMPUTX and CALL SYMPUT

CALL SYMPUTX CALL SYMPUT

Does not write a note to the SAS log when the 2nd argument is numeric

Writes a note to the SAS log when the 2nd argument is numeric

Uses a field width of up to 32 characters for converting a numeric 2nd argument to characters

Uses a field width of up to 12 characters for converting a numeric 2nd argument to characters

Left-justifies both arguments and trims trailing blanks

Does not left-justify the arguments and trims trailing blanks from the first argument only

Enables you to specify the symbol table

Does not enable you to specify the symbol table

Page 96: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

You can create/update macro variables during the execution of PROC SQL

The INTO clause in the SELECT statement performs a similar role to the SYMPUT(X) routine in the DATA step

The INTO clausecan create one or more macro variables can assign a calculated result or the value of a data

variable to a macro variablecan only be used in the outer query of a SELECT

statement cannot be used when you are creating a table or a

view

Page 97: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

Here is the general syntax for PROC SQL and the INTO clause:

PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1 <, :macro-variable2, …>FROM table |view<other clauses>; QUIT;

To select one or more columns from SQL table/view, you can specify column1, column2, … after the keyword SELECT

Page 98: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

Here is the general syntax for PROC SQL and the INTO clause:

PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1 <, :macro-variable2, …>FROM table |view<other clauses>; QUIT;

Macro-variable1, macro-variable2 are the names of the macro variables that you are creating

Page 99: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

Here is the general syntax for PROC SQL and the INTO clause:

PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1 <, :macro-variable2, …>FROM table |view<other clauses>; QUIT;

You must write colon(s) (:) before each of the macro variables

Page 100: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

Here is the general syntax for PROC SQL and the INTO clause:

PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1 <, :macro-variable2, …>FROM table |view<other clauses>; QUIT;

The INTO clause does not trim the leading and trailing blanks of the macro variables

Page 101: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

Here is the general syntax for PROC SQL and the INTO clause:

PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1 <, :macro-variable2, …>FROM table |view<other clauses>; QUIT;

To suppress display-output from PROC SQL, you can use the NOPRINT option

Page 102: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

You can assign a calculated summary statistic to a macro variable

Suppose you would like to create a macro variable MEAN_HT - average heights of students

proc sql noprint; select mean(height) into: mean_ht from height;quit;

%put macro variable mean_ht: &mean_ht;

523 %put macro variable mean_ht: &mean_ht;macro variable mean_ht: 62.75Log:

Page 103: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

Without using the PROC SQL, you must use multiple steps to accomplish this task

proc means data=height noprint; var height; output out=height_mean mean=ht_mean;run;

data _null_; set height_mean; call symputx('mean_ht1', ht_mean);run;

Page 104: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

In the previous program, we created four macro variables:data _null_; set height; if name = 'John' then call symput ('John_ht', height); else if name = 'Tom' then call symput ('Tom_ht', height); else if name = 'Mary' then call symput ('Mary_ht', height); else if name = 'Helen' then call symput ('Helen_ht', height);run;

We can use PROC SQL to achive the same taskproc sql noprint; select height into: John_ht1 from height where name = 'John'; select height into: Tom_ht1 from height where name = 'Tom'; select height into: Mary_ht1 from height where name = 'Mary'; select height into: Helen_ht1 from height where name = 'Helen';quit;

Page 105: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

You can create a range of macro variablesEach of the macro variables will contain each row in the

result of the SELECT statement

PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1_1 - :macro-variable1_n < NOTRIM> <, :macro-variable2_1 - :macro-variable2_n < NOTRIM>, …>FROM table |view<other clauses>; QUIT;

Page 106: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1_1 - :macro-variable1_n < NOTRIM> <, :macro-variable2_1 - :macro-variable2_n < NOTRIM>, …>FROM table |view<other clauses>; QUIT;

name sex height

1 John m 65

2 Tom m 60

3 Mary f 62

4 Helen f 64

name1name2name3name4

height1height2height3height4

into :name1 - :name4, :height1 - :height4

Page 107: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1_1 - :macro-variable1_n < NOTRIM> <, :macro-variable2_1 - :macro-variable2_n < NOTRIM>, …>FROM table |view<other clauses>; QUIT;

By default, the leading and trailing blanks are removed from values before they are stored in macro variables

If you don’t want to remove the leading and trailing blanks, you can use the NOTRIM option

Page 108: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

Suppose that you would like to create macro variables NAME1 – NAME4 and HEIGHT1 – HEIGHT4 to store these four students’ names and their heightsproc sql noprint; select name, height into :name1 - :name4,

:height1 - :height4 from height;quit;

%put macro variable name1: &name1;%put macro variable name2: &name2;%put macro variable name3: &name3;%put macro variable name4: &name4;%put macro variable height1: &height1;%put macro variable height2: &height2;%put macro variable height3: &height3;%put macro variable height4: &height4;

Page 109: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

Suppose that you would like to create macro variables NAME1 – NAME4 and HEIGHT1 – HEIGHT4 to store these four students’ names and their heights663 %put macro variable name1: &name1;macro variable name1: John664 %put macro variable name2: &name2;macro variable name2: Tom665 %put macro variable name3: &name3;macro variable name3: Mary666 %put macro variable name4: &name4;macro variable name4: Helen667 %put macro variable height1: &height1;macro variable height1: 65668 %put macro variable height2: &height2;macro variable height2: 60669 %put macro variable height3: &height3;macro variable height3: 62670 %put macro variable height4: &height4;macro variable height4: 64

Page 110: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

When creating a range of macro variables, PROC SQL restricts the name convention for the macro variables

The names of the macro variables must end with an integer with a valid range, such as HEIGHT1 – HEIGHT4

You won’t be able to use more meaningful names such as JOHN_HT, TOM_HT, MARY_HT, and HELEN_HT

Page 111: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

You can use the INTO clause to create a macro variable that holds all the values of a column by concatenating them and separating them by a delimiter

PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …> INTO :macro-variable1 SEPARATED BY ‘delimiter1’ <, :macro-variable2 SEPARATED BY ‘delimiter2’, …>FROM table |view<other clauses>; QUIT;

Delimiter1 is used to separate all the values in the column and must be enclosed in quotation marks

Page 112: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

Create macro variables

name sex height

1 John m 65

2 Tom m 60

3 Mary f 62

4 Helen f 64

Macro Variable Value

NAMELIST John Tom Mary Helen

HEIGHTLIST 65,60,62,64

Page 113: When best to use the %let statement, the symput routine, or the into clause to create macro variables

PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL

proc sql noprint; select name, height

into : namelist separated by ' ', : heightlist separated by ','

from height;quit;

%put macro variable namelist: &namelist;%put macro variable heightlist: &heightlist;

871 %put macro variable namelist: &namelist;macro variable namelist: John Tom Mary Helen872 %put macro variable heightlist: &heightlist;macro variable heightlist: 65,60,62,64

Page 114: When best to use the %let statement, the symput routine, or the into clause to create macro variables

CONCLUSION

Creating a macro variable by using the %LET statement occurs before the execution of any other SAS language statements

To create a macro variable during the DATA step execution, you must use either the SYMPUT or SYMPUTX routines

To create a macro variable during the execution of PROC SQL, you must use the INTO clause

Understanding the mechanisms of creating macro variables is an important foundation for learning how best to write macro programs

Page 115: When best to use the %let statement, the symput routine, or the into clause to create macro variables

REFERENCES

Burlew, Michele M. SAS® Macro Programming Made Easy, 2nd Edition.

SAS Online Doc® 9.1.3. Cary, NC: SAS Institute Inc.

Page 116: When best to use the %let statement, the symput routine, or the into clause to create macro variables

ACKNOWLEDGEMENT

I would like to thank Kathryn McLawhorn and Scott McElroy, Technical Support Analysts from SAS Technical Support, for their valuable programming suggestions and insight

Page 117: When best to use the %let statement, the symput routine, or the into clause to create macro variables

CONTACT INFORMATION

Arthur Li

City of Hope

Division of Information Science

1500 East Duarte Road

Duarte, CA 91010 - 3000

Phone: (626) 256-4673 ext. 65121

E-mail: [email protected]