<?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: do loop from observations of a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507350#M136158</link>
    <description>&lt;P&gt;Probably easiest:&amp;nbsp; CALL EXECUTE.&amp;nbsp; The basic structure:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;call execute('proc sql;');&lt;/P&gt;
&lt;P&gt;do until (done);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set itemlist end=done;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;call execute('specifics of the query for an ITEM go here');&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;call execute('quit;');&lt;/P&gt;
&lt;P&gt;stop;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You haven't provided any details of the query, so it's not really possible to program the interior CALL EXECUTE yet.&lt;/P&gt;</description>
    <pubDate>Wed, 24 Oct 2018 22:46:38 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-10-24T22:46:38Z</dc:date>
    <item>
      <title>do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507346#M136155</link>
      <description>&lt;P&gt;I want to run a loop SAS code, let's say I have a datatable called itemlist, with only 1 column and several observations as below:&lt;/P&gt;&lt;P&gt;Item&lt;/P&gt;&lt;P&gt;001&lt;/P&gt;&lt;P&gt;003&lt;/P&gt;&lt;P&gt;007&lt;/P&gt;&lt;P&gt;025&lt;/P&gt;&lt;P&gt;079&lt;/P&gt;&lt;P&gt;102&lt;/P&gt;&lt;P&gt;and so on... number of observations is not set yet.&lt;/P&gt;&lt;P&gt;I want to run a query where item = (items in itemlist, like 001, 003 ...) one by one.&lt;/P&gt;&lt;P&gt;So how can I build the loop for this?&lt;/P&gt;&lt;P&gt;Tried&lt;/P&gt;&lt;PRE class="sas"&gt;&lt;SPAN&gt;%macro item_loop;&lt;BR /&gt;%local&lt;/SPAN&gt; i item;
&lt;SPAN&gt;%let&lt;/SPAN&gt; i=&lt;SPAN&gt;1&lt;/SPAN&gt;;
&lt;SPAN&gt;%do&lt;/SPAN&gt; &lt;SPAN&gt;%while&lt;/SPAN&gt; &lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;%scan&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;&amp;amp;itemlist&lt;/SPAN&gt;, &lt;SPAN&gt;&amp;amp;i&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt; ne &lt;SPAN&gt;)&lt;/SPAN&gt;;
   &lt;SPAN&gt;%let&lt;/SPAN&gt; item = &lt;SPAN&gt;%scan&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;&amp;amp;itemlist&lt;/SPAN&gt;, &lt;SPAN&gt;&amp;amp;i&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;;
   some code // &lt;SPAN&gt;DO query where item = number in dataset one by one and other operations;&lt;/SPAN&gt;
   &lt;SPAN&gt;%let&lt;/SPAN&gt; i = &lt;SPAN&gt;%eval&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;&amp;amp;i&lt;/SPAN&gt; + &lt;SPAN&gt;1&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;;
&lt;SPAN&gt;%end&lt;/SPAN&gt;;&lt;BR /&gt;%mend;&lt;BR /&gt;%item_loop;&lt;/PRE&gt;&lt;P&gt;It seems not work, can anyone help me on correcting my mistake or give me other methods to make the loop work?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Wed, 24 Oct 2018 22:33:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507346#M136155</guid>
      <dc:creator>leonzheng</dc:creator>
      <dc:date>2018-10-24T22:33:00Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507350#M136158</link>
      <description>&lt;P&gt;Probably easiest:&amp;nbsp; CALL EXECUTE.&amp;nbsp; The basic structure:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;call execute('proc sql;');&lt;/P&gt;
&lt;P&gt;do until (done);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set itemlist end=done;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;call execute('specifics of the query for an ITEM go here');&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;call execute('quit;');&lt;/P&gt;
&lt;P&gt;stop;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You haven't provided any details of the query, so it's not really possible to program the interior CALL EXECUTE yet.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Oct 2018 22:46:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507350#M136158</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-10-24T22:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507353#M136159</link>
      <description>&lt;P&gt;This looks like well-written code, and it should work&amp;nbsp;with the right data.&lt;/P&gt;
&lt;P&gt;Try this to see if it makes a difference:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro item_loop;
%local i item;
%let i=1;
%do %while (%length(%scan(&amp;amp;itemlist, &amp;amp;i,%str( ))));
   %let item = %scan(&amp;amp;itemlist, &amp;amp;i,%str( ));
   some code // DO query where item = number in dataset one by one and other operations;
   %let i = %eval(&amp;amp;i + 1);
%end;
%mend;
%item_loop;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Oct 2018 22:57:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507353#M136159</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-10-24T22:57:31Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507354#M136160</link>
      <description>Thanks, my operation after query is quite complicated, so I am not sure if call execute will work</description>
      <pubDate>Wed, 24 Oct 2018 23:02:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507354#M136160</guid>
      <dc:creator>leonzheng</dc:creator>
      <dc:date>2018-10-24T23:02:58Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507358#M136161</link>
      <description>It still not working, maybe my query is not right, should I write&lt;BR /&gt;where item = &amp;amp;item.&lt;BR /&gt;is this correct?</description>
      <pubDate>Wed, 24 Oct 2018 23:10:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507358#M136161</guid>
      <dc:creator>leonzheng</dc:creator>
      <dc:date>2018-10-24T23:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507359#M136162</link>
      <description>&lt;P&gt;We don't know what code you run.&lt;/P&gt;
&lt;P&gt;For a string,&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;SPAN&gt;&lt;FONT face="courier new,courier"&gt;where&amp;nbsp;ITEM&amp;nbsp;=&amp;nbsp;"&amp;amp;item"&amp;nbsp;&lt;/FONT&gt;&amp;nbsp;should&amp;nbsp;work.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Oct 2018 23:15:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507359#M136162</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-10-24T23:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507361#M136164</link>
      <description>&lt;P&gt;My dataset is numeric, so I still use "where item = &amp;amp;item. " in my query.&lt;/P&gt;&lt;P&gt;The error code is like this:&lt;/P&gt;&lt;P&gt;ERROR: CLI describe error: [IBM][CLI Driver][DB2/AIX64] SQL0007N The statement was not processed&lt;BR /&gt;because a character that is not supported in SQL statements was included in the SQL&lt;BR /&gt;statement. Invalid character: "&amp;amp;". Text preceding the invalid character: "WHERE&lt;BR /&gt;ITEM&amp;nbsp;=". SQLSTATE=42601&lt;/P&gt;</description>
      <pubDate>Wed, 24 Oct 2018 23:25:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507361#M136164</guid>
      <dc:creator>leonzheng</dc:creator>
      <dc:date>2018-10-24T23:25:42Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507362#M136165</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/184299"&gt;@leonzheng&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;It still not working, maybe my query is not right, should I write&lt;BR /&gt;where item = &amp;amp;item.&lt;BR /&gt;is this correct?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Is item a character or numeric variable?&lt;/P&gt;
&lt;P&gt;If numeric that should work assuming all the &amp;amp;item macro variable values work as numeric values as is.&lt;/P&gt;
&lt;P&gt;If character you need quotes just like any other character comparison with a literal string only macro's require the double quotes.&lt;/P&gt;
&lt;P&gt;where item= "&amp;amp;item";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if the &amp;amp;item variable contains quotes or leading spaces you might have issues.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run your code with OPTIONS MPRINT; to see what is actually generated by the macro. Turn off when not needed with OPTION NOMPRINT.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Oct 2018 23:32:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507362#M136165</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-24T23:32:55Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507363#M136166</link>
      <description>The macro actually hasn't done anything yet, from the log, it failed on the first query, error message as I replied above, thx</description>
      <pubDate>Wed, 24 Oct 2018 23:51:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507363#M136166</guid>
      <dc:creator>leonzheng</dc:creator>
      <dc:date>2018-10-24T23:51:25Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507364#M136167</link>
      <description>Is there an option to use the table itself in a sub query or is the list too long to create a single macro variable with all the values?  Adding another macro loop to a complex problem seems like it’s adding complexity.</description>
      <pubDate>Thu, 25 Oct 2018 00:15:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507364#M136167</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-25T00:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507365#M136168</link>
      <description>SQL subquery:&lt;BR /&gt;Select * from x where item in (select item from item_list_table); &lt;BR /&gt;&lt;BR /&gt;Macro variable (if less than 64K characters)&lt;BR /&gt;Proc sql noprint;&lt;BR /&gt;Select quote(item) into :item_list separated by “, “ from item_list_table;&lt;BR /&gt;Quit;&lt;BR /&gt;&lt;BR /&gt;Then use as&lt;BR /&gt;&lt;BR /&gt;Where item in (&amp;amp;item_list.);</description>
      <pubDate>Thu, 25 Oct 2018 00:20:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507365#M136168</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-25T00:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507372#M136173</link>
      <description>&lt;P&gt;&lt;EM&gt;Invalid character: "&amp;amp;".&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like you are passing the&amp;nbsp;SQL&amp;nbsp;as a single-quoted string. Macro variables are not recognised there. Use double quotes to have macro language&amp;nbsp;elements evaluated.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Oct 2018 01:16:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507372#M136173</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-10-25T01:16:51Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507374#M136175</link>
      <description>&lt;P&gt;Here's an alternative approach that is well-suited for debugging complicated queries.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;set itemlist (obs=1);&lt;/P&gt;
&lt;P&gt;file 'some text file';&lt;/P&gt;
&lt;P&gt;put .............&amp;nbsp; /* use PUT statements to write out the query to a text file */ ;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you get to examine the contents of the text file, adjust the PUT statements until the query looks like it should work properly.&amp;nbsp; Once it looks correct, get rid of OBS=1 and run with the full list of items.&amp;nbsp; Then you can execute using:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;%include 'some text file';&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Oct 2018 01:52:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507374#M136175</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-10-25T01:52:00Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507397#M136194</link>
      <description>&lt;P&gt;Why do you think that will help you?&lt;/P&gt;
&lt;P&gt;What is it that you want to do for each value in your list?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why can't you just do it for all of the items in your list at the same time?&lt;/P&gt;</description>
      <pubDate>Thu, 25 Oct 2018 04:15:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507397#M136194</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-10-25T04:15:33Z</dc:date>
    </item>
    <item>
      <title>Re: do loop from observations of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507414#M136206</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/184299"&gt;@leonzheng&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks, my operation after query is quite complicated, so I am not sure if call execute will work&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It's actually very simple.&lt;/P&gt;
&lt;P&gt;Assume your code needs a macro variable &amp;amp;item to work for a single instance.&lt;/P&gt;
&lt;P&gt;Wrap it into a macro that handles one instance:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro your_mac(item);

/* lots of complicated code here */

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then call that macro off of your dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set have;
call execute('%nrstr(%your_mac(' !! strip(item) !! '));');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The single quotes and the %nrstr() prevent premature execution of macro logic when the code is pushed into the execution chain.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Oct 2018 07:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-from-observations-of-a-dataset/m-p/507414#M136206</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-10-25T07:30:26Z</dc:date>
    </item>
  </channel>
</rss>

