I am trying to get a macro to work that will send in a variable in order to read a database, and then return the variable it reads off the database.
The macro works correctly, as I can see from the proc print showing the db read worked. But how to I pluck the variable value it reads from working memory and return it to the calling program? These are 2 completely separate pieces of code - the macro is not within the main module of code, it is a separate file.
Calling module:
format passvar1 $3.;
passvar1=substr(some_other_variable,1,2);
call execute('%mymacro('||passvar1||')');
Var_i_want_populated = the value that was read in the mymacro macro;
Macro:
call symput('random_var_name',put(var_that_was_read_from_the_database));
var_that_was_read_from_the_database DOES have a value in it and I want to return it to the calling module so that I can assign its value to Var_i_want_populated.
Does anyone know how to do this?
You will need to show us the macro definition and eventually an execution log in order to give you advice.
Just as a thought: You could have a timing problem here:
Call execute "Resolves the argument, and issues the resolved value for execution at the next step boundary."
SAS(R) 9.2 Macro Language: Reference
It doesn't matter that your code is stored in separate .sas files. It still will be executed together within the same environment.
That was exactly what I was thinking as well. Have you tried assigning the value in a new data step. I am also assuming that your variable assignment code is actually something similar to:
var_I_want_populated = "&random_var_name" ;
EJ
Hi there..
If you could know the macro variable name which holds the value in the macro(a separate file). You can do in these 3 ways
1. Define the Macro variable as GLOBAL which holds the results and if done this value can be used in the calling program.
2. If you are aware of the name of macro variable then you can get the value by fetching the value from sashelp.vtable
3. This one is weird. Create a additional parameter in the Macro which holds the results and use it in the calling.
Ex:
%local input result;
%mymacro(inp=&input,
res=result);
%put Result = &result;
Let me know if this answers your question.....
Hi,
This might be a simple case of using the %global macro statement, but I'm not sure I fully understand the situation, e.g:
Does mymacro invoke some SQL?
Does mymacro create a table / dataset of results?
Can mymacro be changed to use "call symput('random_var_name',put(var_that_was_read_from_the_database)); " as part of a data step?
May be the above can be all answered if you share the code for mymacro.
Regards,
Amir.
Have a look at the DOSUBL function.
There is no way call execute can invoke code that returns a value to the data step with that call execute syntax.
Another way might be to create your own call routine or a user defined function with PROC FCMP. Both these and DOSUBL complete their process before the following line in the data step continues execution.
good luck
peterC
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.