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.
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!
%macro getautos(out=paths);
data &out ( keep = path ) ;
length q $ 32000 temp $ 300 var path $ 255 ;
q = getoption ( "sasautos" ) ;
*-- remove surrounding (), and internal quotes;
if index ( q , "(" ) then
q = left(translate ( q , " " , "()'\"" )) ;
do while ( 1 ) ;
temp = scan ( q || "***" , 1 , "() " ) ;
if temp = "***" then leave ;
q = left(substr ( q , length ( temp ) + 1)) ;
if temp =: "!" then
do ;
var = scan ( temp , 1 , "!/\ " ) ;
temp = tranwrd ( temp , '!' || trim(var)
, sysget(trim(var)) ) ;
end ;
*-- do tilda expansion. Works for ~/ and ~user/ where user is
the current user;
if temp =: "~" & symget('sysscp') ^= 'WIN' then
do ;
var = scan ( temp , 1 , "~/\" ) ; put var=;
home = sysget('HOME'); put home=;
if var = ' '
then temp = tranwrd( temp, '~', sysget('HOME'));
else do;
userid = sysget('USER'); put userid=;
temp = tranwrd( temp, '~' || trim(userid), sysget('HOME'));
put temp=;
end;
end ;
*-- Check for a fileref;
path = pathname ( temp ) ;
if path = " " then path = temp ;
if index ( path , "(" ) then
do ;
q = trim(left(translate(path, " ", "()'\"""))) || " " || trim ( q ) ;
end ;
else
if path ^= "***" then
output ;
end ;
run ;
%mend;
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
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!
%getautos();
proc print data = paths ; run ;
1
%getautos();
2
PROC PRINTDATA = 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!
data _null_;
file print;
length sasautos $32000 sasroot $200;
sasroot = sysget('SASROOT');
put 'SASROOT:' sasroot=;
sasautos = getoption('sasautos');
put sasautos=;
sasautos = compbl(sasautos);
put sasautos=;
sasautos = compress(sasautos, '("'')');
put 'SASAUTOS:' sasautos=;
run;
1
DATA _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.
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.