DATA Step, Macro, Functions and more

SAS macro help to pass variable back to the calling program

Reply
Occasional Contributor
Posts: 7

SAS macro help to pass variable back to the calling program

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?

Respected Advisor
Posts: 3,889

Re: SAS macro help to pass variable back to the calling program

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.

Super Contributor
Posts: 333

Re: SAS macro help to pass variable back to the calling program

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

Contributor
Posts: 46

Re: SAS macro help to pass variable back to the calling program

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.....

Super Contributor
Posts: 282

Re: SAS macro help to pass variable back to the calling program

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.

Valued Guide
Posts: 2,175

Re: SAS macro help to pass variable back to the calling program

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

Ask a Question
Discussion stats
  • 5 replies
  • 256 views
  • 0 likes
  • 6 in conversation