SAS macro variables
enable you to substitute text in your SAS programs(替代作用,和c++的 #define 差不多)
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
为什么要使用宏?
因为,宏能起到一处替换,多处替换的效果!
宏储存在哪?
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 are often stored in the global symbol table as well.(自定义的宏变量大部分也储存到全局符号表中)
宏的使用范围有多大?
除了datalines步,其他任何地方都可以使用!
1:宏变量
如何使用宏变量?
使用引号符号(&)对宏进行引用,当sas程序遇到&时,会在symbol table中搜寻同名变量,进而得出变量中的值。如果要在字符串中进行引用,需要用双引号。
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
最基本形式与相关规定如下图
宏的一些常用用法
关于宏处理器、Word Scanner、Input Stack 运行带宏的程序的具体流程,查看445-453页
1.3:输出宏的值的几种方式
1.3.1:系统选项--->>options SYMBOLGEN;
是的log中显示每个宏被调用时所使用的值
1.3.2:%put语句
%put The value of the macro variable CITY is: &city; %put + 语句 + 宏引用;
该语句还有三个已定义好的参数
2:Using Macro Functions to Mask Special Characters(用宏函数来遮盖特殊符号)
宏函数都是要以%开头
options symbolgen; %let prog=data new; x=1; run; &prog proc print; run; /*如果目标是想运行整个let一行我们想表达的值,但是由于在第一个分号,宏的value定义就已经结束,所以是得不到我们想要的结果*/
用宏函数来遮盖!!!!!!!!!!
%str(argument),可处理的符号有 ; + - * / , < > = blank LT EQ GT AND OR NOT LE GE NE
The %STR function is used to mask (or quote) tokens so that the macro processor does not interpret them as macro-level syntax
%let prog=%str(data new; x=1; run;); /*这样引用就能达到想要的效果,宏处理器在处理的时候会对分号mask*/
options symbolgen; %let text=Joan‘s Report; /*宏处理器处理时,单引号被解释成字符串的开始,然而并为找到结束,可以用str来去掉这层意思,中间要做特殊处理*/ %let test=%str(Joan%‘s Report); proc print data=sasuser.courses; where days > 3; title "&text"; run;
When you quote tokens that typically appear in pairs, such as quotation marks or parentheses, you must take one additional step.
To perform this quoting, you precede the token that you want to quote with a percent sign (%) within the %STR function argument.
对于成对出现的符号,在进行处理时要做特殊的处理,加上%
%Nrstr(argument) str的增强版,但是多包含& and %
其他功能的宏函数,他们返回的都不是字符串,得到什么就返回什么,不做任何改动
%upcase
%qupcase加强版包含了&%
%SUBSTR ( argument, position <,n> ) n p可以为数值也可以为表达式,p的位置为子字符串首字母位置
%qsubstr 同上,加强版
%length
%index(source,string):The %INDEX function enables you to determine the position of the first character of a string within another string
%let a=a very long value; %let b=%index(&a,v); %put V appears at position &b.;
%let sitelist=DALLAS SEATTLE BOSTON;
%index(&sitelist,LA); /*结果为4*/
%SCAN ( argument , n <,delimiters> ):enables you to extract words from the value of a macro variable.分隔符不需要单引号包围
9241 data work.thisyear; 9242 set sasuser.schedule; 9243 where year(begin_date) = 9244 year("&sysdate9"d); 9245 run; 9246 %let libref=%scan(&syslast,1,.); 9247 %let dsname=%scan(&syslast,2,.); 9249 data; 9250 %put libref= &libref; libref= WORK 9251 %put dsname= &dsname; dsname= THISYEAR 9252 run;
%QSCAN 加强版
%SYSFUNC ( function ( argument(s) ) <,format> )引用sas函数
title "%sysfunc(today(),weekdate.) - SALES REPORT";
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;