DATA Step, Macro, Functions and more

How do I get the name of the file a Libref is referring to?

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How do I get the name of the file a Libref is referring to?

I am wondering if there is a way to pull the name of the file defined in the LIBNAME statement into a variable. 

That is, is there an analog to the FILENAME= option of the INFILE statement for the SET statement?

 

I would like to be able to access the name of the file to place it in report titles.

 

Thanks.


Accepted Solutions
Solution
‎07-19-2017 11:40 AM
PROC Star
Posts: 254

Re: How do I get the name of the file a Libref is referring to?

you can extract pathnames from dictionary.members or by using pathname function. Need to use capital letters while using dictionary.members. replace yourlib with libname

proc sql ;
select distinct path as path_var from dictionary.members
where libname= 'YOURLIB';
data abc;
path_var=pathname('yourlib');
run;

View solution in original post


All Replies
Super User
Posts: 5,091

Re: How do I get the name of the file a Libref is referring to?

The SET statement supports the INDSNAME= option.  That should be a possibility. 

 

http://support.sas.com/documentation/cdl/en/lestmtsref/69738/HTML/default/viewer.htm#p00hxg3x8lwivcn...

 

 

New Contributor
Posts: 3

Re: How do I get the name of the file a Libref is referring to?

This almost does it.
If you have the statement LIBNAME FILEREF "SASDATASET; in your program

Then when you use this: SET FILEREF.SETNAME INDSNAME= DSNAME , the value of DSNAME is FILEREF.SETNAME when what I was wanting it to be equal to SASDATASET.
PROC Star
Posts: 254

Re: How do I get the name of the file a Libref is referring to?

looks like you have by mistakenly, selected my answer as accepted answer instead of @Astounding answer

Super User
Posts: 5,091

Re: How do I get the name of the file a Libref is referring to?

You could easily have been right ... I wasn't sure what the end goal was either.

 

At any rate, use the SCAN function to pick out the second part of the name returned by INDSNAME=:

 

member_name = scan(full_name, 2, '.');

New Contributor
Posts: 3

Re: How do I get the name of the file a Libref is referring to?

I found that this worked when using a file on the SAS server. 

 

What I failed to mention was that I was using the code while connected to the mainframe.  When connected to the mainframe, it is necessary to use the PROC SQL answer of kirnav_.

 

Solution
‎07-19-2017 11:40 AM
PROC Star
Posts: 254

Re: How do I get the name of the file a Libref is referring to?

you can extract pathnames from dictionary.members or by using pathname function. Need to use capital letters while using dictionary.members. replace yourlib with libname

proc sql ;
select distinct path as path_var from dictionary.members
where libname= 'YOURLIB';
data abc;
path_var=pathname('yourlib');
run;
Super User
Super User
Posts: 6,502

Re: How do I get the name of the file a Libref is referring to?

The PATHNAME() function will return the path that a libref or fileref is using.

You can use it in a data step.

data _null_;
  length path $256 ;
  path=pathname('mylib');
  put path= ;
run;

Or wrap it in %SYSFUNC() and use it directly in macro code.

%let path=%sysfunc(pathname(mylib));

You can also ask PROC CONTENTS to tell you where a particular dataset is. That is useful if you libref is actually pointing to multiple directories.

proc contents data=mylib.mymember;
run;
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 118 views
  • 2 likes
  • 4 in conversation