DATA Step, Macro, Functions and more

Macrovariable in macrovariable

Accepted Solution Solved
Reply
Contributor
Posts: 41
Accepted Solution

Macrovariable in macrovariable

Hi there,

 

I get an issue on macrovariable.inside another one.

I explain:

 

I call a macro function A like this:

%A (mymacrovariable = MMV);
%put &MMV.;

 

My macro function A is something like this:

%macro A (mymacrovariable);

%global &mymacrovariable.;
&&mymacrovariable. = 0;

%mend;

When I run it I have an issue on MMV...

MLOGIC(A):  Beginning execution.
MLOGIC(A):  Parameter MYMACROVARIABLE has value MMV
MLOGIC(A):  %GLOBAL  &MYMACROVARIABLE.
MPRINT(A):   MMV = 0;

MLOGIC(A):  Ending execution.
NOTE: Line generated by the macro variable "MYMACROVARIABLE".
31           MMV
             ___
             180
ERROR 180-322: Statement is not valid or it is used out of proper order.

32         %put &MMV.;
 
33         
34         GOPTIONS NOACCESSIBLE;
35         %LET _CLIENTTASKLABEL=;
36         %LET _CLIENTPROCESSFLOWNAME=;
37         %LET _CLIENTPROJECTPATH=;
38         %LET _CLIENTPROJECTNAME=;
39         %LET _SASPROGRAMFILE=;
40         
41         ;*';*";*/;quit;run;
42         ODS _ALL_ CLOSE;
43         
44         
2                                                          The SAS System                           11:18 Thursday, October 19, 2017

45         QUIT; RUN;
46         

Can you help me to solve it?

 

Thanks,

FP12


Accepted Solutions
Solution
‎10-19-2017 06:22 AM
Super Contributor
Posts: 359

Re: Macrovariable in macrovariable

Hello,

 

%macro A (mymacrovariable);

	%global &mymacrovariable.;
	%let &mymacrovariable.=0;

%mend;

View solution in original post


All Replies
Super User
Super User
Posts: 9,840

Re: Macrovariable in macrovariable

This:

&&mymacrovariable. = 0;

Generates a Base SAS statement:

mmv= 0;

 Which is not in a datastep, thus error.

 

But seriously, why? You are hitting every bad programming practice there, creating global variables in macros, passing references to macro variables etc.  There is never a need to goto this level.  Base SAS is the programming language, Macro is an additional component - never needed - to help create generic code.  All it does is create some text, nothing more, it is not a replacement for Base SAS.  As simple example, if you find you passing data across multiple items, just put them in a dataset and use simple datastep to process things.  You will find your code becomes simpler, and more robust.

Contributor
Posts: 41

Re: Macrovariable in macrovariable

Thanks for the answer.

But seriously, why?

 

I didn't want to write the complete code here. It is just the part of the code interesting here. There are also other parameters inside the macro function. The variable won't be always 0.

 

Moreover I create some other variables using the same macro function (and using different values for parameters of the macro function).

Instead of writing several time the same datastep for several macrovariables, it is much readable and better practises to write the code only once, I am sure Smiley Happy

Super User
Super User
Posts: 9,840

Re: Macrovariable in macrovariable

Without seeing the code I can't say specifically, however I would almost never see a need for multiple macro variables, references to them being passed into macro functions, and global macro variables being created within separate macro functions.  You will get problems later on.  A good tip is to write the code once as Base SAS, then if there are some repetitons, extract that part of the code and create macros from it.  Essentially, what you are doing with macro is writing Base SAS code, but just doing it in a very complicated way.  

Solution
‎10-19-2017 06:22 AM
Super Contributor
Posts: 359

Re: Macrovariable in macrovariable

Hello,

 

%macro A (mymacrovariable);

	%global &mymacrovariable.;
	%let &mymacrovariable.=0;

%mend;
Contributor
Posts: 41

Re: Macrovariable in macrovariable

Thanks, it worked. Smiley Happy

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 151 views
  • 2 likes
  • 3 in conversation