Special character references for SAS macros

Source: Internet
Author: User

From:http://blog.chinaunix.net/uid-675476-id-2076827.html

In SAS macros, strings are enclosed in double quotation marks, but if a string contains special characters such as double quotes or a percent semicolon, a single quotation mark is used to escape the special characters in the string. Like what:
%let test = ' A ' is the first character! ';

However, if the string contains single quotes, the above method is invalidated. So more generally, SAS provides%STR and%nrstr to escape special characters. Where%str can escape commas, single and double quotes, left and right parenthesis, tilde, semicolon, space, sharp angle, etc., while%nrstr can escape the special symbols%str can escape, but also can be escaped with the (&) symbol and the percent sign. Specific use methods such as:
%let test=%str ("A" is the first character!);

It is important to note that when using%STR, if the referenced string contains mismatched single double quotes, the parentheses must be escaped with a percent sign, such as:
%let test=%str (i% ' m consultant.);

Of course, the author in order to fear errors,%str in general encountered special characters are escaped with a percent, such as:
%macro doit;
X "%str (copy%" a% "%" b% ")";
%mend doit

The most special symbol is the percent sign itself, if you want to include a percent in the%str, but also do not let the SAS and its subsequent% of the characters as a macro name or macro call, use percent to escape the percent semicolon, that is, with two percent sign to represent a percent semicolon, such as:
%let test =%str (% "%%%" is a special character!);

Test it:
Data _null_;
Put "&test";
Run

The log window is not printed out "%" is a special character! words.

%str and%nrstr are valid for constants, and%STR and%nrstr do not escape the contents of variables if they are variables. In this case, we need%bquote and%nrbquote, these two statement are specifically after the macro expansion, the macro execution of the variable corresponding to the contents of the escape, such as:

%let test =%STR (% "percent");
%macro DD (h=);
Data _null_;
%let a =%nrbquote (%substr (&h.,1,1));
Put "&a.";
Run
%mend DD;
%DD (h=&test.);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.