Previous Page | Next Page

Macro Statements

%DO %WHILE Statement

Executes a section of a macro repetitively while a condition is true.
Type: Macro statement
Restriction: Allowed in macro definitions only
See also: %END Statement

Removing Markup Tags from a Title


%DO %WHILE (expression);
text and macro program statements


can be any macro expression that resolves to a logical value. The macro processor evaluates the expression at the top of each iteration. The expression is true if it is an integer other than zero. The expression is false if it has a value of zero. If the expression resolves to a null value or to a value containing nonnumeric characters, the macro processor issues an error message.

These examples illustrate expressions for the %DO %WHILE statement:

  • %do %while(&a<&b);

  • %do %while(%length(&name)>20);


The %DO %WHILE statement tests the condition at the top of the loop. If the condition is false the first time the macro processor tests it, the %DO %WHILE loop does not iterate.


Example 1: Removing Markup Tags from a Title

This example demonstrates using the %DO %WHILE to strip markup (SGML) tags from text to create a TITLE statement:

%macro untag(title);
      %let stbk=%str(<);
      %let etbk=%str(>);
      /* Do loop while tags exist  */
   %do %while (%index(&title,&stbk)>0) ;
      %let pretag=;
      %let posttag=;
      %let pos_et=%index(&title,&etbk);
      %let len_ti=%length(&title);
          /* Is < first character? */
      %if (%qsubstr(&title,1,1)=&stbk) %then %do;
         %if (&pos_et ne &len_ti) %then
            %let posttag=%qsubstr(&title,&pos_et+1);
      %else %do;
         %let pretag=%qsubstr(&title,1,(%index(&title,&stbk)-1));
            /* More characters beyond end of tag (>) ? */
         %if (&pos_et ne &len_ti) %then
            %let posttag=%qsubstr(&title,&pos_et+1);
         /* Build title with text before and after tag */
      %let title=&pretag&posttag;
title "&title";
%mend untag;

You can invoke the macro UNTAG as

%untag(<title>Total <emph>Overdue </emph>Accounts</title>)

The macro then generates this TITLE statement:

TITLE "Total Overdue Accounts";

If the title text contained special characters such as commas, you could invoke it with the %NRSTR function.

   %nrstr(<title>Accounts: Baltimore, Chicago, and Los Angeles</title>))

Previous Page | Next Page | Top of Page