<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How pass a numeric range or list into a macro and then create a do loop to iterate over it in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-pass-a-numeric-range-or-list-into-a-macro-and-then-create-a/m-p/642091#M191508</link>
    <description>&lt;P&gt;Him I would like to&amp;nbsp; be able to pass a numeric range such as 2013:2017 into a macro and then iterate over it through it with a do loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It would look something like what I have below.&amp;nbsp; I have wrote ....... where I am not sure of what to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%some_macro(year);
   %do .........    ;

      %let year_i = ............    ; 

      %put year_I;
   %end;

%mend some_macro;

&lt;/PRE&gt;
&lt;P&gt;I would like it to work with a range.&lt;/P&gt;
&lt;P&gt;%some_macro(2013:2017);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I also would like it to work with a single number.&lt;/P&gt;
&lt;P&gt;%some_macro(2013);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or with a list of numbers.&lt;/P&gt;
&lt;P&gt;%some_macro(2013 2015 2018)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure if this is possible or practical SAS, but i thought it would be interesting to try.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-Bill&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 22 Apr 2020 21:29:37 GMT</pubDate>
    <dc:creator>whs278</dc:creator>
    <dc:date>2020-04-22T21:29:37Z</dc:date>
    <item>
      <title>How pass a numeric range or list into a macro and then create a do loop to iterate over it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-pass-a-numeric-range-or-list-into-a-macro-and-then-create-a/m-p/642091#M191508</link>
      <description>&lt;P&gt;Him I would like to&amp;nbsp; be able to pass a numeric range such as 2013:2017 into a macro and then iterate over it through it with a do loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It would look something like what I have below.&amp;nbsp; I have wrote ....... where I am not sure of what to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%some_macro(year);
   %do .........    ;

      %let year_i = ............    ; 

      %put year_I;
   %end;

%mend some_macro;

&lt;/PRE&gt;
&lt;P&gt;I would like it to work with a range.&lt;/P&gt;
&lt;P&gt;%some_macro(2013:2017);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I also would like it to work with a single number.&lt;/P&gt;
&lt;P&gt;%some_macro(2013);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or with a list of numbers.&lt;/P&gt;
&lt;P&gt;%some_macro(2013 2015 2018)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure if this is possible or practical SAS, but i thought it would be interesting to try.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-Bill&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 21:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-pass-a-numeric-range-or-list-into-a-macro-and-then-create-a/m-p/642091#M191508</guid>
      <dc:creator>whs278</dc:creator>
      <dc:date>2020-04-22T21:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: How pass a numeric range or list into a macro and then create a do loop to iterate over it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-pass-a-numeric-range-or-list-into-a-macro-and-then-create-a/m-p/642101#M191515</link>
      <description>&lt;PRE&gt;%macro dummy (parm= );
%do i=1 %to %sysfunc(countw(&amp;amp;parm));
   %let value = %scan(&amp;amp;parm, &amp;amp;i);
   %put Value for &amp;amp;i. is &amp;amp;value.;
%end;
%mend;

%dummy(parm= a b 234 q)&lt;/PRE&gt;
&lt;P&gt;First thing to remember: MACRO VARIABLES ARE ALWAYS TEXT.&lt;/P&gt;
&lt;P&gt;If you really want a "range" instead of an explicit list then use a separate parameter for the lower and upper bound (makes it clear what you are doing which could be masked by using one parameter with two values).&lt;/P&gt;
&lt;PRE&gt;%macro dummy2 (lower= , upper= );
%do i=&amp;amp;lower. %to &amp;amp;upper. ;
   %put Value is &amp;amp;i.; 
%end;
%mend;

%dummy2(lower= 123, upper=127)&lt;/PRE&gt;
&lt;P&gt;If you go this route I strongly suggest doing some error check coding to insure that lower is less than upper AND that the values are integers. Macro do loops aren't going to perform well with decimal values.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 21:52:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-pass-a-numeric-range-or-list-into-a-macro-and-then-create-a/m-p/642101#M191515</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-22T21:52:14Z</dc:date>
    </item>
    <item>
      <title>Re: How pass a numeric range or list into a macro and then create a do loop to iterate over it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-pass-a-numeric-range-or-list-into-a-macro-and-then-create-a/m-p/642127#M191531</link>
      <description>&lt;P&gt;So you want to take space delimited list of values. Where each value is either a single number or a pair of numbers separated by a colon.&lt;/P&gt;
&lt;P&gt;Scan the list. Then scan/parse the items in the list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro some_macro(yearlist);
%local index token year;
%do index=1 %to %sysfunc(countw(&amp;amp;yearlist,%str( )));
  %let token=%scan(&amp;amp;yearlist,&amp;amp;index,%str( ));
  %do year=%scan(&amp;amp;token,1,:) %to %scan(&amp;amp;token,-1,:) ;
    %put &amp;amp;=index &amp;amp;=token &amp;amp;=year ;
  %end;
  %put ;
%end;
%mend ;

%some_macro(2000  2010:2013  2015);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;INDEX=1 TOKEN=2000 YEAR=2000

INDEX=2 TOKEN=2010:2013 YEAR=2010
INDEX=2 TOKEN=2010:2013 YEAR=2011
INDEX=2 TOKEN=2010:2013 YEAR=2012
INDEX=2 TOKEN=2010:2013 YEAR=2013

INDEX=3 TOKEN=2015 YEAR=2015&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 23:53:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-pass-a-numeric-range-or-list-into-a-macro-and-then-create-a/m-p/642127#M191531</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-22T23:53:10Z</dc:date>
    </item>
  </channel>
</rss>

