简体   繁体   中英

How to explan this SAS macro?

I'm a new SAS learner. Here is a SAS statement.

%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. Please explan this SAS macro. Many thanks!

prxmatch is a PERL regular expression function in SAS. This statement is checking if the user ID name contains E0349646 . prxmatch returns the first position in which the match occurs. If it finds no match, it is 0.

%sysfunc() is a macro function that allows you to use SAS functions within macros. Since prxmatch is a SAS function being used within a macro, it must be enclosed with %sysfunc() .

Finally, ne is another way of saying 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. 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.

So what you have is a macro %if statement. 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; 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.

%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. That is part of the group of functions that implement Perl Regular Expressions. You are also referencing a macro variable named SYSUSERID . That particular macro variable is one that SAS creates automatically to contain the userid of the user that is running the SAS program. The regular expression being used in the prxmatch() function call is testing if the value is equal to E0349646 , ignoring case. 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.).

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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