Published on :
Macro EXTERNE

Write environment variables to a SAS dataset

This code is also available in: Deutsch Français
Awaiting validation
Attention : This code requires administrator privileges.
This macro executes the system command 'set' via a 'pipe' to capture all environment variables. It then parses the output to create a SAS© dataset containing the name and value of each variable. The default output dataset is '_env2ds', but it can be customized. The code is designed to be generic but mentions an alternative for Windows 7 where anonymous 'pipes' might not work.
Data Analysis

Type : EXTERNE


The data is dynamically generated by executing the system command 'set' via the 'filename pipe' instruction. The source is therefore not a pre-existing file but the standard output of this system command, which lists the session's environment variables.

1 Code Block
FILENAME
Explanation :
This instruction associates the fileref '_env2ds' with a 'pipe'. It executes the 'set' system command and captures its standard output so that it can be read as a flat file by SAS.
Copied!
1filename _env2ds pipe 'set';
2 Code Block
DATA STEP Data
Explanation :
This DATA STEP block reads data from the '_env2ds' 'pipe'. For each line read ('input'), it extracts the environment variable name by taking the part before the '=' sign and its value by taking the part after. A condition checks that the value is not empty before assigning it. 'name' and 'value' variables are created with predefined lengths and descriptive labels.
Copied!
1DATA &dsout;
2 LENGTH name $ 40 value $ 1000;
3 INFILE _env2ds;
4 INPUT;
5 name=scan(_infile_,1,"=");
6 IF scan(_infile_,2,"=") NE " " THEN value=substr(_infile_,index(_infile_,"=")+1);
7 label name="Environment Variable Name"
8 value="Environment Variable Value"
9 ;
10RUN;
3 Code Block
FILENAME
Explanation :
This instruction frees the '_env2ds' fileref and closes the associated 'pipe', which is good practice for resource cleanup.
Copied!
1filename _env2ds clear;
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.
Copyright Info : This is public domain software. No guarantee as to suitability or accuracy is given or implied. User uses this code entirely at their own risk.