DATA Step, Macro, Functions and more

Dynamic Macro Variable Creation

Reply
Occasional Contributor
Posts: 15

Dynamic Macro Variable Creation

Hi,

 

I want to create some macro variable with the help of another macro variable. Please suggest how i can resolve below code?

I am in some sort of programming where i can just edit this macro only. So kindly dont suggest to create another macro. I can only edit this one.

%macro rowToMacroVariable(dataSetName,variableName,macroVariableName);
data _null_;
set &dataSetName;
call symputx("&macroVariableName"||strip(put(_n_,best.)),&variableName,"G");
run;
%mend;
data ab;
input brand$ impress$;
cards;
b TJ
c DJ
d PJ
;
run;
%rowToMacroVariable(ab,brand,brand);
%rowToMacroVariable(ab,impress,var_&brand);
%put _GLOBAL_;



Now with the help of above code i want to create

 

brand1=b brand2=c brand3=d which is working perfectly fine. But in next row when i am trying to create var_b=TJ var_c=DJ var_d=PJ it throws an error. I am not sure why this is hapenning?

Super User
Super User
Posts: 7,392

Re: Dynamic Macro Variable Creation

This works:

 

%macro rowToMacroVariable(dataSetName,variableName,macroVariableName);
data _null_;
set &dataSetName;
call symputx("&macroVariableName"||strip(put(_n_,best.)),%trim(&variableName),"G");
run;
%mend;
data ab;
input brand$ impress$;
cards;
b TJ
c DJ
d PJ
;
run;
%rowToMacroVariable(ab,brand,brand);

%put _GLOBAL_;

And creates the three macro variables.  This line however:

 

%rowToMacroVariable(ab,impress,var_&brand);

There is no macro variable in the code you have provided for var_&brand.  It does not exist hence the error.  Perhaps you meant;

%rowToMacroVariable(ab,impress,var_&brand1.);

 

I would again take this opportunity to state that this code is not the way to proceed.  You are far overcomplicating your problems and leading to much more serious problems down the line. 

Occasional Contributor
Posts: 15

Re: Dynamic Macro Variable Creation

Thanks RW9.

Actully issue is with

Spoiler
%rowToMacroVariable(ab,impress,var_&brand.);

Because Here i cannot use &brand1 or 2 because its not in loop. I dont want to run loop because my func will again open whole dataset and create various unwanted variables. So I will explain once more my problem statment is :

 

I have 100's of rows in data set. But some how programing is already done with the help of this 100's of macro variable. So currenty we are using like

%let brand1=b

%let brand2=c and so on.

So my motive was to remove this code and for this i have created a dataset. Now Call Symputx works very well fine if i am using static variable name that is "brand". But now i want to combine this "brand1" with some other text to create another variable.

i want to pass var_&brand and i want it will create var_b. I donnot want to make it in loop. So is there any way ? or can you suggest what is the best way for this problem statment ?  

Super User
Super User
Posts: 7,392

Re: Dynamic Macro Variable Creation

So your going to create hundreds of macro variables and then hundreds more with a slightly different name, to run several macros.

I am sorry, you are lost to madness.  Base SAS is a programming language deisnged to process datasets there are lots of functions, data modelling techniques, and processes deisgned for making data processing simple and easily maintainable.  You are ignoring this in total and remaking the whole thing in macro language, hence I can't help any further.

Occasional Contributor
Posts: 15

Re: Dynamic Macro Variable Creation

The reason I am dong this because I am not authorized to touch those already programmed algorithms. I am trying to optimize it in best possible way. But it seems like you are very much resistive with the procedure and technique you follow. So no worry but still thanks for your suggestions Smiley Happy
Contributor
Posts: 35

Re: Dynamic Macro Variable Creation

Actually when you are calling the macro 

 

%rowToMacroVariable(ab,impress,var_&brand);

 

Yor are givng &brand when calling the macro .there is no macro variable present with the name Brand

 

there are macro variable present but they have different names brand1,brand2 and brand =3 which are created when you have run below line 

 

%rowToMacroVariable(ab,brand,brand);

 

that why you will be getting error in the log because no macro varibale is present with name- brand

 

Occasional Contributor
Posts: 15

Re: Dynamic Macro Variable Creation

Hi Rohit,

 

Agreed this is because &brand is not resolved, but i want to know how i can relove it in Call symput because it is creating below error. Can you have some look in snapshot attached.


Capture.PNG
Regular Contributor
Posts: 194

Re: Dynamic Macro Variable Creation

Hello,

 

If your goal is only to generate var_<brand> macrovariables, you can do it directly.

 


data _NULL_;
    set ab;
    call symput(cats("var_",brand),impress);
run;
Ask a Question
Discussion stats
  • 7 replies
  • 153 views
  • 3 likes
  • 4 in conversation