Sample 25961: Using character values on a macro %DO loop
The sample code on the Full Code tab contains two macro techniques for iterating through character values in a macro %DO loop.
The sample below can be used if using a DATA step DO loop:
Sample #35010, Iterating through character values in a DATA Step DO loop
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.
/* This macro will allow you to step through the lowercase letters of */
/* the alphabet on a %DO loop. For uppercase letters, edit the first */
/* %LET statement to include them as well. */
%macro iterm(beg,end);
%let lst=a b c d e f g h i j k l m n o p q r s t u v w x y z;
%let start=%sysfunc(indexc(%sysfunc(compress(&lst)),&beg));
%let finish=%sysfunc(indexc(%sysfunc(compress(&lst)),&end));
%do i = &start %to &finish;
%put %scan(&lst,&i);
%end;
%mend;
/* Just pass in starting and ending value */
%iterm(a,e)
/** An alternative to the above example using the RANK and BYTE function **/
%macro iterm(beg,end);
%do i = %sysfunc(rank(&beg)) %to %sysfunc(rank(&end));
%put %sysfunc(byte(&i));
%end;
%mend;
/* Just pass in starting and ending value */
%iterm(a,e)
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.
Output written to log
a
b
c
d
e
This sample code illustrates how to use characters on a macro %DO loop such as going from a to m.
Type: | Sample |
Topic: | SAS Reference ==> Macro
|
Date Modified: | 2009-03-10 10:00:55 |
Date Created: | 2005-08-03 14:27:14 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |