SAS Pathname

5 techniques of finding paths or locations in SAS

This article discusses the techniques used to find and determine path and location information.

1. Using the PATHNAME function

The PATHNAME function returns the physical path for a given fileref or libref.

Determining the location of the file using filref.

Using the pathname function on a fileref returns the path of the filename.

filename prgrm "/home/subhroster20070/sasprgrm.sas";
%let prgmpath = %sysfunc(pathname(prgrm)); 
%put &prgmpath;

The LOG shows the location of the files below:

/home/subhroster20070/sasprgrm.sas

Determining the location of the library using the libref

Using PATHNAME on a libref returns the path to that directory.

libname mylib "/home/subhroster20070/examples";
%let libpath = %sysfunc(pathname(mylib)); 
%put &libpath;

And here is the LOG below.

/home/subhroster20070/examples

You can even use it on concatenated filerefs such as the autocall library.

To get the current location of all of the locations in the SASAUTOS fileref you could specify:

%sysfunc(pathname(sasautos));

2. SASHELP.VIEWS and DICTIONARY Tables

The path information for existing librefs and filerefs can be consolidated and displayed by examining the SASHELP views and SQL DICTIONARY tables.

The path would be returned by the PATHNAME function, including other information like LIBNAME, ENGINE and many more.

SASHELP.VLIBNAM and DICTIONARY.LIBNAMES

SASHELP.VLIBNAM contains the libref and path information for each libref. A sample of the listing of this table is shown below.

SASHELP.VLIBNAM

The below DATA step extracts the appropriate row and saves the path in a macro variable concatenated with the row number.

data _null_;
set sashelp.vlibnam(keep=libname path where=(upcase(libname)='MYLIB'));
call symputx(cats('mylib',_n_),path); 
run;
%put &mylib3;

The macro variable &mylib3 now contains the path associated with the libref path for row number 3.

Similar information can be extracted from the LIBNAMES DICTIONARY table through an SQL step.

proc sql noprint;
describe table dictionary.libnames; 
select count(*) into :nobs from dictionary.libnames;
select path into :sqllibpath1-:sqllibpath%left(&nobs)
from dictionary.libnames where upcase(libname)='MYLIB';
quit;
%put &sqllibpath3;

The DESCRIBE statement used here is optional. It only writes the names of the table’s columns to the LOG.

3. Path information for external file

SASHELP.VEXTFL or DICTIONARY.EXTFILES can be used to determine the path information of external files.

The data step is identical to the one used to retrieve the path for the libref, except for the different file and variable names.

data _null_;
set sashelp.vextfl(keep=fileref xpath
where=(fileref='SASPRGM')); call symputx('prgmpath',xpath);
run;

%put &prgmpath;

In SQL, the DICTIONARY.EXTFILES table contains the same information as in the SASHELP.VEXTFL.The SQL step could be something like the below:

proc sql noprint;
select xpath into :pgrmsqlpath
from dictionary.extfiles where fileref='SASPRGM';
quit;
%put &prgmsqlpath;

4. Finding Formats

SAS Formats are stored in catalogs. Since format catalogs can be concatenated across multiple libraries, you must first determine the libraries for which you need to determine the paths.

The FMTSEARCH system option will list the libraries that SAS will search when looking for a format. This option can be accessed by using the GETOPTION function.

Using the data step, you can write the code below.

%let fmtlibs= %sysfunc(translate(%sysfunc(getoption(fmtsearch)),%str( ),%str(%( %))));

Data test(keep=libnames path);
liblist=symget('fmtlibs');

do i=1 to countw(liblist);
libnames=compress(scan(liblist,i,' '));
path=pathname(libnames);
output;
end;
run;
5 techniques of finding paths or locations in SAS

Using Macro

%macro fmtlibs; %sysfunc(translate(%sysfunc(getoption(fmtsearch)),%str( ),%str(%( %))))
%mend fmtlibs;

This %FMTLIBS returns the list of search locations enclosed in parentheses. So, we have used the translate function to replace the parenthesis with spaces.

(MYLIB)

The resulting list of librefs MYLIB can now be passed to the PATHNAME function to return the location.

%macro pathinfo(liblist);
		%local cnt libref path;
		%let cnt = 0;

		%do %while(%qscan(&liblist, &cnt+1, %str(%( %))) ne);
			%let cnt = %eval(&cnt+1);
			%let libref = %qscan(%bquote(&liblist), &cnt, %str(%( %)));
			%let path = %sysfunc(pathname(&libref));
			%put &libref &path;
		%end;
		%mend pathinfo;
%pathinfo(%fmtlibs);

And here is what you get in the log.

MYLIB /home/subhroster20070/examples
FRMTLIB /home/subhroster20070/my_content

You can also get useful information for formats and format catalogs through the view SASHELP.VFORMAT.

This view contains one row per format, including SAS and user-defined formats. For user-defined formats, it contains the libref and catalog name. So, you need to find out the path separately.

Below is the snapshot of SASHELP.VFORMATS table.

SASHELP.VFORMATS

Similar information can be determined using the SQL table DICTIONARY.FORMATS.

5. Determining the executing program name and path.

You can automatically detect the name or location of an executing program.

In batch mode, the name of the executing program is stored in the system option SYSIN, and the value of system options can be retrieved using the GETOPTION function.

%sysget(SAS_EXECFILENAME)

If you need to know the location of the SAS program when executing from the Enhanced Editor, you can use the SAS_EXECFILEPATH environmental variable.

%macro pathname; 
%sysget(SAS_EXECFILEPATH)
%mend pathname;
%put pathname;

The %PATHNAME macro returns both the path and the name of the executing program.

References

Carpenter’s Complete Guide to the SAS Macro Language, Third Edition

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This Post Has 3 Comments

  1. Milo

    Thanks, I’ve just been looking for info approximately this topic for ages and
    yours is the best I’ve found out till now.

  2. Russ

    Thanks for this, very useful. Just politely pointing out a small typo in this section…

    %let libpath = %sysfunc(pathname(mylib));
    %put libpath;

    Literally a missing ampersand. Should read,

    %let libpath = %sysfunc(pathname(mylib));
    %put &libpath.;

    1. Subhro

      Thanks. Typo has been corrected. Appreciate your help!!