sas宏(1)、系统宏变量、自定义宏变量、输出宏变量值、宏与text结合

SAS macro variables

  1. enable you to substitute text in your SAS programs(替代作用,和c++的 #define 差不多)

  2. When you reference a macro variable in a SAS program, SAS replaces the reference with the text value that has been assigned to that macro variable. By substituting text into programs, SAS macro variables make your programs more reusable and dynamic

  3. 判断宏变量定义的结束是以分号为分隔符。

为什么要使用宏变量?

因为,宏能起到一处替换,多处替换的效果!

宏变量储存在哪?

The value of a macro variable is stored in a symbol table

The values of automatic macro variables are always stored in the global symbol table(意味着你总可以引用到这些宏)

The values of user-defined macro variables can reside either in a macro symbol table local or in the global symbol table.(自定义的宏变量大部分也储存到全局符号表中)

宏变量的使用范围有多大?

除了datalines步,其他任何地方都可以使用!

如何删除宏?

%symdel mvariable;

 

1:宏变量

如何使用宏变量?

使用引号符号(&)对宏进行引用,当sas程序遇到&时,会在symbol table中搜寻同名变量,进而得出变量中的值。如果要在字符串中进行宏的引用,需要用双引号,SAS不会对单引号中宏的引用进行任何处理

引用宏时,对宏变量的大小写无要求,就是&sex,&Sex得出的结果是一样

title "June Totals as of &sysdate"; 
%let sex='';
data juntot; 
set sashelp.class;  
if sex = &sex; 
run; 

1.1:Automatic Macro Variables (用来创建脚注十分好) 

Automatic macro variables contain information about your computing environment, such as the date and time of the session, and the version of SAS you are running.

性质:sas系统启动时创建,全局的,通常由sas赋值,有时可以由用户自己赋值

footnote1 "Created &systime &sysday, &sysdate9";   
footnote2 "on the &sysscp system using Release &sysver";

 

 

1.2:User-Defined Macro Variables  

最基本形式与相关规定如下图

宏的一些常用用法

当定义了一个宏后,要记住以下几点:

1:所有的值都是以character strings的形式储存(有个地方不是写的string,不记得是哪,以这个为标准)

2:value的case(大小写)被保留

3:数学运算符号不被运算(如下表的第五条,那么&sum*&sum就等于4+3*4+3=19)

4:SAS 会将宏对应的值在放到macro symbol时自动移除leading and trailing blanks,除非你用一些特殊的宏函数进行处理。

5:单引号也被包含在宏值内 (比如下图行2)

6:variable的命名好像没有任何限制,因为连proc 和run都可以用、、、、暂时没找到限制

虽然说是以character strings的形式储存,但是这并不就是从字面上以为着就是字符串,我自己写了个ex,log如下

177  %let xx="xx";
178  %let yy=yyy;
179  data _null_;
180      k0 = (&xx=xx);
181      k1 = (&xx="xx");
182      k2 = (&yy=yyy);
183      put k0= k1= k2=;
184  run;

NOTE: 变量 xx 未初始化。
NOTE: 变量 yyy 未初始化。
k0=0 k1=1 k2=1

 

 

宏处理器、Word Scanner、Input Stack 运行带宏的程序的具体流程

当submit program时,程序会进入input stack,对于所有模块都成立

当程序进入后会做以下五件事

? reads the text in the input stack (left-to-right, top-to-bottom)
? routes text to the appropriate compiler upon demand
? suspends this activity when a step boundary such as a RUN statement is reached
? executes the compiled code if there are no compilation errors
? repeats this process for any subsequent steps.

总的来说就是,将代码按从左到右从上到下的顺序读入缓存栈,然后将对应的代码分配给对应的编译器进行编译,如果遇到了run语句就执行。

执行完后如果后面还有语句就继续按上面的步骤重复执行! 

 

在input stack和compiler之间,sas程序会被一个叫word scanner的东西分解成小的token

? Tokens are passed on demand to the compiler.
? The compiler requests tokens until it receives a semicolon.(编译器在遇到分号时会请求上次读入的全部tokens)

? The compiler performs a syntax check on the statement.(然后编译器在语句上做语法检查)

 

某些token是macro trigger(like & %)会通知Word scanner接下来相关的code要被移交到macro processor去处理

宏处理器(macro processor)会进行如下工作

? examines these tokens
? requests additional tokens as necessary
? performs the action indicated.

对于宏变量,宏处理器会做以下几件事

? creates a macro variable in the symbol table and assigns a value to the variable
? changes the value of an existing macro variable in the symbol table
? looks up an existing macro variable in the symbol table and returns the variable's value to the input stack in place of the original reference.

图例 448 - 457

1.3:输出宏变量的值的几种方式

1.3.1:系统选项--->>options SYMBOLGEN;

在log中显示每个宏被调用时所使用的值,包括系统宏变量和用户自定义宏变量

它的关键字是解析或者resolve to

SYMBOLGEN:  宏变量 X 解析为 3

1.3.2:%put语句

%put <text | _ALL_ | _AUTOMATIC_ | _GLOBAL_ | _LOCAL_ | _USER_ | ERROR:... | WARNING:...| NOTE:... >;

  1. The text option includes both literal text and references to macro variables.
  2. The next five options are keywords that list different classifications of macro variables.
  3. The last three options are keywords that can simulate color-coded SAS messages. The ‘…’ represents the text that you want the %PUT statement to display following the keyword.

  

 

5:宏与text结合

宏跟在text后面,形成新的text

%let year=02;
%let month=jan;
proc
chart data=sasuser.y&year&month;编译前 proc chart data=sasuser.y02jan;编译后
%let year=02;
%let month=jan;
proc chart data=sasuser.y&year&month;
hbar week / sumvar=sale;
run;
proc plot data=sasuser.y&year&month;
plot sale*day;
run;

 

宏在text之前,在text与宏之间加特殊的符号,以便能被分解为token

%let var=sale;
plot &var*day;

 对于一般的前连接

%let graphic=g;

proc &graphicplot ...
想表达的意思是proc gplot,但是c p之间无分隔符,不能被分解为token,所以要想表达完整的意思需使用 proc &graphic.plot ->>>proc gplot
%let graphics=g;
%let year=02;
%let month=jan;
%let var=sale;
proc &graphics.chart data=sasuser.y&year&month;
hbar week / sumvar=&var;
run;
proc &graphics.plot data=sasuser.y&year&month;
plot &var*day;
run;

 

转载于:https://www.cnblogs.com/yican/p/4090487.html

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值