<?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 Re: How can access a macro do loop (or do loop only) for a list of items ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/302115#M64025</link>
    <description>&lt;P&gt;Thank you for Reeza and ballardw again for your correct solution.&lt;/P&gt;
&lt;P&gt;I also contribute my little discovery from this code. My macro goes through an oracle sql which some conditions such as&lt;/P&gt;
&lt;P&gt;proc sql ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; select * from table;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;code 1: &amp;nbsp; where var BETWEEN &amp;amp;start_date. and &amp;amp;end_date. &amp;nbsp;&amp;nbsp; (macro resolved, sql error)&lt;/P&gt;
&lt;P&gt;code 2:&amp;nbsp; where var BETWEEN "&amp;amp;start_date." and "&amp;amp;end_date." (macro resolved, sql error)&lt;/P&gt;
&lt;P&gt;code 3:&amp;nbsp; where var between '&amp;amp;start_date.' and '&amp;amp;end_date.'&amp;nbsp; (macro unresolved, sql error)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;code 4: where var between &amp;nbsp;%nrbquote('&amp;amp;start_date.') and&amp;nbsp; %nrbquote('&amp;amp;end_date.')&amp;nbsp; (macro resolved, sql correct)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have searching for long time how to resolve the macro within quote '&amp;amp;macro_varibale'. With macro function %nrbquote , it worked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for all contributions on this topic. Good job SAS community.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;WT196838&lt;/P&gt;</description>
    <pubDate>Mon, 03 Oct 2016 17:44:51 GMT</pubDate>
    <dc:creator>wtien196838</dc:creator>
    <dc:date>2016-10-03T17:44:51Z</dc:date>
    <item>
      <title>How can access a macro do loop (or do loop only) for a list of items ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/301779#M63921</link>
      <description>&lt;P&gt;Hi SAS Community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a list of item ids such as:&lt;/P&gt;
&lt;P&gt;%let item_id = 1234 2456 3567 4567 5674 8765 ;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I want to put in a do loop like:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%DO i = &amp;amp;item_id ;&lt;/P&gt;
&lt;P&gt;/* call one macro and input item id */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %macro_call(&amp;amp;i)&lt;/P&gt;
&lt;P&gt;%END ;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;The %macro_call is working with individual item id such as&lt;/P&gt;
&lt;P&gt;%macro_call(1234)&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Thank you for sharing your knowledge.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;WT1968&lt;/P&gt;</description>
      <pubDate>Fri, 30 Sep 2016 15:44:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/301779#M63921</guid>
      <dc:creator>wtien196838</dc:creator>
      <dc:date>2016-09-30T15:44:39Z</dc:date>
    </item>
    <item>
      <title>Re: How can access a macro do loop (or do loop only) for a list of items ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/301783#M63923</link>
      <description>&lt;P&gt;Simple answer, as with all these things, use Base SAS:&lt;/P&gt;
&lt;PRE&gt;data _null_;
  do i=1 to countw("&amp;amp;ITEM_ID."," ");
    call execute(cats('%macro (',scan("&amp;amp;ITEM_ID.",i," "),");"));
  end;
run;&lt;/PRE&gt;
&lt;P&gt;In fact, as with any question stating "Macro" use Base SAS, macro is only there to generate code, Base SAS has all the functionality, and all the datatypes. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Sep 2016 15:49:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/301783#M63923</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-09-30T15:49:06Z</dc:date>
    </item>
    <item>
      <title>Re: How can access a macro do loop (or do loop only) for a list of items ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/301786#M63925</link>
      <description>&lt;P&gt;One way IF your list &lt;STRONG&gt;never&lt;/STRONG&gt; contains commas or special characters like % &amp;amp; and such.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro control (list);
   %local list;
   %do i=1 %to %sysfunc(countw(&amp;amp;list));
      %let key = %scan(&amp;amp;list,&amp;amp;i);

       %macro_call(&amp;amp;key);
   %end;
%mend;

%control(1234 2456 3567 4567 5674 8765);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Another would be to have the values as one record per value in a data set and use a data step with Call Execute.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Sep 2016 15:53:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/301786#M63925</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-09-30T15:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: How can access a macro do loop (or do loop only) for a list of items ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/301814#M63935</link>
      <description>&lt;P&gt;See the examples in the Appendix&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Sep 2016 18:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/301814#M63935</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-09-30T18:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: How can access a macro do loop (or do loop only) for a list of items ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/301855#M63940</link>
      <description>&lt;P&gt;Thanh you for Ballardw and Reeza quick response.&lt;/P&gt;
&lt;P&gt;Both solutions are resolved my working problem.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I have an&amp;nbsp;advanced question based on previous question.&lt;/P&gt;
&lt;P&gt;Now I have:&lt;/P&gt;
&lt;P&gt;%let item_id = 1234 2456 3567 4567 5674 &amp;nbsp;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let start_date = '03-SEP-16' &amp;nbsp;'10-SEP-16' '17-SEP-16' '24-SEP-16' '01-OCT16' ;&lt;/P&gt;
&lt;P&gt;%let end_date = '09-SEP-16' '16-SEP-16' '23-SEP-16' '30-SEP-16' '07-OCT-16';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to put in a do loop such that&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DO &amp;nbsp;i = &amp;amp;item_id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%macro_call(item_id,start_date,end_date)&lt;/P&gt;
&lt;P&gt;End;&lt;/P&gt;
&lt;P&gt;Macro call ask for each item associate date range. Example: item_id=1234 must link to "03-SEP-16' and '09-SEP-16' etc..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is very helpful to get a solution for this problem.&lt;/P&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;WT196838&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Oct 2016 01:07:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/301855#M63940</guid>
      <dc:creator>wtien196838</dc:creator>
      <dc:date>2016-10-01T01:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: How can access a macro do loop (or do loop only) for a list of items ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/301858#M63942</link>
      <description>&lt;P&gt;I would suggest instead putting the values in a data set and then using a call execute to call your macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise you can use scan() or %scan() to isolate each item in turn and pass it to the macro.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Oct 2016 01:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/301858#M63942</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-01T01:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: How can access a macro do loop (or do loop only) for a list of items ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/302092#M64011</link>
      <description>&lt;P&gt;As a general comment: placing quote marks into macro variables seldom yields clean code. I would say that it usually is easier to leave the macro parameter as plain text and then if, and only if, you are referencing the value in code that requires the value to place it DOUBLE quotes for use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your end and start date parameters exactly match the number of ITEMS then it is a trivial programming exercise to extend the provided code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro control (list=, start_date=, end_date=);
   %local list;
   %do i=1 %to %sysfunc(countw(&amp;amp;list));
      %let key = %scan(&amp;amp;list,&amp;amp;i);
      %let key2= %scan(&amp;amp;start_date,&amp;amp;i); /* you already had the pattern for this statement*/
      %let key3= %scan(&amp;amp;start_date,&amp;amp;i);

       %macro_call(&amp;amp;key,&amp;amp;key2,&amp;amp;key3);
   %end;
%mend;

%control(list=1234 2456 3567 4567 5674 8765,
         start_date= 03-SEP-16  10-SEP-16 17-SEP-16 24-SEP-16 01-OCT16,
         end_date  = 09-SEP-16 16-SEP-16 23-SEP-16 30-SEP-16 07-OCT-16 );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I strongly recommend using keyword parameters if there are many parameters involved and especially if there are longish lists of items.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2016 16:15:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/302092#M64011</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-10-03T16:15:42Z</dc:date>
    </item>
    <item>
      <title>Re: How can access a macro do loop (or do loop only) for a list of items ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/302115#M64025</link>
      <description>&lt;P&gt;Thank you for Reeza and ballardw again for your correct solution.&lt;/P&gt;
&lt;P&gt;I also contribute my little discovery from this code. My macro goes through an oracle sql which some conditions such as&lt;/P&gt;
&lt;P&gt;proc sql ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; select * from table;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;code 1: &amp;nbsp; where var BETWEEN &amp;amp;start_date. and &amp;amp;end_date. &amp;nbsp;&amp;nbsp; (macro resolved, sql error)&lt;/P&gt;
&lt;P&gt;code 2:&amp;nbsp; where var BETWEEN "&amp;amp;start_date." and "&amp;amp;end_date." (macro resolved, sql error)&lt;/P&gt;
&lt;P&gt;code 3:&amp;nbsp; where var between '&amp;amp;start_date.' and '&amp;amp;end_date.'&amp;nbsp; (macro unresolved, sql error)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;code 4: where var between &amp;nbsp;%nrbquote('&amp;amp;start_date.') and&amp;nbsp; %nrbquote('&amp;amp;end_date.')&amp;nbsp; (macro resolved, sql correct)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have searching for long time how to resolve the macro within quote '&amp;amp;macro_varibale'. With macro function %nrbquote , it worked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for all contributions on this topic. Good job SAS community.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;WT196838&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2016 17:44:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-for-a-list-of/m-p/302115#M64025</guid>
      <dc:creator>wtien196838</dc:creator>
      <dc:date>2016-10-03T17:44:51Z</dc:date>
    </item>
  </channel>
</rss>

