I have a macro below that is used to assign values to macro variables. I then want to call that macro inside another written macro that takes a value in the sas dataset as the parameter for the other macro.
%macro pde1b (Table = );
proc sql noprint;
select sd_ded
into :sd_ded
from work.&Table;
quit;
proc sql noprint;
select sd_icl
into :sd_icl
from work.&Table;
quit;
%mend pde1b;
Example code:
%macro pde1;
data x;
%pde1b (Table = x.id);
/**DO OTHER THINGS USING MACRO VARIABLES**/
run;
%mend pde1;
I am also getting a 1 unclosed DO block error message for the %pde1b but I do not have any do blocks in that code and all sections have quit; The code works before I added this as well.
How can I fix this? Or is there another way I can accomplish this?
Doing a well-structured step once for big data performs better than repeating the same action over and over for subsets and joining the results back.
So we need to see what you are doing in there, and get a feel for the "big picture": what do you start with (example data), and what information you need to draw out of it.
@vicmaria98 wrote:
I am trying to get the macro variable values for that specific plan. Those values are then used in calculations which are done in the data step. I am working with large sets of data so I don’t want to join the tables.
If you want to use the values from a dataset in calculations then you should avoid transforming them into text to put into macro variables. Your posted code is very similar to just doing something like this, which should not require any macro coding at all.
data x;
set x.id (keep= sd_ded sd_icl);
/**DO OTHER THINGS USING DATASET VARIABLES**/
run;
Your macro calls PROC SQL. You call the macro from within a SAS data step. You can't run PROC SQL inside a SAS data step. Just because you have put something in a macro, the resolved code has to still follow all rules of SAS syntax.
Show us the syntax you want to use WITHOUT macros.
It looks like part of your question is how to use data to drive a macro call, passing values from a dataset as parameters in a macro call. For that, you should read up on CALL EXECUTE. Also, if you want the macro variables created in one macro to be available in another macro variable, you need to read up on how the macro language handles scope. This is a complex use of the macro language, and will take some effort to learn.
That said, I agree with @JosvanderVelden, it would be helpful to see a small example with the input data (just 2-3 records should be fine), and the code you have tried (including some of the "do other things with macro variables" part), along with the log and a description of the desired output.
Yes, it's possible to have macros that call macros, and it's possible to pass values to macro parameters from data sets. At the same time, if you've got (say) a dataset of standard deviations that you want to use in some calculation, often it's easier to use the DATA step to combine the data, without the macro language. (And I say that as a *big* fan of the macro language).
As always, to become a master of a tool, you have to know when not to use it.
Just resolve your macro calls yourself, and see the resulting code:
data x;
proc sql noprint;
select sd_ded
into :sd_ded
from work.x.id;
quit;
proc sql noprint;
select sd_icl
into :sd_icl
from work.x.id;
quit;
/**DO OTHER THINGS USING MACRO VARIABLES**/
run;
The first PROC SQL statement ends the DATA step, so it creates an empty dataset WORK.X.
And the /**DO OTHER THINGS USING MACRO VARIABLES**/ is what then causes the ERRORs because the code is outside of a DATA step.
The macro preprocessor is a code generator, and it must create valid code. Valid code that can then run AFTER the macro processor has done its work.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.