BookmarkSubscribeRSS Feed
tjain90
Fluorite | Level 6

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?

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

tjain90
Fluorite | Level 6

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 ?  

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

tjain90
Fluorite | Level 6
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 🙂
Rohit12
Obsidian | Level 7

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

 

tjain90
Fluorite | Level 6

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
gamotte
Rhodochrosite | Level 12

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 1699 views
  • 3 likes
  • 4 in conversation