Published on :
Macro INTERNAL_CREATION

Generation of unique temporary filename (AHGgettempfilename)

This code is also available in: Deutsch Español Français
The `AHGgettempfilename` macro loops until it finds a filename that does not physically exist on disk (checked via the `fileExist` function). It constructs the name using a prefix and a random part derived from the `normal(0)` function. The result is dynamically stored in the macro variable whose name is passed as a parameter.
Data Analysis

Type : INTERNAL_CREATION


No external data is read. The script generates character strings (filenames) algorithmically.

1 Code Block
MACRO
Explanation :
Macro definition. It uses a `%do %until` loop coupled with `%sysfunc(fileExist(...))` to ensure file uniqueness. The target variable (passed by reference via `tempname`) is updated at each iteration with a new candidate containing a random string.
Copied!
1%macro AHGgettempfilename(tempname,dir=%AHGtempdir,start=&tempname,ext=txt);
2 %local rdn ;
3 %DO %until (not %sysfunc(fileExist(&&&tempName)) );
4 %let rdn=%sysfunc(normal(0));
5 %let rdn=%sysfunc(translate(&rdn,00,.-));
6 %let &tempname=T_&start.._%substr(&rdn,1,5).&ext;
7 %END;
8 %put &tempname=&&&tempname;
9%mend;
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.