Sas 简明教程

SAS - Macros

SAS 有一个名为 Macros 的强大编程功能,该功能允许我们避免使用代码的重复部分,并在需要时多次使用它们。它还有助于在代码中创建动态变量,这些变量可以在同一代码的不同运行实例中采用不同的值。对于将多次以类似宏变量的方式重新使用的代码块,还可以声明宏。我们在下面的示例中将看到这两个。

SAS has a powerful programming feature called Macros which allows us to avoid repetitive sections of code and to use them again and again when needed. It also helps create dynamic variables within the code that can take different values for different run instances of the same code. Macros can also be declared for blocks of code which will be reused multiple times in a similar manner to macro variables. We will see both of these in the below examples.

Macro variables

这些变量包含将被 SAS 程序反复使用的值。它们在 SAS 程序的开头进行声明,并在程序主体中进行后期调用。它们在作用域中可以是全局的或局部的。

These are the variables which hold a value to be used again and again by a SAS program. They are declared at the beginning of a SAS program and called out later in the body of the program. They can be Global or Local in scope.

Global Macro variable

它们被称为全局宏变量,因为任何在 SAS 环境中可用的 SAS 程序都可以访问它们。一般而言,它们是由系统分配的变量,多个程序访问这些变量。系统日期就是一个通用的示例。

They are called global macro variables because they can accessed by any SAS program available in the SAS environment. In general they are the system assigned variables which are accessed by multiple programs. A general example is the system date.

Example

下面是名为 SYSDATE 的 SAS 变量的一个示例,它表示系统日期。考虑这样一个情景,在每天生成的报表标题中打印系统日期。标题将显示当前日期和星期,而我们并不会为此对它们进行任何值编码。我们使用 SASHELP 库中可用的称为 CARS 的内置 SAS 数据集。

Below is a example of the SAS variable called SYSDATE which represents the system date. Consider a scenario to print the system date in the title of the SAS report every day the report is generated. The title will show the current date and day without we coding any values for them. We use the in-built SAS data set called CARS available in the SASHELP library.

proc print data = sashelp.cars;
where make = 'Audi' and type = 'Sports' ;
   TITLE "Sales as of &SYSDAY &SYSDATE";
run;

运行上述代码后,我们得到以下输出。

When the above code is run we get the following output.

global macro result

Local Macro variable

这些变量可以通过 SAS 程序访问,其中它们被声明为程序的一部分。它们通常用于向同一个 SAS 语句 sl 提供不同的变量,使它们可以处理数据集的不同观测值。

These variables can be accessed by SAS programs in which they are declared as part of the program. They are typically used to supply different variables to the same SAS statements sl that they can process different observations of a data set.

Syntax

使用以下语法声明局部变量。

The local variables are declared with below syntax.

% LET (Macro Variable Name) = Value;

此处,根据程序要求,值字段可以采用任何数字、文本或日期值。宏变量名称是任何有效的 SAS 变量。

Here the Value field can take any numeric, text or date value as required by the program. The Macro variable name is any valid SAS variable.

Example

SAS 语句使用 & 字符追加在变量名开头,由此使用这些变量。下面的程序为我们获取所有关于品牌为“奥迪”且类型为“跑车”的观测值。如果我们想要 different make 的结果,我们需要更改变量 make_name 的值而不更改程序的其他任何部分。在调用程序时,此变量可以在任何 SAS 语句中一遍又一遍地引用。

The variables are used by the SAS statements using the & character appended at the beginning of the variable name. Below program gets us all the observation of the make 'Audi' and type 'Sports'. In case we want the result of different make, we need to change the value of the variable make_name without changing any other part of the program. In case of bring programs this variable can be referred again and again in any SAS statements.

%LET make_name = 'Audi';
%LET type_name = 'Sports';
proc print data = sashelp.cars;
where make = &make_name and type = &type_name ;
   TITLE "Sales as of &SYSDAY &SYSDATE";
run;

运行上述代码后,我们得到的输出与前一个程序相同。但是,让我们将 type name 更改为 'Wagon' 并运行同一程序。我们将得到以下结果。

When the above code is run we get the same output as the previous program. But let’s change the type name to 'Wagon' and run the same program. We will get the below result.

local macro result

Macro Programs

宏是一组 SAS 语句,用名称引用它们,并在程序的任何位置使用该名称。它们以 %MACRO 语句开头,以 %MEND 语句结尾。

Macro is a group of SAS statements that is referred by a name and to use it in program anywhere, using that name. It starts with a %MACRO statement and ends with %MEND statement.

Syntax

使用以下语法声明局部变量。

The local variables are declared with below syntax.

# Creating a Macro program.
%MACRO <macro name>(Param1, Param2,….Paramn);

Macro Statements;

%MEND;

# Calling a Macro program.
%MacroName (Value1, Value2,…..Valuen);

Example

以下程序在名为 'show_result' 的宏下声明了一组 SAS 语句。此宏由其他 SAS 语句调用。

The below program decalres a group of SAT staemnets under a macro named *'show_result'; *This Macro is being called by other SAS statements.

%MACRO show_result(make_ , type_);
proc print data = sashelp.cars;
where make = "&make_" and type = "&type_" ;
   TITLE "Sales as of &SYSDAY &SYSDATE";
run;
%MEND;

%show_result(BMW,SUV);

运行上述代码后,我们得到以下输出。

When the above code is run we get the following output.

program macro result

Commonly Used Macros

SAS 编程语言中有许多内置的 SAS MACRO 语句。其他 SAS 程序可以使用它们,而不必显式声明它们。常见示例有 - 当满足某些条件时终止程序或者在程序日志中捕获变量的运行时值。以下是一些示例。

SAS has many MACRO statements which are in-built in the SAS programming language. They are used by other SAS programs without explicitly declaring them.Common examples are - terminating a program when some condition is met or capturing the runtime value of a variable in the program log. Below are some examples.

Macro %PUT

此宏语句将文本或宏变量信息写入 SAS 日志。在以下示例中,将变量“today”的值写入程序日志。

This macro statement writes text or macro variable information to the SAS log. In the below example the value of the variable 'today' is written to the program log.

data _null_;
CALL SYMPUT ('today',
TRIM(PUT("&sysdate"d,worddate22.)));
run;
%put &today;

运行上述代码后,我们得到以下输出。

When the above code is run we get the following output.

macro put

Macro %RETURN

执行此宏时,当特定条件评估为“真”时,将导致当前执行的宏正常终止。在下例中,当变量 "val" 的值变为 10 时,宏终止,否则继续。

Execution of this macro causes normal termination of the currently executing macro when certain condition evaluates to be true. In the below examplewhen the value of the variable "val" becomes 10, the macro terminates else it contnues.

%macro check_condition(val);
   %if &val = 10 %then %return;

   data p;
      x = 34.2;
   run;

%mend check_condition;

%check_condition(11)  ;

运行上述代码后,我们得到以下输出。

When the above code is run we get the following output.

macro return

Macro %END

此宏定义包含一个 %DO %WHILE 循环,该循环根据要求以 %END 语句结束。在下例中,名为 test 的宏采用用户输入并使用此输入值运行 DO 循环。DO 循环的结束通过 %end 语句实现,而宏的结束通过 %mend 语句实现。

This macro definition contains a %DO %WHILE loop that ends, as required, with a %END statement. In the below example the macro named test takes a user input and runs the DO loop using this input value. The end of DO loop is achieved through the %end statement while the end of macro is achieved through %mend statement.

%macro test(finish);
   %let i = 1;
   %do %while (&i <&finish);
      %put the value of i is &i;
      %let i=%eval(&i+1);
   %end;
%mend test;
%test(5)

运行上述代码后,我们得到以下输出。

When the above code is run we get the following output.

macro do finish