![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
Contents: | Purpose / History / Requirements / Usage / Details / Limitations / Missing Values / See Also |
%RunBY(version)
The RunBY macro always attempts to check for a later version of itself. If it is unable to do this (such as if there is no active internet connection available), the macro will issue the following message:
NOTE: Unable to check for newer version
The computations performed by the macro are not affected by the appearance of this message.
Version
|
Update Notes
|
1.1 | Added lvlquote= and print= |
1.0 | Initial coding |
%inc "<location of your file containing the RunBY macro>";
After running the above statement, the RunBY macro is ready for use. However, before you call the macro, you must create a simple macro, named code, that contains the code that you want to apply to each BY group. See Details below and the Results tab for examples.
The following parameter is required when using the RunBY macro:
The following parameters are optional:
User-defined macro, code, and special macro variables _BYx, _LVLx, and _BYlabel
You specify the code that you want to run on each BY group in a simple macro called code. In order for your code to select one BY group at a time, it should contain a subsetting condition using a special pair of macro variables, _BYx and _LVLx, where x is a number representing the x-th variable specified in by=. Typically, your code will do this by containing a WHERE statement or clause that uses these macro variables. If a by= variable is character, it is not necessary to specify quotation marks around _LVLx in the condition. Additionally, the macro variable, BYlabel, is available for labeling the displayed results with the BY group definition. BYlabel can be used in a TITLE, FOOTNOTE, or other appropriate statement or option.
Example
Following is an example where the BY groups are defined by the single character variable, Gender, that has values Female and Male in data set MyData. You want to run a macro (not shown) called MyMacro for each level of Gender. You precede your code with a %macro code(); statement, and follow it with a %mend; statement as shown. Assuming that MyMacro does not have built-in BY processing capabilities, an analysis on one Gender level needs to subset the data and then run MyMacro on that subset. The subsetting is easily done with a WHERE statement in a DATA step to create the subset. A WHERE clause in the SET statement could be used instead. When the RunBY macro runs, it replaces the macro variables, _BY1 and _LVL1, with Gender="Female" for one analysis, and then with Gender="Male" for the second analysis. Note that you do not need to specify quotation marks around _LVL1 in the WHERE statement condition since this is done for you by lvlquote=yes. The results are labeled with a title line showing the appropriate one of these two BY group definitions. This assumes that MyMacro itself does not overwrite any prior title. If so, perhaps a FOOTNOTE statement could be used instead. Note the use of the ampersand (&) character to allow resolution of the _BY1, _LVL1, and BYlabel macro variables.
%macro code(); data subset; set MyData; where &_BY1 = &_LVL1; run; title "&BYlabel"; %MyMacro(data=subset) title; %mend; %RunBY(data=MyFullData, by=Gender)
If MyMacro is written to accept data set options in its data= parameter, the following could also work:
%macro code(); %MyMacro( data=subset(where=(&_BY1 = &_LVL1)) ) %mend; %RunBY(data=MyData, by=Gender)
If there are multiple by= variables, for example by=gender race, then the WHERE statement would include a second condition as below, and so on, for any number of by= variables.
where &_BY1 = &_LVL1 and &_BY2 = &_LVL2;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.