BookmarkSubscribeRSS Feed
pheynix
Calcite | Level 5

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?

5 REPLIES 5
Patrick
Opal | Level 21

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.

esjackso
Quartz | Level 8

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

suraestar
Calcite | Level 5

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

Amir
PROC Star

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.

Peter_C
Rhodochrosite | Level 12

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

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
  • 5 replies
  • 886 views
  • 0 likes
  • 6 in conversation