5 techniques of finding paths or locations in SAS

This article is about the techniques used for finding and determining path and location information.

Using the PATHNAME function

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

Determining location of 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 as below:

/home/subhroster20070/sasprgrm.sas

Determining location of 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));

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.

A 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 columns of the table to the LOG.

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 differen 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 below:

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

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 we 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 datastep, you can write the code as 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 1

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, I have used 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 use of the view SASHELP.VFORMAT.

This view contains one row per format which includes SAS formats 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.

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

Subhro Kar

Been in the realm with the professionals of the IT industry. I am passionate about Coding, Blogging, Web Designing and deliver creative and useful content for a wide array of audience.

>