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.
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!
data _null_ ;
length root $ 4096
str $ 1024 ;
%* -- identify parent path ;
%* note: if not specified, using path of WORK library ;
root = symget('path');
if ( missing(root) ) then
root = pathname('WORK');
%* initililze with prefix ;
str = strip(symget('prefix'));
%* force first character to be A-Z-ish (thanks SAS) ;
if missing(str) then
str = byte( mod( floor(100*rand("uniform")), 26) + rank("A") - 1 ) ;
%* add random string ;
call cats( str, hashing( "crc32", catx( "-", symget('sysjobid'), symget('sysprocessid'), put( rand("uniform"), best32.), put( datetime(), datetime23.3) ) ) );
%* add file extension;
if ( not missing( symget('fileext') ) ) then
call catx( '.', str, symget('fileext') );
%* assign output value ;
call symput( symget('return'), translate( catx( "/", root, lowcase(str) ), "/", "\") );
run;
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) ;
Explanation : Restores the state of system error codes (SYSCC) as it was before macro execution, ensuring that the macro does not clear previous errors.
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.