Using character values on a macro %DO loop


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

3 examples


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. */
  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)
  26.  
  27.  
  28. %macro loop(values);
  29. * Count the number of values in the string;
  30. %let count=%sysfunc(countw(&values));
  31. * Loop through the total number of values;
  32. %do i = 1 %to &count;
  33. %let value=%qscan(&values,&i,%str(,));
  34. %put &value;
  35. %end;
  36. %mend;
  37. * %STR is used to mask the commas from the macro compiler when;
  38. * the macro %LOOP is called.;
  39. %loop(%str(2,3,5,7,11,13,17))

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.