Published on :
Macro CREATION_INTERNE

Temporary File Name Generation

This code is also available in: Deutsch Español Français
This macro generates a unique file path by combining a prefix, a random string based on hashing (CRC32 of job ID, process ID, time, random), and an optional extension. If no path is provided, it uses the physical path of the WORK library. The result is returned in a calling macro variable. It includes error state management (SYSCC) to avoid polluting the log in case of successful internal execution.
Data Analysis

Type : CREATION_INTERNE


Processing is performed via a DATA _NULL_ step that manipulates character strings and system functions to generate the file name.

1 Code Block
Macro Definition
Explanation :
Macro initialization: declaration of local variables, call to an internal debug macro, and saving of the current system error state (SYSCC, SYSMSG) to isolate macro execution.
Copied!
1%macro cxtf_tempfile( prefix = __cxtf, path = , fileext = , return = );
2 
3 %local _cxtf_rc _cxtf_syscc _cxtf_sysmsg _cxtf_debug
4 _str
5 ;
6 
7 %* PRINT debug details ;
8 %_cxtf_debug();
9 
10 
11 %* -- capture entry state ;
12 %let _cxtf_syscc = 0;
13 %let _cxtf_sysmsg = ;
14 
15 %IF ( &syscc ^= 0 ) %THEN %DO;
16 %let _cxtf_syscc = &syscc;
17 %let _cxtf_sysmsg = &sysmsg;
18 
19 %let syscc = 0;
20 %let sysmsg = ;
21 
22 %END;
2 Code Block
DATA STEP
Explanation :
Core logic: determines the root directory (parameter or WORK), generates a unique random string (based on hashing system info), adds the extension, and constructs the full path. The result is stored in the return macro variable via CALL SYMPUT.
Copied!
1 DATA _null_ ;
2 
3 LENGTH root $ 4096
4 str $ 1024 ;
5 
6 %* -- identify parent path ;
7 %* note: IF not specified, using path of WORK library ;
8 root = symget('path');
9 
10 IF ( missing(root) ) THEN
11 root = pathname('WORK');
12 
13 
14 %* initililze with prefix ;
15 str = strip(symget('prefix'));
16 
17 
18 %* force first character to be A-Z-ish (thanks SAS) ;
19 IF missing(str) THEN
20 str = byte( mod( floor(100*rand("uniform")), 26) + rank("A") - 1 ) ;
21 
22 %* add random string ;
23 call cats( str, hashing( "crc32", catx( "-", symget('sysjobid'), symget('sysprocessid'), put( rand("uniform"), best32.), put( datetime(), datetime23.3) ) ) );
24 
25 
26 %* add file extension;
27 IF ( not missing( symget('fileext') ) ) THEN
28 call catx( '.', str, symget('fileext') );
29 
30 
31 %* assign OUTPUT value ;
32 call symput( symget('return'), translate( catx( "/", root, lowcase(str) ), "/", "\") );
33 
34 RUN;
3 Code Block
Macro Cleanup
Explanation :
Restores the state of system error codes (SYSCC) as it was before macro execution, ensuring that the macro does not clear previous errors.
Copied!
1 %* -- macro exit point ;
2 %macro_exit:
3 
4 
5 
6 %* -- restore entry state ;
7 %IF ( &_cxtf_syscc ^= 0 ) %THEN %DO;
8 %let syscc = &_cxtf_syscc;
9 %let sysmsg = &_cxtf_sysmsg;
10 %END;
11 
12 
13%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.