DATA Step, Macro, Functions and more

where is the problem for this macro variable reference?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

where is the problem for this macro variable reference?

 




%macro test1; data a; a=1; put "a long text"; run; %mend; %test1; * this works; %let long=%nrbquote(%test1); %put &long; options mprint mlogic symbolgen; %macro test2; %if &long ne %str( ) %then %do; data b; b=1; %end; &long /* problem here, why? */ proc print width=min; run; %mend; %test2;

log.png 

 

I was about to check some macros, but happened to find this error while testing, where is the problem?

 

Thank you very much


Accepted Solutions
Solution
‎05-26-2017 10:00 AM
Super User
Posts: 5,498

Re: where is the problem for this macro variable reference?

The contents of &long have been quoted by %NRBQUOTE.  That includes the double quotes surrounding "a long text".  Evidently, SAS isn't figuring out to unquote those in time to parse the statement correctly.  Try replacing this:

 

&long

 

Instead, use:

 

%unquote(&long)

View solution in original post


All Replies
Super User
Posts: 19,770

Re: where is the problem for this macro variable reference?

What happens if you add a semicolon after &long?

Occasional Contributor
Posts: 13

Re: where is the problem for this macro variable reference?

Thank you, Reeza. I still got the same error if i add a semicolon...

 

but if i remove the the put "a long text" , it works, do not know whySmiley Sad

Occasional Contributor
Posts: 13

Re: where is the problem for this macro variable reference?

%macro test1;
%do;
data a;
a=1;
%put a long text; /*There must be a sign "%"*/
run;
%end;
%mend;
%test1;

%let long=%nrbquote(%test1);
%put &long;

options mprint mlogic symbolgen;
%macro test2;
%if &long ne %str( ) %then %do;
data b;
b=1;
run;
%end;
&long /* problem here, why? */
proc print width=min;
run;
%mend;
%test2;

Solution
‎05-26-2017 10:00 AM
Super User
Posts: 5,498

Re: where is the problem for this macro variable reference?

The contents of &long have been quoted by %NRBQUOTE.  That includes the double quotes surrounding "a long text".  Evidently, SAS isn't figuring out to unquote those in time to parse the statement correctly.  Try replacing this:

 

&long

 

Instead, use:

 

%unquote(&long)

Occasional Contributor
Posts: 13

Re: where is the problem for this macro variable reference?

Posted in reply to Astounding
Thank you very much for the explanation
Super User
Posts: 10,020

Re: where is the problem for this macro variable reference?

Try %UNQUOTE()

 

 

%macro test1;
    data a;
    a=1;
    put "a long text";
    run;
%mend;
%test1


%let long=%nrbquote(%test1);
%put &long;

options mprint mlogic symbolgen;
%macro test2;
%if &long ne  %then %do;
    data b;
    b=1;
    run;
%end;
 %unquote(&long) 
proc print width=min;
run;
%mend;

%test2;
Super User
Super User
Posts: 7,039

Re: where is the problem for this macro variable reference?

The macro quoting introduced by the %NRBQUOTE() function is causing the generated code to not be valid SAS syntax.

Use the %UNQUOTE() function to remove the macro quoting when you no longer need it.

Here is a simplier example.

%let mvar1=put "no macro quoting";
%let mvar2=%nrbquote(put "macro quoting");
data _null_;
 &mvar1;
run;
data _null_;
 &mvar2;
run;
data _null_;
 %unquote(&mvar2);
run;
Occasional Contributor
Posts: 13

Re: where is the problem for this macro variable reference?

Thank you allSmiley Wink

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 160 views
  • 4 likes
  • 6 in conversation