BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
duanzongran
Obsidian | Level 7

dear all:

i wanna refer a macro variable in a macro like this:

%let a=%nrstr(%do i= 1 %to 3;);
%let b=%nrstr(%end;);
%macro linshi();
&a.;
%put &i.;
&b.;
%mend;
%linshi();

 

but the log shows :

NOTE: Line generated by the macro variable "A".
1 %do i= 1 %to 3;
-
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

WARNING: Apparent symbolic reference I not resolved.
&i.
NOTE: Line generated by the macro variable "B".
1 %end;
-
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

 

What should i do to make it work?

Thanks in advance !

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @duanzongran,

 

I think the error messages are due to the (invisible) macro quoting characters which you introduced by (correctly) using the %NRSTR function. 

 

The deeper issue is that you want to create macro program text, then compile and finally execute this macro, but you mix the first two of these three steps. The compilation of the %MACRO, %PUT and %MEND statements must be deferred similarly to that of the %DO-%END statements. Here's how you can achieve this (and use the %UNQUOTE function to remove the macro quoting characters):

%let a=%nrstr(%do i= 1 %to 3;);
%let b=%nrstr(%end;);

%unquote(
  %nrstr(%macro linshi();)
  &a.;
  %nrstr(%put &i.;)
  &b.;
  %nrstr(%mend;)
)

%linshi();

View solution in original post

6 REPLIES 6
FreelanceReinh
Jade | Level 19

Hello @duanzongran,

 

I think the error messages are due to the (invisible) macro quoting characters which you introduced by (correctly) using the %NRSTR function. 

 

The deeper issue is that you want to create macro program text, then compile and finally execute this macro, but you mix the first two of these three steps. The compilation of the %MACRO, %PUT and %MEND statements must be deferred similarly to that of the %DO-%END statements. Here's how you can achieve this (and use the %UNQUOTE function to remove the macro quoting characters):

%let a=%nrstr(%do i= 1 %to 3;);
%let b=%nrstr(%end;);

%unquote(
  %nrstr(%macro linshi();)
  &a.;
  %nrstr(%put &i.;)
  &b.;
  %nrstr(%mend;)
)

%linshi();
Astounding
PROC Star

Another working approach would be to create the macro variables A and B without quoting functions:

 

data _null_;
   call symput('A', '%do i=1 %to 3;');
   call symput('B', '%end;');
run;

 

Then you can use your macro as is.

Tom
Super User Tom
Super User

Sounds like the old Doctor joke.

The patient says, "Doctor, it hurts when I do this." "Then don't do that!"

 

If you really need to dynamically generate code it is going to be much easier to dynamically generate SAS code instead of trying to dynamically generate macro code.

 

What is the actual problem you are trying to solve by creating that Rube Goldberg machine?

Kurt_Bremser
Super User

My thoughts, in sequence:

  1. "This is crazy!"
  2. "This is absolutely crazy!"
  3. "Somebody get help, quickly, for this poor person!"

Really, what is the purpose of this?

Astounding
PROC Star

@Kurt_Bremser ,

 

I have had such thoughts many times about many posts. My tongue has permanent scars from the number and intensity of times I have bitten it. But not in this case.

 

To me, this is how you learn about macro language.  Try things out, see what works and what doesn't.  For the things that don't work, try to figure out why.  Then re-test your conclusions.

 

Of course, I can't guarantee the intention here, but I look at this as a learning experience not application development.

Reeza
Super User
Agreed with others here, this is a very weird way to generate code. If you're actually using this code in production or to do an analysis I'd highly recommend you state your actual intent and someone can help you move towards a better solution.

If you're doing this for the pleasure of learning about macros, have fun.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 725 views
  • 9 likes
  • 6 in conversation