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,对于所有模块都成立
当程序进入后会做以下五件事