BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
arodriguez
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.

View solution in original post

9 REPLIES 9
arodriguez
Lapis Lazuli | Level 10

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.

Kurt_Bremser
Super User

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.

data_null__
Jade | Level 19

If your function style macro needs to know the data set name it probably shouldn't be function style.

arodriguez
Lapis Lazuli | Level 10

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.

data_null__
Jade | Level 19

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.

Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
arodriguez
Lapis Lazuli | Level 10

Thanks all,

I guess that I will pass the dataset as parameter and I will forget about that.

data_null__
Jade | Level 19

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2541 views
  • 5 likes
  • 4 in conversation