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

Aus phenixxenia.org
Zur Navigation springen Zur Suche springen
K
K
 
(10 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
 
|übersicht=Macro Programming (from Fraktal SAS Programming)
 
|übersicht=Macro Programming (from Fraktal SAS Programming)
|zurück=Macro XAMINE (from Fraktal SAS Programming)
+
|zurück=Macro XEDIT (from Fraktal SAS Programming)
|vorwärts=Macro XAMINE (from Fraktal SAS Programming)
+
|vorwärts=Macro XSET (from Fraktal SAS Programming)
 
}}
 
}}
  
%MACRO xamine(xpath,etype);
+
== What it does ==
%GLOBAL ne ie dir;
+
 
%LOCAL xpath xentry entry etype;
+
This multilevel structured macro combines the simpler ones from above to perform adapted processing of entries from a specified path 'XPATH'. Behavior depends on filter arguments supplied by a segmented parameter 'ETYPE'.
filename entries pipe "dir /b &XPATH." lrecl = 256;
+
* Segments are limited hardcoded with an underscore ('_').
filename dirs pipe "dir /b /ad &XPATH." lrecl = 256;  
+
** 1st segment arguments are processed as SAS program code and copied into the SAS DMS Program Editor.
data entries;
+
** 2nd segment arguments are processed as Text and passed to the Wordpad text processor from the MS-Windows accessories collection.
length entry $256;
+
 
infile entries length = lrecl end = EOF;
+
== Annotated Code ==
input entry $varying256. lrecl;
+
 
if EOF then call symput('ne',trim(left(put(_N_,8.))));
+
{| class="wikitable"
run;
+
! Code executed
data dirs;
+
! Function performed
length dir $256;
+
|-
infile dirs length = lrecl end = EOF;
+
|
input dir $varying256. lrecl;
+
<font face="Courier New">
run;
+
;%MACRO xamine(xpath,etype);
%DO ie = 1 %TO &NE.;
+
</font>
data _NULL_;
+
|Start Macro definition with name and positional parameters xpath and etype
set entries(firstobs = &IE. obs = &IE.);
+
|-
call symput('entry',compress(translate(entry,'_','(-)')));
+
|
call symput('xentry',trim(left(entry)));
+
<font face="Courier New">
run;
+
:%GLOBAL ne ie dir;
%PUT |;
+
</font>
%PUT &XPATH.&XENTRY.;
+
|Declare global macro variables for communication with called macros
data _NULL_;
+
|-
  set dirs(where = (dir = "&XENTRY.")) end = EOF;
+
|
if EOF then call symput('dir',trim(left(put(_N_,8.))));
+
<font face="Courier New">
run;
+
:%LOCAL xpath xentry entry etype;
%IF &DIR. = 1 %THEN %DO;
+
</font>
%PUT +----;
+
|Declare local macro variables
%XDIR(&XPATH.&XENTRY.);
+
|-
%PUT +----;
+
|
%END;
+
<font face="Courier New">
%ELSE %DO;
+
;filename entries pipe "dir /b &XPATH." lrecl = 256;
%IF %LENGTH(&ETYPE.) ne 0 %THEN %DO;
+
</font>
%IF %INDEX(%SCAN(&ETYPE.,2,_),%SCAN(&XENTRY,2,.)) <> 0 %THEN %DO;
+
|Open access path to listing of OS directory content
%GLOBAL windir;
+
|-
%XSET(windir);
+
|
%PUT +----;
+
<font face="Courier New">
%PUT | File &XENTRY. opened in external editor WordPad.;
+
;filename dirs pipe "dir /b /ad &XPATH." lrecl = 256;  
SYSTASK command "&WINDIR.\write.exe ""&XPATH.\&XENTRY.""";
+
</font>
%PUT +----;
+
|Open access path to listing of OS directory content of type 'dir'
%END;
+
|-
%IF %INDEX(%SCAN(&ETYPE.,1,_),%SCAN(&XENTRY,2,.)) <> 0 %THEN %DO;
+
|
%PUT +----;
+
<font face="Courier New">
%PUT | File &XENTRY. opened in SAS program editor window.;
+
;data entries;
%XEDIT(&XENTRY.,&XPATH.);
+
</font>
%PUT +----;
+
|Initiate data step to access OS directory content
%END;
+
|-
%END;
+
|
%END;
+
<font face="Courier New">
%LET dir =;
+
:length entry $256;
%END;
+
</font>
proc sql;
+
|Declare length of data field 'entry'
drop table dirs;
+
|-
drop table entries;
+
|
quit;
+
<font face="Courier New">
filename entries clear;
+
:infile entries length = lrecl end = EOF;
filename dirs clear;
+
</font>
%MEND xamine;
+
|Set pointer to access path 'entries'
 +
|-
 +
|
 +
<font face="Courier New">
 +
:input entry $varying256. lrecl;
 +
</font>
 +
|Read from 'entries' with variable field length
 +
|-
 +
|
 +
<font face="Courier New">
 +
:if EOF then call symput('ne',trim(left(put(_N_,8.))));
 +
</font>
 +
|Write count of entries to macro variable 'ne'
 +
|-
 +
|
 +
<font face="Courier New">
 +
;run;
 +
</font>
 +
|Terminate data step
 +
|-
 +
|
 +
<font face="Courier New">
 +
;data dirs;
 +
</font>
 +
|Initiate data step to access OS directories listing
 +
|-
 +
|
 +
<font face="Courier New">
 +
:length dir $256;
 +
</font>
 +
|Declare length of data field 'dir'
 +
|-
 +
|
 +
<font face="Courier New">
 +
:infile dirs length = lrecl end = EOF;
 +
</font>
 +
|Set pointer to access path 'dirs'
 +
|-
 +
|
 +
<font face="Courier New">
 +
:input dir $varying256. lrecl;
 +
</font>
 +
|Read from 'dirs' with variable field length
 +
|-
 +
|
 +
<font face="Courier New">
 +
;run;
 +
</font>
 +
|Terminate data step
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%DO ie = 1 %TO &NE.;
 +
</font>
 +
|Start macro loop to process entries one by one with loop index 'ie'
 +
|-
 +
|
 +
<font face="Courier New">
 +
;data _NULL_;
 +
</font>
 +
|Initiate data step without creating a dataset
 +
|-
 +
|
 +
<font face="Courier New">
 +
:set entries(firstobs = &IE. obs = &IE.);
 +
</font>
 +
|Read row number 'ie' from dataset 'entries'
 +
|-
 +
|
 +
<font face="Courier New">
 +
:call symput('entry',compress(translate(entry,'_','(-)')));
 +
</font>
 +
|Write value from read row to macro variable 'entry' with intermediate name processing
 +
|-
 +
|
 +
<font face="Courier New">
 +
:call symput('xentry',trim(left(entry)));
 +
</font>
 +
|Write value from read row to macro variable 'xentry'
 +
|-
 +
|
 +
<font face="Courier New">
 +
;run;
 +
</font>
 +
|Terminate data step
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%PUT |;
 +
</font>
 +
|Write string '|' to the LOG
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%PUT &XPATH.&XENTRY.;
 +
</font>
 +
|Write full path and filename to the LOG
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%LET dir =;
 +
</font>
 +
|Reset value for macro variable 'dir'
 +
|-
 +
|
 +
<font face="Courier New">
 +
;data _NULL_;
 +
</font>
 +
|Initiate data step without creating a dataset
 +
|-
 +
|
 +
<font face="Courier New">
 +
: set dirs(where = (dir = "&XENTRY.")) end = EOF;
 +
</font>
 +
|Read row from dataset 'dirs' with value equal to content of macro variable 'xentry'
 +
|-
 +
|
 +
<font face="Courier New">
 +
:if EOF then call symput('dir',trim(left(put(_N_,8.))));
 +
</font>
 +
|Write count of read rows to macro variable 'dir'
 +
|-
 +
|
 +
<font face="Courier New">
 +
;run;
 +
</font>
 +
|Terminate data step
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%IF &DIR. = 1 %THEN %DO;
 +
</font>
 +
|Start macro branch to directory processing
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%PUT +----;
 +
</font>
 +
|Write string '+----' to the LOG
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%XDIR(&XPATH.&XENTRY.);
 +
</font>
 +
|Use macro 'xdir' to write list of directory content to the LOG
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%PUT +----;
 +
</font>
 +
|Write string '+----' to the LOG
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%END;
 +
</font>
 +
|End macro branch for directory processing
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%ELSE %DO;
 +
</font>
 +
|Start macro branch for non-directory processing
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%IF %LENGTH(&ETYPE.) ne 0 %THEN %DO;
 +
</font>
 +
|Start macro branch for supplied positional parameter 'etype'
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%IF %INDEX(%SCAN(&ETYPE.,2,_),%SCAN(&XENTRY,2,.)) != 0 %THEN %DO;
 +
</font>
 +
|Start macro branch for extension of macro variable 'xentry' is found in 2nd delimited segment of macro variable 'etype'
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%GLOBAL windir;
 +
</font>
 +
|Declare macro variable 'windir' to be global
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%XSET(windir);
 +
</font>
 +
|Call macro 'xset' to populate macro variable 'windir' from OS environment variable 'windir'
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%PUT +----;
 +
</font>
 +
|Write string '+----' to the LOG
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%PUT | File &XENTRY. opened in external editor WordPad.;
 +
</font>
 +
|Write string '|' followed by a message to the LOG
 +
|-
 +
|
 +
<font face="Courier New">
 +
:SYSTASK command "&WINDIR.\write.exe ""&XPATH.\&XENTRY.""";
 +
</font>
 +
|Call external program 'write.exe' to open currently processed file
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%PUT +----;
 +
</font>
 +
|Write string '+----' to the LOG
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%END;
 +
</font>
 +
|End macro branch for extension of macro variable 'xentry' is found in 2nd delimited segment of macro variable 'etype'
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%IF %INDEX(%SCAN(&ETYPE.,1,_),%SCAN(&XENTRY,2,.)) != 0 %THEN %DO;
 +
</font>
 +
|Start macro branch for extension of macro variable 'xentry' is found in 1st delimited segment of macro variable 'etype'
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%PUT +----;
 +
</font>
 +
|Write string '+----' to the LOG
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%PUT | File &XENTRY. opened in SAS program editor window.;
 +
</font>
 +
|Write string '|' followed by a message to the LOG
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%XEDIT(&XENTRY.,&XPATH.);
 +
</font>
 +
|Call macro 'xedit' to open currently processed file in SAS DMS Program Editor
 +
|-
 +
|
 +
<font face="Courier New">
 +
:%PUT +----;
 +
</font>
 +
|Write string '+----' to the LOG
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%END;
 +
</font>
 +
|End macro branch for extension of macro variable 'xentry' is found in 1st delimited segment of macro variable 'etype'
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%END;
 +
</font>
 +
|End macro branch for supplied positional parameter 'etype'
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%END;
 +
</font>
 +
|End macro branch for non-directory processing
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%END;
 +
</font>
 +
|End macro loop to process entries one by one with loop index 'ie'
 +
|-
 +
|
 +
<font face="Courier New">
 +
;proc sql;
 +
</font>
 +
|Invoke the SAS SQL procedure
 +
|-
 +
|
 +
<font face="Courier New">
 +
:drop table dirs;
 +
</font>
 +
|Delete table 'dirs'
 +
|-
 +
|
 +
<font face="Courier New">
 +
:drop table entries;
 +
</font>
 +
|Delete table 'entries'
 +
|-
 +
|
 +
<font face="Courier New">
 +
;quit;
 +
</font>
 +
|Terminate the SAS SQL procedure
 +
|-
 +
|
 +
<font face="Courier New">
 +
;filename entries clear;
 +
</font>
 +
|Close access path to listing of OS directory content
 +
|-
 +
|
 +
<font face="Courier New">
 +
;filename dirs clear;
 +
</font>
 +
|Close access path to listing of OS directory content of type 'dir'
 +
|-
 +
|
 +
<font face="Courier New">
 +
;%MEND xamine;
 +
</font>
 +
|End Macro definition with name
 +
|}
 +
 
 +
== Special Effects ==
 +
 
 +
# The macro makes use of the 'pipe' type file reference. When used appropriately, this returns a list that is instantly generated by an OS command that is given as the statement's argument.
 +
# As an alternate text processing instance the macro uses a SYSTASK statement to call the windows component 'Wordpad'. Since each paticular windows installation uses its own path structure, the macro makes use of the 'windir' environment variable.
  
 
{{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
 
|übersicht=Macro Programming (from Fraktal SAS Programming)
 
|übersicht=Macro Programming (from Fraktal SAS Programming)
|zurück=Macro XAMINE (from Fraktal SAS Programming)
+
|zurück=Macro XEDIT (from Fraktal SAS Programming)
|vorwärts=Macro XAMINE (from Fraktal SAS Programming)
+
|vorwärts=Macro XSET (from Fraktal SAS Programming)
 
}}
 
}}

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

Zurück

Übersicht

Vorwärts

What it does

This multilevel structured macro combines the simpler ones from above to perform adapted processing of entries from a specified path 'XPATH'. Behavior depends on filter arguments supplied by a segmented parameter 'ETYPE'.

  • Segments are limited hardcoded with an underscore ('_').
    • 1st segment arguments are processed as SAS program code and copied into the SAS DMS Program Editor.
    • 2nd segment arguments are processed as Text and passed to the Wordpad text processor from the MS-Windows accessories collection.

Annotated Code

Code executed Function performed

%MACRO xamine(xpath,etype);

Start Macro definition with name and positional parameters xpath and etype

%GLOBAL ne ie dir;

Declare global macro variables for communication with called macros

%LOCAL xpath xentry entry etype;

Declare local macro variables

filename entries pipe "dir /b &XPATH." lrecl = 256;

Open access path to listing of OS directory content

filename dirs pipe "dir /b /ad &XPATH." lrecl = 256;

Open access path to listing of OS directory content of type 'dir'

data entries;

Initiate data step to access OS directory content

length entry $256;

Declare length of data field 'entry'

infile entries length = lrecl end = EOF;

Set pointer to access path 'entries'

input entry $varying256. lrecl;

Read from 'entries' with variable field length

if EOF then call symput('ne',trim(left(put(_N_,8.))));

Write count of entries to macro variable 'ne'

run;

Terminate data step

data dirs;

Initiate data step to access OS directories listing

length dir $256;

Declare length of data field 'dir'

infile dirs length = lrecl end = EOF;

Set pointer to access path 'dirs'

input dir $varying256. lrecl;

Read from 'dirs' with variable field length

run;

Terminate data step

%DO ie = 1 %TO &NE.;

Start macro loop to process entries one by one with loop index 'ie'

data _NULL_;

Initiate data step without creating a dataset

set entries(firstobs = &IE. obs = &IE.);

Read row number 'ie' from dataset 'entries'

call symput('entry',compress(translate(entry,'_','(-)')));

Write value from read row to macro variable 'entry' with intermediate name processing

call symput('xentry',trim(left(entry)));

Write value from read row to macro variable 'xentry'

run;

Terminate data step

%PUT |;

' to the LOG

%PUT &XPATH.&XENTRY.;

Write full path and filename to the LOG

%LET dir =;

Reset value for macro variable 'dir'

data _NULL_;

Initiate data step without creating a dataset

set dirs(where = (dir = "&XENTRY.")) end = EOF;

Read row from dataset 'dirs' with value equal to content of macro variable 'xentry'

if EOF then call symput('dir',trim(left(put(_N_,8.))));

Write count of read rows to macro variable 'dir'

run;

Terminate data step

%IF &DIR. = 1 %THEN %DO;

Start macro branch to directory processing

%PUT +----;

Write string '+----' to the LOG

%XDIR(&XPATH.&XENTRY.);

Use macro 'xdir' to write list of directory content to the LOG

%PUT +----;

Write string '+----' to the LOG

%END;

End macro branch for directory processing

%ELSE %DO;

Start macro branch for non-directory processing

%IF %LENGTH(&ETYPE.) ne 0 %THEN %DO;

Start macro branch for supplied positional parameter 'etype'

%IF %INDEX(%SCAN(&ETYPE.,2,_),%SCAN(&XENTRY,2,.)) != 0 %THEN %DO;

Start macro branch for extension of macro variable 'xentry' is found in 2nd delimited segment of macro variable 'etype'

%GLOBAL windir;

Declare macro variable 'windir' to be global

%XSET(windir);

Call macro 'xset' to populate macro variable 'windir' from OS environment variable 'windir'

%PUT +----;

Write string '+----' to the LOG

%PUT | File &XENTRY. opened in external editor WordPad.;

' followed by a message to the LOG

SYSTASK command "&WINDIR.\write.exe ""&XPATH.\&XENTRY.""";

Call external program 'write.exe' to open currently processed file

%PUT +----;

Write string '+----' to the LOG

%END;

End macro branch for extension of macro variable 'xentry' is found in 2nd delimited segment of macro variable 'etype'

%IF %INDEX(%SCAN(&ETYPE.,1,_),%SCAN(&XENTRY,2,.)) != 0 %THEN %DO;

Start macro branch for extension of macro variable 'xentry' is found in 1st delimited segment of macro variable 'etype'

%PUT +----;

Write string '+----' to the LOG

%PUT | File &XENTRY. opened in SAS program editor window.;

' followed by a message to the LOG

%XEDIT(&XENTRY.,&XPATH.);

Call macro 'xedit' to open currently processed file in SAS DMS Program Editor

%PUT +----;

Write string '+----' to the LOG

%END;

End macro branch for extension of macro variable 'xentry' is found in 1st delimited segment of macro variable 'etype'

%END;

End macro branch for supplied positional parameter 'etype'

%END;

End macro branch for non-directory processing

%END;

End macro loop to process entries one by one with loop index 'ie'

proc sql;

Invoke the SAS SQL procedure

drop table dirs;

Delete table 'dirs'

drop table entries;

Delete table 'entries'

quit;

Terminate the SAS SQL procedure

filename entries clear;

Close access path to listing of OS directory content

filename dirs clear;

Close access path to listing of OS directory content of type 'dir'

%MEND xamine;

End Macro definition with name

Special Effects

  1. The macro makes use of the 'pipe' type file reference. When used appropriately, this returns a list that is instantly generated by an OS command that is given as the statement's argument.
  2. As an alternate text processing instance the macro uses a SYSTASK statement to call the windows component 'Wordpad'. Since each paticular windows installation uses its own path structure, the macro makes use of the 'windir' environment variable.

Zurück

Übersicht

Vorwärts