DATA Step, Macro, Functions and more

INDSNAME macro

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 144
Accepted Solution

INDSNAME macro

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


Accepted Solutions
Solution
‎03-04-2015 09:36 AM
PROC Star
Posts: 1,230

Re: INDSNAME macro

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.

View solution in original post


All Replies
Super User
Posts: 6,930

Re: INDSNAME macro

How do you read the dataset in the macro?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 144

Re: INDSNAME 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.

Super User
Posts: 6,930

Re: INDSNAME macro

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,777

Re: INDSNAME macro

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

Frequent Contributor
Posts: 144

Re: INDSNAME macro

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.

Respected Advisor
Posts: 3,777

Re: INDSNAME macro

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.

Solution
‎03-04-2015 09:36 AM
PROC Star
Posts: 1,230

Re: INDSNAME macro

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.

Frequent Contributor
Posts: 144

Re: INDSNAME macro

Thanks all,

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

Respected Advisor
Posts: 3,777

Re: INDSNAME macro

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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