DATA Step, Macro, Functions and more

the quoting %NRSTR() not worked as expected

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

the quoting %NRSTR() not worked as expected

Hi,

 

i am trying the basic macro facility. While i tried to submit below the code:

 

%let param=%nrstr(%RO CD8+NK+ (%));
%put &param;

 

the SAS 9.4 get stuck.

however,below codes worked

%let param=%nrstr(%%RO CD8+NK+ (%%));
%put &param;

 

 

But according to the SAS doc here http://support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/default/viewer.htm#p0pnc7p9n4h6g5n16..., i didn't find where is the problem. 

 

can anybody help to explain this?

 

thank you very much


Accepted Solutions
Solution
‎11-10-2017 01:17 AM
Valued Guide
Posts: 533

Re: the quoting %NRSTR() not worked as expected

I can see why you say "the  parenthesis are matched this time". But in fact, by prepending a percent to that last closing paren you still don't have one. That closing paren around the macro parameters is just not safe from being masked. The message you get is clear about that:

ERROR: Expected close parenthesis after macro function invocation not found.

View solution in original post


All Replies
Valued Guide
Posts: 533

Re: the quoting %NRSTR() not worked as expected

It's  the percent sign between parenthesis that are hurting you. That one needs to be doubled up. The doc is clear about this. Percent signs in combination with quotation marks and before a parenthesis need special attention. The first percent sign in your string is okay.

 

Hope this helps,

- Jan

Occasional Contributor
Posts: 15

Re: the quoting %NRSTR() not worked as expected

Posted in reply to jklaverstijn

thanks,Jan. But i still didn't find this "Percent signs in combination with quotation marks and before a parenthesis need special attention"  instruction....

 

Could you please kindly paste the specific sentence from the doc?

 

 

Occasional Contributor
Posts: 15

Re: the quoting %NRSTR() not worked as expected

Note: If an unmatched single or double quotation mark or an open or close parenthesis is used with %STR or %NRSTR, these characters must be preceded by a percent sign (%).

 

for above the highlighted part, dose that mean , no matter the open or close parenthesis matched or not, an extra % is needed?

 

please kindly correct me if i misunderstand anything.

 

thank you very much

Valued Guide
Posts: 533

Re: the quoting %NRSTR() not worked as expected

The percent sign is used inside %STR() and %NRSTR() to mask unbalanced quotation marks or parenthesis. Hence your "%)" would be interpreted as "mask the unmatched parenthesis. This leads to a either a message about unbalanced parenthesis (as the closing paren is no longer is matched to its opening sibling) or a seemingly unresponsive EG run. Therefore the need to mask the percent sign itself by adding a second percent sign, eventhough %NRSTR() is described as "masking % signs".

 

This is all documented in the online doc. There is a paragraph titled "Using Unmatched Quotation Marks and Parentheses with %STR and %NRSTR".

 

Regards Jan.

Occasional Contributor
Posts: 15

Re: the quoting %NRSTR() not worked as expected

Posted in reply to jklaverstijn

got it, thank you. i misunderstood the unmatched concept.

 

one more question, what's wrong with this case?

%let test=%nrstr(RO CD8+NK+ ()%); *the  parenthesis are matched this time, but it didn't work;

%put &test;

 

%let test1=%nrstr(RO CD8+NK+ ()); *the  parenthesis are matched this time, but it works without the last %;

%put &test1;

Solution
‎11-10-2017 01:17 AM
Valued Guide
Posts: 533

Re: the quoting %NRSTR() not worked as expected

I can see why you say "the  parenthesis are matched this time". But in fact, by prepending a percent to that last closing paren you still don't have one. That closing paren around the macro parameters is just not safe from being masked. The message you get is clear about that:

ERROR: Expected close parenthesis after macro function invocation not found.
Occasional Contributor
Posts: 15

Re: the quoting %NRSTR() not worked as expected

Posted in reply to jklaverstijn

i see, thank you for the patience

 

i didn't get that error if submit this:

%let test=%nrstr(RO CD8+NK+ ()%); *the parenthesis are matched this time, but it didn't work;
%put &test;

 

but nothing happen in the log and the codes submitted thereafter are not executed as well. ...weird

Super User
Posts: 13,523

Re: the quoting %NRSTR() not worked as expected


ifendo wrote:

i see, thank you for the patience

 

i didn't get that error if submit this:

%let test=%nrstr(RO CD8+NK+ ()%); *the parenthesis are matched this time, but it didn't work;
%put &test;

 

but nothing happen in the log and the codes submitted thereafter are not executed as well. ...weird


I don't see anywhere that you have shown us what you actually expect the value of &test or &param to be.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 180 views
  • 0 likes
  • 3 in conversation