Posted By

webonomic on 06/11/13


Tagged

list character loop macro


Versions (?)

Using character values on a macro %DO loop


 / Published in: SAS
 

URL: http://support.sas.com/kb/25/961.html

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

  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. */
  4.  
  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;
  13.  
  14. /* Just pass in starting and ending value */
  15. %iterm(a,e)
  16.  
  17.  
  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  

You need to login to post a comment.