Hi,
I'm tring to create a data step function style macro, but in a step I need to know the name of the dataset that I'm using.
Exist something like the INDSNAME but in a macro code? like a %sysfunc(indsname)
Thanks
I have wanted what you describe sometimes, but I don't think it's possible.
If every DATA step just had one input dataset and one output dataset, then it might theoretically be possible. When the macro executed, the DATA step would be in the process of compiling. Assuming pointers to the input and output datasets had already been created, the names of those datasets should be available.
But since a single DATA step could have multiple input data sets, multiple output data sets, multiple SET statements, etc etc, it's hard for me to see how SAS could define "dataset that I'm using."
I think &SYSLAST, &SYSDSN, and _last_ are similar concepts ("last dataset I created"), and I stay away from them, instead forcing users to pass data set names.
How do you read the dataset in the macro?
The idea is use the macro such as
data dataset;
set example;
%macro_call;
run;
Doing this I don't need to send as parameters the dataset name to use. I think in the &SYSMACRONAME and I 'm wondering if there exist something similar to know the dataset, without think everytime in introduce the indsname option.
Since the macro will be dealt with by the macro processor BEFORE the data step is compiled and starts to execute, there is no way to know the name of the dataset. But you can use the indsname function in the code _generated_ by the macro. So if you need to know the name of the dataset AFTER the data step has executed, you can use call symput to store the result of the indsname function in a macro variable. But this macro variable will not be available to the macro called "inside" the data step.
If your function style macro needs to know the data set name it probably shouldn't be function style.
It's a function style macro, but to avoid unnecessary calculations and errors I want to detect if the needed variables are in the dataset, and to do that my unique idea was use varnum, that need the name of the dataset.
Well I still say function style ain't the way to go.
But if you insist you will need to do that OPEN VARNUM checking before the data step starts right? INDSNAME won't do you any good anyway, it doesn't get populated until the first record is read.
You "could" use CALL VNEXT to find the names of all the variables AFTER the data step starts and stop the data step if you don't find the variables you're looking for.
I have wanted what you describe sometimes, but I don't think it's possible.
If every DATA step just had one input dataset and one output dataset, then it might theoretically be possible. When the macro executed, the DATA step would be in the process of compiling. Assuming pointers to the input and output datasets had already been created, the names of those datasets should be available.
But since a single DATA step could have multiple input data sets, multiple output data sets, multiple SET statements, etc etc, it's hard for me to see how SAS could define "dataset that I'm using."
I think &SYSLAST, &SYSDSN, and _last_ are similar concepts ("last dataset I created"), and I stay away from them, instead forcing users to pass data set names.
Thanks all,
I guess that I will pass the dataset as parameter and I will forget about that.
Another way might be to turn the problem inside out. Instead of a function style macro that is part of a data step that you have no control over make your macro create the data step but give the user a hook into it to add code before or after you do your thing.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.