Sample 26155: Loop through a nonsequential list of values with a macro DO loop
In the macro language, there is not an equivalent to the following DATA step code:
do i=2,3,5,7,11,13,17;
In order to do this within the macro language, the individual values are passed to the macro and each value is read using %QSCAN. The COUNTW function is used to count the number of words in the string using the comma as the delimiter.
Click on the Full Code and Output tabs to see the code and output.
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.
%macro loop(values);
/* Count the number of values in the string */
%let count=%sysfunc(countw(&values));
/* Loop through the total number of values */
%do i = 1 %to &count;
%let value=%qscan(&values,&i,%str(,));
%put &value;
%end;
%mend;
/* %STR is used to mask the commas from the macro compiler when */
/* the macro %LOOP is called. */
%loop(%str(2,3,5,7,11,13,17))
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
2
3
5
7
11
13
17
Use macro logic to loop through a nonsequential list of values.
Type: | Sample |
Topic: | SAS Reference ==> Macro
|
Date Modified: | 2011-05-20 16:36:27 |
Date Created: | 2007-04-28 03:02:51 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |