Extracting the Base Path of a File

This code is also available in: Deutsch Español Français
Difficulty Level
Beginner
Published on :
This macro takes a full file path as input via the 'pth' parameter and returns only the part of the path that precedes the last directory separator (slash or backslash), thereby excluding the file name and its extension. It is designed for string manipulation within the SAS© macro environment.
Data Analysis

Type : INTERNAL_CREATION


The macro operates on a character string provided as a parameter ('pth') and does not read data from SASHELP tables or external sources. It manipulates character strings in memory to construct the result.

1 Code Block
MACRO DEFINITION
Explanation :
This block defines the `getFileStem` macro. It uses SAS functions `reverse` to reverse the path string, `indexc` to find the position of the first directory separator (slash or backslash, which corresponds to the last in the original reversed string), and `qsubstr` to extract the relevant part. The result is then re-reversed to obtain the base path. The `qleft` and `qsysfunc` functions are used to handle quotes and execute SAS functions in macro mode, ensuring robustness in character string processing.
Copied!
1%macro getFileStem(pth)/des="Extract the path without the file name and extension";
2 %local revstr cutstr gotstm;
3 %let revstr=%qleft(%qsysfunc(reverse(&pth)));
4 %let cutstr=%qsubstr(&revstr,%qsysfunc(indexc(&revstr,%str(/\))));
5 %let gotstm=%qleft(%qsysfunc(reverse(&cutstr)));
6 %str(&gotstm)
7%mend getFileStem;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.

Related Documentation

Aucune documentation spécifique pour cette catégorie.