简体   繁体   English

如何解释这个SAS宏?

[英]How to explan this SAS macro?

I'm a new SAS learner. 我是一名新的SAS学习者。 Here is a SAS statement. 这是SAS声明。

%if %sysfunc(prxmatch(/^(E0349646)$/i, &SYSUSERID.)) ne 0 %then %do;

I only know the "E0348535" is a user ID, but can't understand this whole statement. 我只知道“E0348535”是一个用户ID,但无法理解这整个声明。 Please explan this SAS macro. 请解释这个SAS宏。 Many thanks! 非常感谢!

prxmatch is a PERL regular expression function in SAS. prxmatch是SAS中的PERL正则表达式函数。 This statement is checking if the user ID name contains E0349646 . 此语句检查用户标识名称是否包含E0349646 prxmatch returns the first position in which the match occurs. prxmatch返回匹配发生的第一个位置。 If it finds no match, it is 0. 如果找不到匹配项,则为0。

%sysfunc() is a macro function that allows you to use SAS functions within macros. %sysfunc()是一个宏函数,允许您在宏中使用SAS函数。 Since prxmatch is a SAS function being used within a macro, it must be enclosed with %sysfunc() . 由于prxmatch是在宏中使用的SAS函数,因此必须用%sysfunc()括起来。

Finally, ne is another way of saying not equals . 最后, ne是另一种说不not equals Putting it all together, here's what this statement is doing in plain English: 总而言之,这就是这句话用简单的英语做的:

If the user ID contains 'E0349646', then do some stuff.

First a little on terminology. 首先是术语。 The SAS macro processor is a tool that can manipulate text and pass the result onto the actual SAS system to interpret. SAS宏处理器是一种可以处理文本并将结果传递到实际SAS系统以进行解释的工具。 That one line of code is using macro processor statement, but it is not a full macro definition. 那一行代码是使用宏处理器语句,但它不是一个完整的宏定义。 In fact starting with SAS 9.4 (I think perhaps maintenance release 9.4m4?) you could use that %if/%then/%do type of statement in a normal SAS program, without ever having to define an actual macro at all. 实际上从SAS 9.4开始(我想也许是维护版本9.4m4?)你可以在正常的SAS程序中使用%if/%then/%do语句类型,而%if/%then/%do不必定义实际的宏。

So what you have is a macro %if statement. 所以你拥有的是一个宏%if语句。 General form is: 一般形式是:

%if condition %then statement ;

Since the statement after the %then part is a %do macro statement then your program will need a %end; 由于%then部分之后的语句是%do宏语句,那么你的程序将需要一个%end; statement to mark the end of the block to execute when the condition is true. 用于标记条件为真时执行的块结束的语句。

Let's look at the condition being tested in your %if statement. 让我们看看你在%if语句中测试的条件。

%sysfunc(prxmatch(/^(E0349646)$/i, &SYSUSERID.)) ne 0 

So that is using macro function %sysfunc() to let you call the SAS function prxmatch() in macro code. 因此,使用宏函数%sysfunc()可以在宏代码中调用SAS函数prxmatch() That is part of the group of functions that implement Perl Regular Expressions. 这是实现Perl正则表达式的一组函数的一部分。 You are also referencing a macro variable named SYSUSERID . 您还引用了一个名为SYSUSERID的宏变量。 That particular macro variable is one that SAS creates automatically to contain the userid of the user that is running the SAS program. 该特定宏变量是SAS自动创建的变量,用于包含运行SAS程序的用户的用户标识。 The regular expression being used in the prxmatch() function call is testing if the value is equal to E0349646 , ignoring case. prxmatch()函数调用中使用的正则表达式是测试值是否等于E0349646 ,忽略大小写。 The result will be position of the first found location in the string being searched. 结果将是被搜索字符串中第一个找到的位置的位置。 If it is not found then it will return 0. (SAS, like humans, uses indexes starting from one and not zero.). 如果没有找到,那么它将返回0.(SAS,就像人类一样,使用从1开始而不是从零开始的索引。)。

Note that including the ^ and $ in the regular expression means that it needs to match the full string. 请注意,在正则表达式中包含^$意味着它需要匹配完整的字符串。 So you are testing if E0349646 is running the program. 因此,您正在测试E0349646是否正在运行该程序。

It would be much easier to test that directly without the need for a regular expression or the need to use non-macro function calls. 在不需要正则表达式或需要使用非宏函数调用的情况下直接测试它会容易得多。

%if %upcase(&sysuserid)=E0349646 %then %do;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM