Auditing the SASAUTOS Library: How to Map and Resolve Macro Autocall Paths

This code is also available in: Deutsch Español English Français
Difficulty Level
Beginner
Published on :
Simon

Expert Advice

Simon
Expert SAS et fondateur.

One of the most robust features of your %getautos macro is the use of the pathname() function. In SAS, the SASAUTOS option can contain both physical directory paths and filerefs. By passing the extracted string through pathname(), your script intelligently determines if the value is a shortcut (like MASTERS) or a direct disk location. Without this step, a simple string parse would often return useless logical names instead of the actual file paths.

This script defines and executes a macro named `%getautos`. This macro reads the value of the `SASAUTOS` system option, which contains a list of paths where SAS© searches for macros. The code parses this string to extract each individual path. It handles various cases, such as paths enclosed in quotes, environment variable substitution (prefixed by `!`) and tilde expansion (`~`) for home directories under Unix/Linux. The macro generates an output table (`work.paths` by default) containing the list of extracted paths. The script concludes by executing the macro and displaying the result, as well as printing the raw value of `SASAUTOS` and `SASROOT`.
Data Analysis

Type : CREATION_INTERNE


The script does not read external data. It generates a data table named 'paths' in the WORK library. The content of this table is derived from the value of the SAS system option 'SASAUTOS'.

1 Code Block
DATA STEP Data
Explanation :
This block defines the `%getautos` macro. It uses a DATA step to process the character string obtained via `GETOPTION('sasautos')`. The logic iterates through the string, extracts each path using the `SCAN` function, handles quotes, and performs substitutions for environment variables (via `SYSGET`) and the tilde (`~`). The `PATHNAME` function is used to resolve filerefs. Each valid path is added to the output table specified by the `out` parameter.
Copied!
1%macro getautos(out=paths);
2 DATA &out ( keep = path ) ;
3 LENGTH q $ 32000 temp $ 300 var path $ 255 ;
4 q = getoption ( "sasautos" ) ;
5 
6 *-- remove surrounding (), and internal quotes;
7 IF index ( q , "(" ) THEN
8 q = left(translate ( q , " " , "()'\"" )) ;
9 
10 DO while ( 1 ) ;
11 
12 temp = scan ( q || "***" , 1 , "() " ) ;
13 
14 IF temp = "***" THEN leave ;
15 
16 q = left(substr ( q , LENGTH ( temp ) + 1)) ;
17 
18 IF temp =: "!" THEN
19 DO ;
20 var = scan ( temp , 1 , "!/\ " ) ;
21 temp = tranwrd ( temp , '!' || trim(var)
22 , sysget(trim(var)) ) ;
23 END ;
24 
25 *-- do tilda expansion. Works for ~/ and ~user/ where user is
26 the current user;
27 IF temp =: "~" & symget('sysscp') ^= 'WIN' THEN
28 DO ;
29 var = scan ( temp , 1 , "~/\" ) ; put var=;
30 home = sysget('HOME'); put home=;
31 if var = ' '
32 then temp = tranwrd( temp, '~', sysget('HOME'));
33 else do;
34 userid = sysget('USER'); put userid=;
35 temp = tranwrd( temp, '~' || trim(userid), sysget('HOME'));
36 put temp=;
37 end;
38 end ;
39 
40 *-- Check for a fileref;
41 path = pathname ( temp ) ;
42 if path = " " then path = temp ;
43 
44 if index ( path , "(" ) then
45 do ;
46 q = trim(left(translate(path, " ", "()'\"""))) || " " || trim ( q ) ;
47 end ;
48 else
49 if path ^= "***" THEN
50 OUTPUT ;
51 END ;
52 RUN ;
53%mend;
2 Code Block
PROC PRINT
Explanation :
This block executes the `%getautos` macro with its default parameters, which creates the `work.paths` table. Then, `PROC PRINT` is used to display the content of this resulting table in the output window.
Copied!
1%getautos();
2PROC PRINT DATA = paths ; RUN ;
3 Code Block
DATA STEP
Explanation :
This `_NULL_` step is not intended to create a SAS table. It uses a `FILE PRINT` statement to write information to the standard output (or log). It retrieves and displays the value of the `SASROOT` environment variable via `SYSGET`, then the raw value of the `SASAUTOS` option. It then performs some string manipulations (`COMPBL`, `COMPRESS`) on the `SASAUTOS` value and displays the results.
Copied!
1DATA _null_;
2 file PRINT;
3 LENGTH sasautos $32000 sasroot $200;
4 sasroot = sysget('SASROOT');
5 put 'SASROOT:' sasroot=;
6 sasautos = getoption('sasautos');
7 put sasautos=;
8 sasautos = compbl(sasautos);
9 put sasautos=;
10 sasautos = compress(sasautos, '("'')');
11 put 'SASAUTOS:' sasautos=;
12 RUN;
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.