Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write macro that creates global variable from another global variable and macro parameter

I have global variable &project_path and need to write macro %set_path that will create another global variable which is equal &project_path\parameter. In my case name of new variable have to be parameter_path. I am new in SAS macro and as I understand they can’t return values ​​like normal functions, so I can’t do something like log_path=%set_path(log)? So I have to pass a new variable as a parameter like %set_path(log_path, log) but then I can’t make log_path Global? How to solve this problem?

The thing I came up with is to set the name of the new variable depending on the parameter. But, it only works if you add a prefix. I need a postfix. And anyway, in general, it looks like not the best option

%macro set_path(sub_dir);
    %global &sub_dirSet;
    %let &sub_dirSet = &project_path\&sub_dir;
%mend set_path;

%set_path(data);
%set_path(log);

%put &dataSet;
%put &logSet;

Of course I tried something like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

%macro set_path(var_name, sub_dir);
    %global var_name;
    %let var_name = &project_path\&sub_dir;
%macro set_path;

but as I understand it is fundamentally wrong

>Solution :

As a general coding pattern, it’s usually a good idea to avoid creating lots of global macro variables. But to your question of how to add a postfix, you can use a . to end a macro variable reference and add a postfix. So I think your approach would work with:

%let project_path=\\my\dir ;

%macro set_path(sub_dir);
    %global &sub_dir.Set;
    %let &sub_dir.Set = &project_path\&sub_dir;
%mend set_path;

%set_path(data)
%set_path(log)

%put &dataSet;
%put &logSet;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading