没有合适的资源?快使用搜索试试~ 我知道了~
Macro Quoting Functions PharmaSUG-2018-BB20.pdf
需积分: 9 0 下载量 200 浏览量
2020-06-30
16:02:10
上传
评论
收藏 150KB PDF 举报
温馨提示
PharmaSUG 2018 - Paper BB-20 The Fundamentals of Macro Quoting Functions Arthur Li, City of Hope National Medical Center, Duarte, CA
资源推荐
资源详情
资源评论
1
PharmaSUG 2018 - Paper BB-20
The Fundamentals of Macro Quoting Functions
Arthur Li, City of Hope National Medical Center, Duarte, CA
ABSTRACT
The Macro quoting functions is one of the most difficult concepts to grasp when one learns to write a macro program.
One of the reasons for its complexity is closely related to understanding the macro processing, such as macro
compilation and execution phases. Furthermore, the situations for utilizing macro quoting functions are not obvious
for programmers. In this talk, in addition to reviewing the macro processing, we will examine the most useful macro
quoting functions via various examples.
INTRODUCTION
The macro language is a character-based language. That means even numeric values are treated as characters by
the macro processor. The purpose of using the macro language is used to generate SAS codes based on our
instruction. For example, when we write characters such as % and & signs, which are used to signal the macro
processor to perform tasks such as creating macro variables, retrieving values of macro variables, and calling macro.
But in some situations, & and % signs need to be treated simply as texts. The mnemonics, such as OR or GE, are
operators in macro language expressions. In some situations, these special characters or mnemonics only need to be
interpreted as texts as well. Therefore, we need to use the macro quoting functions to mask the special meanings so
that the macro processor will be able to interpret them correctly.
WHY DO WE NEED TO USE THE QUOTING FUNCTIONS?
Here’re a couple of situations where the macro quoting is needed. For example, to create a macro variable, we can
use the %LET statement. The following example attempts to store “proc print data=foo; run;” in the macro
variable &PRINT_FOO.
Code Chunk 1:
%let print_foo = proc print data=foo; run;;
%put &print_foo;
SAS Log:
216 %let print_foo = proc print data=foo; run;;
217 %put &print_foo;
proc print data=foo
The macro variable &PRINT_FOO is not created as we intended and it only stores the value of “proc print
data=foo” since the macro processor treats the first semicolon, which is after “foo”, as the end of the %LET
statement. In this situation, we need to mask the meaning of the semicolons and let the macro processor treat the
semicolons simply as text.
In the following example, the macro variable &NAME stores the value of “Doe, John”. In the second %LET statement,
&NAME needs to be resolved first in the %SUBSTR function. However, the resolution of &NAME contains a comma
which creates a function call that contains four arguments. We need to mask the meaning of commas to prevent the
macro processor from misinterpreting the comma in the resolved values as a separator for the %SUBSTR function.
Code Chunk 2:
%let name = Doe, John;
%let initial = %substr(&name, 1, 1);
%put &initial;
SAS Log:
228 %let name = Doe, John;
229 %let initial = %substr(&name, 1, 1);
ERROR: Macro function %SUBSTR has too many arguments.
230 %put &initial;
The Fundamentals of Macro Quoting Functions, continued
2
TYPES OF MACRO QUOTING FUNCTIONS
During the macro processing, we need to let the macro processor use the macro quoting functions to mask the
following characters and mnemonics:
& % blank , ; + - * / < > =
┑
^ ~ # | ' " ( )
AND OR NOT EQ NE LE LT GE GT IN
These quoting functions can be categorized into compilation and execution quoting functions.
The compilation quoting functions are %STR and %NRSTR. This type of quoting function is used when a user types
special characters in the open code, such as in the %LET or %PUT statements. They can also be used in a
programming logic statement such as %IF statement. And we use these functions when a user supplies parameter
values on a macro call. During the macro compilation phase, using the compilation quoting functions will treat
characters as text in the macro program.
Other than the %STR and %NRSTR functions, other macro quoting functions are mostly used during the macro
execution. During the macro execution, the execution macro functions mask resolved values from macro variables.
The macro quoting functions generally come in pairs. The function that begins with NR, such as %NRSTR masks all
the special characters listed above. The function that does not begin with NR, such as %STR, masks the special
characters except for ampersands (&) and percent signs (%). The letters NR are short for “non-resolved”. The
function that starts with NR prevents macro or macro variable resolution.
There are also functions that with letter B (for “by itself”), such as %BQUOTE and %NRBQUOTE, which are useful
for quoting unmatched quotation marks or parentheses.
COMPILATION QUOTING FUNCTIONS
The %STR and %NRSTR functions mask special characters and mnemonics during the macro compilation phase.
%STR(character-string)
%NRSTR(character-string)
Both functions mask the special character or mnemonics that are listed above, except that the %STR function does
not mask & and %. When masking ' " ( ), we need to place a percent sign (%) in front of these characters.
THE %STR FUNCTION
The example in Code Chunk 1 can be modified by using the %STR function. The %STR function in Code Chunk 3
masks the meaning of semicolons and the macro processor treats the semicolons within the %STR function as texts.
Code Chunk 3:
%let print_foo = %str(proc print data=foo; run;);
&print_foo
SAS Log:
235
236 %let print_foo = %str(proc print data=foo; run;);
237 &print_foo
NOTE: There were 1 observations read from the data set WORK.FOO.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.10 seconds
cpu time 0.01 seconds
The following macro FIRST_LAST is used to separate first and last name of a given full name. The full name is
separated by using a comma, which is argument value for the FRIST_LAST macro. The two %LET statements are
used to create two macro variables by using the %SCAN function to extract the first and last name of the macro
variable &NAME, which is the parameter of the FIRST_LAST macro. The default separator, which is including a
comma, is used as the separator for the %SCAN function.
剩余9页未读,继续阅读
资源评论
Miya_o00
- 粉丝: 1177
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 淘宝客项目,支持App,微信小程序,QQ小程序.zip
- 第16课作业周旭.iml
- 海风小店,商城,微信小开源程序商城服务器端.zip
- 技术资料分享uCOS-II原理3很好的技术资料.zip
- 【java毕业设计】食品安全管理系统源码(springboot+vue+mysql+说明文档+LW).zip
- 海风小店,商城,微信小开源程序商城管理后台,后台管理,VUE.zip
- 【java毕业设计】流浪动物救助平台源码(springboot+vue+mysql+说明文档+LW).zip
- 技术资料分享uCOS-II原理2很好的技术资料.zip
- 流动图书馆微信小程序引入.zip
- 【java毕业设计】某大学外卖系统源码(springboot+前端+mysql+说明文档+LW).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功