<?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 macro do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/481811#M124712</link>
    <description>&lt;P&gt;Hi Folks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have following code,&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test (num,country);
	%do i = 1 %to 3;
proc sql outobs=&amp;amp;num;
create table combined as
select distinct contractID, sourcesystemID, countryid, stage
from ifrs._09_abc
where countryid = &amp;amp;country. and stage = &amp;amp;i;&lt;BR /&gt;
%end;

%mend;

%test(10,1);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When it ran it produces the table with only stage 3. I think the loop replaces the values. I want to have all three stages in the output table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 27 Jul 2018 10:40:13 GMT</pubDate>
    <dc:creator>Myurathan</dc:creator>
    <dc:date>2018-07-27T10:40:13Z</dc:date>
    <item>
      <title>macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/481811#M124712</link>
      <description>&lt;P&gt;Hi Folks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have following code,&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test (num,country);
	%do i = 1 %to 3;
proc sql outobs=&amp;amp;num;
create table combined as
select distinct contractID, sourcesystemID, countryid, stage
from ifrs._09_abc
where countryid = &amp;amp;country. and stage = &amp;amp;i;&lt;BR /&gt;
%end;

%mend;

%test(10,1);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When it ran it produces the table with only stage 3. I think the loop replaces the values. I want to have all three stages in the output table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 10:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/481811#M124712</guid>
      <dc:creator>Myurathan</dc:creator>
      <dc:date>2018-07-27T10:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/481814#M124714</link>
      <description>&lt;P&gt;You always use the same name for the output table, so the second iteration overwrites the result from the first, and the third the second.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 10:51:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/481814#M124714</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-07-27T10:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/481815#M124715</link>
      <description>&lt;P&gt;Then you need to append the proc sql output to a dataset each loop, i.e.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
  stop;
run;

%macro test (num,country);
  %do i = 1 %to 3;
    proc sql outobs=&amp;amp;num;
      create table combined as
      select distinct contractID, 
                      sourcesystemID, 
                      countryid, 
                      stage
      from            ifrs._09_abc
       where          countryid =&amp;amp;country. 
             and      stage=&amp;amp;i;
    quit;
    data want;
      set want combined;
    run;
  %end;
%mend;

%test(10,1);&lt;/PRE&gt;
&lt;P&gt;So each iteration the output combined is added to want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That being said, I don't see any point for this code at all, simply:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let country=1;&lt;BR /&gt;%let stage=3;

proc sort data=ifrs._09_abc out=want (keep=contractid...) nodupkey;&lt;BR /&gt;  by contractid...;&lt;BR /&gt;  where countryid=&amp;amp;country. and stage &amp;lt;= &amp;amp;stage.;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;Note add your other variables in where the ... is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 10:55:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/481815#M124715</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-07-27T10:55:19Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/481816#M124716</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the first iteration &amp;nbsp;you are creating a table Combined for Stage1 data.&lt;/P&gt;
&lt;P&gt;In the next iteration you are creating a table for stage2 data with the same table name Combined which overrides the first table.&lt;/P&gt;
&lt;P&gt;The third iteration creates a table Combined which overrides the table Combined which has Stage2 data.&lt;/P&gt;
&lt;P&gt;So you are left with Stage3 data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to create 3 tables for 3 values you can use&amp;nbsp;&lt;BR /&gt;create table Combined&lt;STRONG&gt;&amp;amp;i&amp;nbsp;&lt;/STRONG&gt;as ...... and append the tables&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro test (num,country);
%do i = 1 %to 3;

proc sql outobs=&amp;amp;num;
create table combined&amp;amp;i as
select distinct contractID, sourcesystemID, countryid, stage
from ifrs._09_abc
where countryid = &amp;amp;country. and stage = &amp;amp;i;
quit;

proc append base= combined data = combined&amp;amp;i;run;

%end;

%mend;

%test(10,1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 11:01:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/481816#M124716</guid>
      <dc:creator>MadhuKorni</dc:creator>
      <dc:date>2018-07-27T11:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/481819#M124717</link>
      <description>&lt;P&gt;Thanks RW9.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need this code as I want 10 rows of output for each stages. So each country will have 30 rows of data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 11:01:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/481819#M124717</guid>
      <dc:creator>Myurathan</dc:creator>
      <dc:date>2018-07-27T11:01:55Z</dc:date>
    </item>
  </channel>
</rss>

