SAS Macro 由两部分组成:Macro variables and Macro. 宏变量是以字符存储的。

Macro variable :

  • 命名规范:需要遵循 SAS 变量命名规范(不超过32 characters, 以下划线或字母开始,只包含数字、字母或下划线)。Macro variable value 长度不超过64000 characters.
  • Local & Global : Local macro variable is defined insides the macro, which can be used only in the macro. Global macro is defined outsides the macro, which can be used exerywhere in the program. Local macro variable 可以被转换成 Global macro variable, 反之亦然。生成全局或局部宏变量:%global or %local statement
    %GLOBAL macro-varriable-1 macro-varriable-2 macro-varriable-n;
    %let macro-varriable-1=var1;
    %let macro-varriable-2=var2;
    
    %LOCAL macro-varriable-1 macro-varriable-2 macro-varriable-n;
    %let macro-varriable-1=var1;
    %let macro-varriable-2=var2;

    If you define both a global macro variable and a local macro variable with the same name, the macro processor uses the value of the local variable during the execution of the macro that contains the local variable. When the macro that contains the local variable is not executing, the macro processor uses the value of the global variable.

  • 使用宏变量:给宏变量添加前缀&就可以调用宏变量。当需要把 macro variable 放进引号时,由于SAS macro process无法识别单引号,必须将其放进双引号。
%let iterations = 10;
%let country = New Zealand;

DO i = 1 to &iterations;
TITLE "Addresses in &country";

                      

  •  常用系统宏变量:
    • &sysindex : Contains the number of macros that have started execution in the current SAS job or session. You can use SYSINDEX in a program that uses macros when you need a unique number that changes after each macro invocation. 当宏调用时,会自动产生这个变量,&sysindex的值在调用不同宏时都不一样,是一个在当前进程中的 unique value. 可以利用这一特性给变量名赋值。比如:
      %macro mc();
          array _a&sysindex. [3] var1 var2 var3;
      
          do i=1 to 3;
              if _a&sysindex.[i]="" then _a&sysindex.[i]="0";
          end;
      %mend;      
    • &SYSPARM : enables you to pass a character string from the operating environment to SAS program steps and provides a means of accessing or using the string while a program is executing. The default value of SYSPARM is null (zero characters). (1) Assigning a value to SYSPARM is the same as specifying a value for the SYSPARM= system option. (2) Retrieving the value of SYSPARM is the same as using the SYSPARM() SAS function. 例子:
      In this example, you invoke SAS on a UNIX operating environment on September 20, 2001 (the librefs DEPT and TEST are defined in the config.sas file) with a command like the following:
      
      sas program-name -sysparm dept.projects -config /myid/config.sas
      
      Macro variable SYSPARM supplies the name of the data set for PROC REPORT:
      proc report data=&sysparm
           report=test.resorces.priority.rept;
      title "%sysfunc(date(),worddate.)";
      title2;
      title3 'Active Projects By Priority';
      run;
      
      SAS sees the following:
      proc report data=dept.projects
           report=test.resorces.priority.rept;
      title "September 20, 2001";
      title2;
      title3 'Active Projects By Priority';
      run;
    • &_SASPROGRAMFILE: The full path and filename of the SAS program that is currently being run. This macro variable is available only for SAS program files that are saved on the same server on which your SAS Enterprise Guide code is being run.  The _sasprogramfile would only be set when the program was opened via the server (it would not be set when the program was opened through the context of the local file system). For example, a  _sasprogramfile can be like '/home/username/data/mycode.sas'. The EG server usually looks like 

       

       

       

       

Macro :

%MACRO macro-name;
    macro-text
%MEND macro-name;
  • 命名规范:Macro name 要遵循和 macro variable name 一致的命名规范。Macro name in %mend is optional but it's better to have it.
  • 添加逻辑判断:%IF-%THEN 比 IF-THEN 的功能更多,比如包含在 DATA,PROC 步外层。其他带%的逻辑语句也是。
%IF condition %THEN action;
    %ELSE %IF condition %THEN action;
    %ELSE action;

%IF condition %THEN %DO;
    SAS statements
%END;

在 macro 运行中,会自动产生一些系统宏变量,常见的有:

&SYSDATE : the character value of the date that job or session began,e.g. 28MAY08

&SYSDAY:the day of the week that job or session began,e.g.Wednesday

 

  • CALL SYMPUT("macro-variable-name",value) : 将DATA步的值赋值给一个宏变量,使后面的代码可以调用它。value 可以是一个变量或一个具体的值,当时一个具体的值时,必须放进引号。
IF Age >= 18 THEN CALL SYMPUT("status", "Adult");
ELSE CALL SYMPUT("status", "Minor");

IF TotalSales > 1000000 THEN CALL SYMPUT("bestseller", BookTitle);

注意:由于只有在DATA步运行后才会将值赋给宏变量,所以用CALL SYMPUT创建的宏变量无法在它所在的当前DATA步中使用,只能在后面的代码语句中使用。

 

  • System Options for macro debugging: 在执行 macro 前面添加 options 帮助输出 macro 执行的一些信息。常用的 options :粗体是选项的默认设置。

/*Usage*/
OPTIONS
MPRINT NOSYMBOLGEN NOMLOGIC;


/*
Options MPRINT*/ %macro mktitle(proc,data); title "%upcase(&proc) of %upcase(&data)"; %mend mktitle; options mprint; %mktitle; /*Directing MPRINT Output to an External File - use mfile*/ options mfile mprint; filename mprint 'debugmac';

 

posted on 2022-10-04 16:35  MOZY  阅读(378)  评论(0编辑  收藏  举报