Macro XSET (from Fraktal SAS Programming): Unterschied zwischen den Versionen

Aus phenixxenia.org
Zur Navigation springen Zur Suche springen
K
K
 
(13 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
 
[[Kategorie:Zazy]]
 
[[Kategorie:Zazy]]
 
{{SeitenNavigation1
 
{{SeitenNavigation1
|hoch=Duck_zazy_com.png
+
|hoch=call a macro bubble 0.png
 
|links=xx_left.png
 
|links=xx_left.png
 
|rechts=xx_right.png
 
|rechts=xx_right.png
Zeile 14: Zeile 14:
 
* Characters ''"not appreciated"'' by '''''SAS''''' are removed from variable values.
 
* Characters ''"not appreciated"'' by '''''SAS''''' are removed from variable values.
 
* If a specific variable name is supplied, this will be the only one transferred.
 
* If a specific variable name is supplied, this will be the only one transferred.
 +
 +
== Annotated Code ==
  
 
{| class="wikitable"
 
{| class="wikitable"
Zeile 27: Zeile 29:
 
|
 
|
 
<font face="Courier New">  
 
<font face="Courier New">  
:filename path pipe "set" lrecl = 2048;
+
;filename path pipe "set" lrecl = 2048;
 
</font>  
 
</font>  
 
|Open source for text data of type ''pipe'' pointing to OS command
 
|Open source for text data of type ''pipe'' pointing to OS command
Zeile 35: Zeile 37:
 
;data ecotope / view = ecotope;  
 
;data ecotope / view = ecotope;  
 
</font>  
 
</font>  
|
+
|Start datastep run group to create a view
 
|-
 
|-
 
|
 
|
Zeile 65: Zeile 67:
 
;%IF %LENGTH(&XVAR.) != 0 %THEN %DO;
 
;%IF %LENGTH(&XVAR.) != 0 %THEN %DO;
 
</font>  
 
</font>  
|Specify condition on macro parameter ''xvar'' for using code segment  
+
|Specify branch on macro parameter ''xvar'' for using code segment  
 
|-
 
|-
 
|
 
|
 
<font face="Courier New">  
 
<font face="Courier New">  
:if trim(xvar) = "%UPCASE(&XVAR.)";
+
:if upcase(trim(xvar)) = upcase(symget('XVAR'));
 
</font>  
 
</font>  
|Conditionally executed code
+
|Restrict properties kept to name in macro parameter ''xvar''
 
|-
 
|-
 
|
 
|
Zeile 77: Zeile 79:
 
;%END;
 
;%END;
 
</font>  
 
</font>  
|End condition on macro parameter ''xvar''
+
|End branch on macro parameter ''xvar''
 
|-
 
|-
 
|
 
|
 
<font face="Courier New">  
 
<font face="Courier New">  
 
;run;
 
;run;
|
+
</font>
|-
+
|End data step run group
 
|-
 
|-
 
|
 
|
 
<font face="Courier New">  
 
<font face="Courier New">  
 
;proc sql noprint;
 
;proc sql noprint;
|
+
</font>
|-
+
|Initialize SQL interpreter in silent mode
 
|-
 
|-
 
|
 
|
 
<font face="Courier New">  
 
<font face="Courier New">  
 
:select xvar
 
:select xvar
|
+
</font>
|-
+
|Query values in data field ''xvar''
 
|-
 
|-
 
|
 
|
 
<font face="Courier New">  
 
<font face="Courier New">  
 
:  into :xvar_l separated by ' '
 
:  into :xvar_l separated by ' '
|
+
</font>
|-
+
|Generate macro variable ''xvar_l'' from blank-separated list-of-values
 
|-
 
|-
 
|
 
|
 
<font face="Courier New">  
 
<font face="Courier New">  
 
:  from ecotope
 
:  from ecotope
|
+
</font>
|-
+
|Use data step view ''ecotope''
 
|-
 
|-
 
|
 
|
 
<font face="Courier New">  
 
<font face="Courier New">  
 
: ;
 
: ;
|
+
</font>
|-
+
|Trigger immediate query execution
 
|-
 
|-
 
|
 
|
 
<font face="Courier New">  
 
<font face="Courier New">  
 
;quit;
 
;quit;
|
+
</font>
|-
+
|Terminate SQL interpreter
 
|-
 
|-
 
|
 
|
 
<font face="Courier New">  
 
<font face="Courier New">  
 
:%GLOBAL &XVAR_L.;
 
:%GLOBAL &XVAR_L.;
|
+
</font>
|-
+
|Use list-of-values to declare scope as global
 
|-
 
|-
 
|
 
|
Zeile 131: Zeile 133:
 
;data _NULL_;
 
;data _NULL_;
 
</font>  
 
</font>  
|Start datastep run group
+
|Start datastep run group without creating a dataset
 
|-
 
|-
 
|
 
|
 
<font face="Courier New">  
 
<font face="Courier New">  
 
: set ecotope;
 
: set ecotope;
|
+
</font>
|-
+
|Use data delivered by data step view ''ecotope''
 
|-
 
|-
 
|
 
|
Zeile 166: Zeile 168:
 
== How it is done ==
 
== How it is done ==
  
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.
+
# 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.
 +
# This fileref is then used to dynamically "populate" a '''"data step view"''' that serves as data source for subsequent processing.
 +
# 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.
 +
# 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.
  
This fileref is then used to dynamically "populate" a '''"data step view"''' that serves as data source for subsequent processing.
+
== Special Effects ==
  
From the left side of the matrix accessed the macro obtains the names of the OS environment variables and stores them as blank-separated list into a macro variable for use later on.
+
This example avoids writing static data tables and therefore uses a neat choice of '''''SAS''''' functionality to accomplish this.
  
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.
+
=== Data Step View ===
  
== Special Effects ==
+
Since the environment managed by the underlying OS is due to change by other processes the use of a data view appears appropriate here. '''''SAS''''' provides the option to store the data step coded processing logic as valid data source of member type '''"VIEW"'''. Since this is not an SQL view the DESCRIBE VIEW command does not work here. Instead, use this code:
 +
 
 +
data view = ecotope;
 +
describe;
 +
run;
 +
 
 +
Because the macro variable resolution initialized by the '''macro trigger ''"&"''''' is done during preprocessing, the macro's behavior will not be really dynamic for the value used is the one present during preprocessing time, if any. It is therefore strongly recommended to read macro parameter's directly from the symbol tables during execution time. The function '''''"SYMGET"''''' does exactly this.
 +
 
 +
=== Dynamic List of Macro Parameters ===
 +
 
 +
Due to the behavior of '''''SAS''''' when writing macro variables it is necessary here to declare the newly generated variables' scope as global. If this is not done, the variables' scope will remain local and hence, will be deleted after the macro terminates.
 +
 
 +
To accomplish this task, the SQL procedure is used to generate the blank-separated list-of-values from field ''xvar'' which is then used to populate the '''''%GLOBAL''''' statements arguments. The OS environment is then copied to a symbol table that remains available even after macro termination.
 +
 
 +
Finally, to not leave unwanted remainders in static data tables the '''reserved dataset name ''"_NULL_"''''' is used here to avoid generation of a data set.
  
 
{{SeitenNavigation1
 
{{SeitenNavigation1
|hoch=Duck_zazy_com.png
+
|hoch=call a macro bubble 0.png
 
|links=xx_left.png
 
|links=xx_left.png
 
|rechts=xx_right.png
 
|rechts=xx_right.png

Aktuelle Version vom 6. Januar 2016, 18:37 Uhr

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.

Annotated Code

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 upcase(trim(xvar)) = upcase(symget('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

This example avoids writing static data tables and therefore uses a neat choice of SAS functionality to accomplish this.

Data Step View

Since the environment managed by the underlying OS is due to change by other processes the use of a data view appears appropriate here. SAS provides the option to store the data step coded processing logic as valid data source of member type "VIEW". Since this is not an SQL view the DESCRIBE VIEW command does not work here. Instead, use this code:

data view = ecotope;
describe;
run;

Because the macro variable resolution initialized by the macro trigger "&" is done during preprocessing, the macro's behavior will not be really dynamic for the value used is the one present during preprocessing time, if any. It is therefore strongly recommended to read macro parameter's directly from the symbol tables during execution time. The function "SYMGET" does exactly this.

Dynamic List of Macro Parameters

Due to the behavior of SAS when writing macro variables it is necessary here to declare the newly generated variables' scope as global. If this is not done, the variables' scope will remain local and hence, will be deleted after the macro terminates.

To accomplish this task, the SQL procedure is used to generate the blank-separated list-of-values from field xvar which is then used to populate the %GLOBAL statements arguments. The OS environment is then copied to a symbol table that remains available even after macro termination.

Finally, to not leave unwanted remainders in static data tables the reserved dataset name "_NULL_" is used here to avoid generation of a data set.

Zurück

Übersicht

Vorwärts