Using character values on a macro %DO loop

/ Published in: SAS
Save to your folder(s)

This sample contains 2 macro techniques for iterating through character values on a macro %DO loop.

Copy this code and paste it in your HTML
  1. /* This macro will allow you to step through the lowercase letters of */
  2. /* the alphabet on a %DO loop. For uppercase letters, edit the first */
  3. /* %LET statement to include them as well. */
  5. %macro iterm(beg,end);
  6. %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;
  7. %let start=%sysfunc(indexc(%sysfunc(compress(&lst)),&beg));
  8. %let finish=%sysfunc(indexc(%sysfunc(compress(&lst)),&end));
  9. %do i = &start %to &finish;
  10. %put %scan(&lst,&i);
  11. %end;
  12. %mend;
  14. /* Just pass in starting and ending value */
  15. %iterm(a,e)
  18. /** An alternative to the above example using the RANK and BYTE function **/
  19. %macro iterm(beg,end);
  20. %do i = %sysfunc(rank(&beg)) %to %sysfunc(rank(&end));
  21. %put %sysfunc(byte(&i));
  22. %end;
  23. %mend;
  24. /* Just pass in starting and ending value */
  25. %iterm(a,e)


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.