BookmarkSubscribeRSS Feed
Richardvan_tHoff
Obsidian | Level 7

%LET location=g:\sasdiv01\;

%LET env=prod;

%LET folder_code=&location.&env.\code;

 

The above works perfect, but now I only want to change the env at execution in my code.

 

What I want as a result is the following.

%LET env=test;

%LET folder_test=&folder_code;

%PUT &=folder_test;

 

Current Result

folder_test=g:\sasdiv\prod\code

 

Desired Result

folder_test=g:\sasdiv\test\code

 

I know it is not possible with the code i wrote above. Is there a way to do this a other way.  The reason I want to this is because all the references to folders and other stuff is in the 1 piece of code. We don't want any references to folders in the code directly (if we change a folder we only have to change it in that first piece of code). 

 

 

6 REPLIES 6
Shmuel
Garnet | Level 18

Yes you can, just by using a macro program, like next code:

%macro exec(env,folder_test=g:\sasdiv\test\code);
    %if &env=prod %then %do;
          %LET location=g:\sasdiv01\;
          %LET folder_code=&location.&env.\code;
    %end; %else
    %if &env=test %then %do;
          %LET folder_code=&folder_test;
          %PUT &=folder_test;
    %end;
     ..... enter your code here or %include it from a source folder ...
%mend exec;
%exec(<env>);  /* suppay the environment to run in */

 

Kurt_Bremser
Super User

Don't forget to use %global for variables you want to use later outside of the macro.

Or create a function-style macro that resolves to code:

%let location=g:\sasdiv01\;

%macro set_path(env);
  &location.&env.\code
%mend;

%put %set_path(prod);
%put %set_path(test);
Kurt_Bremser
Super User

A macro variable does not know from which parts it was built, so it cannot update itself dynamically; you need to create the new variable like you created the old one:

%let location=g:\sasdiv01\;
%let env=prod;
%let folder_code=&location.&env.\code;

/* then, later: */

%let env=test;
%let folder_test=&location.&env.\code;

I played around a little trying to use indirect addressing of macro variables to achieve this, and while I suspect it may be possible (people with more macro-fu than me might find a way), I also suspect that it would create hard-to-maintain code.

 

Richardvan_tHoff
Obsidian | Level 7

Thank you all for your suggestions. For the problem I have it  will to work but not the way I want it.

 

I think it is not possible to change the value dynamically at execution time. I was looking for such a solution.

 

I am working on a stored proces to transfer data en code from development to production. In that STP I have prompts to choose source and target (which are based on a column from a sas table in the metadata). Knowing that i solved the problem a other way.

 

%LET g_sytem=acc;

%LET g_system_folder_code=g:\sasdiv\program\&g_system.\code;

 

From the stp I get the variables p_source en p_target;

p_source=ont;

p_target=prod;

 

%LET v_source_code_source =%SYSFUNC(TRANWRD(&g_system_folder_code.,\&g_system.\,\&p_source.\));

%LET v_source_code_target  =%SYSFUNC(TRANWRD(&g_system_folder_code.,\&g_system.\,\&p_target.\));

 

This way i still use the standard available macro variable's. 

 

 

 

Shmuel
Garnet | Level 18

You can change any macro variable value according to a known condition, either in program or after a given trigger.

 

If the code is part of a macro program you cat insert %IF <condition> and use %LET to assign a new value.

If the code is part of a data step you can use a statement like:

    if <condition> then call symput(<macro_name>, <macro_value>);

 

Having the code in a stored process, it needs of course to be changed/updated/adapted to the new requirements and restored.

s_lassen
Meteorite | Level 14

One very simple way is to change FOLDER_CODE to a macro:

 

%macro folder_code;
  &location.&env\code
%mend;

%LET location=g:\sasdiv01\;
%LET env=prod;
%put %folder_code;

%let env=test;
%put %folder_code;

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1669 views
  • 1 like
  • 4 in conversation