Published on :
Macro CREATION_INTERNE

Extract File Name and Path from an Absolute Path

This code is also available in: Français Deutsch English Español
This SAS© macro-program, `_getAbsPathComponents`, takes an absolute file path (using '/' as a separator) as input. It identifies the position of the last '/' separator to isolate the file name from the rest of the path. Two macro variables are created as output: one containing the file name and the other containing the parent directory path. The code handles cases where the path does not contain a separator, considering it entirely as a file name.
Data Analysis

Type : CREATION_INTERNE


The macro-program does not read any tabular data. It operates exclusively on the character string provided as an input parameter (`i_absPath`) to extract its components.

1 Code Block
Macro
Explanation :
This block defines the `_getAbsPathComponents` macro. It first initializes the output macro variables. It checks if the input string `i_absPath` is not empty. If it contains a '/' separator, it uses `countw` and `scan` to find the last element (the file name) and `findw` for its starting position. The `substr` function is then used to extract the path and the file name. If no separator is found, the entire string is considered the file name.
Copied!
1%MACRO _getAbsPathComponents (i_absPath =
2 ,o_fileName =
3 ,o_pathWithoutName =
4 );
5 
6 %LOCAL l_pathElementCount;
7 %LOCAL l_fileNameStartPos;
8 
9 %LET &o_fileName=;
10 %LET &o_pathWithoutName=;
11 
12 %IF "%sysfunc(compress(&i_absPath))" NE "" %THEN %DO; /*if not empty input string*/
13 %IF %sysfunc(index(&i_absPath.,/)) GT 0 %THEN %DO; /*input string contains dir separators*/
14 %LET l_pathElementCount=%sysfunc(countw(&i_absPath.,/));
15 %LET l_fileNameStartPos=%sysfunc(findw(&i_absPath.,%scan(&i_absPath,&l_pathElementCount,/)));
16 %LET &o_pathWithoutName=%sysfunc(substr(&i_absPath.,1,%EVAL(&l_fileNameStartPos. - 2)));
17 %LET &o_fileName = %sysfunc(substr(&i_absPath.,&l_fileNameStartPos.));
18 %END; /*IF %sysfunc(index(&i_absPath.,'/')) GT 0*/
19 %ELSE %DO; /*input string is a filename only*/
20 %LET &o_fileName = &i_absPath;
21 %END;
22 %END; /*IF "%sysfunc(compress(&i_absPath))" NE ""*/
23
24%MEND _getAbsPathComponents;
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.
Copyright Info : Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de. This file is part of SASUnit, the Unit testing framework for SAS(R) programs.