Macro XSET (from Fraktal SAS Programming)

Aus phenixxenia.org
Zur Navigation springen Zur Suche springen

Zurück

Übersicht

Vorwärts

What it does

This SAS Macro transfers the environment from the particular operating system to SAS Macro variables.

  • Characters "not appreciated" by SAS are removed from variable values.
  • If a specific variable name is supplied, this will be the only one transferred.
Code executed Function performed

%MACRO xset(xvar);

Start Macro definition with name and positional parameter xvar

filename path pipe "set" lrecl = 2048;

Open source for text data of type pipe pointing to OS command

data ecotope / view = ecotope;

Start datastep run group to create a view

length xvar $256 xval $1024;

Declare variables xvar and xval by length in bytes

infile path dlm = '=';

Utilize data stream from data source path

input xvar $ xval $;

Define structure of data stream path with variables xvar and xval

xvar = upcase(translate(xvar,'_','(-)'));

Remove unwanted characters from variable xvar

%IF %LENGTH(&XVAR.) != 0 %THEN %DO;

Specify branch on macro parameter xvar for using code segment

if trim(xvar) = "%UPCASE(&XVAR.)";

Restrict properties kept to name in macro parameter xvar

%END;

End branch on macro parameter xvar

run;

End data step run group

proc sql noprint;

Initialize SQL interpreter in silent mode

select xvar

Query values in data field xvar

into :xvar_l separated by ' '

Generate macro variable xvar_l from blank-separated list-of-values

from ecotope

Use data step view ecotope

 ;

Trigger immediate query execution

quit;

Terminate SQL interpreter

%GLOBAL &XVAR_L.;

Use list-of-values to declare scope as global

data _NULL_;

Start datastep run group without creating a dataset

set ecotope;

Use data delivered by data step view ecotope

call symput(compress(xvar),compress(xval));

Transfer variable values to symbol table

run;

End data step run group

filename path clear;

Close source for text data

%MEND xset;

End Macro definition with name

How it is done

  1. The Macro first defines the OS command "SET" as text data source (SAS file reference) of type "PIPE" that delivers a dynamic two-column matrix with delimiter "=" upon reference to the fileref.
  2. This fileref is then used to dynamically "populate" a "data step view" that serves as data source for subsequent processing.
  3. From the left side of the matrix accessed the SQL procedure obtains the names of the OS environment variables and stores them as blank-separated list into a macro variable for use later on.
  4. Finally a data step with name "_NULL_" is used to write the two-variable dataset delivered by the data step view into the global symbol table.

Special Effects

Zurück

Übersicht

Vorwärts