<?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 to do loop over a loop in SAS Macro? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-loop-over-a-loop-in-SAS-Macro/m-p/732817#M228356</link>
    <description>&lt;P&gt;You can find this documented under below links.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n0vl7e3du5f7cjn1g4ajozqxe6b6.htm&amp;amp;locale=en"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n0vl7e3du5f7cjn1g4ajozqxe6b6.htm&amp;amp;locale=en&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n0700fspmubii5n1vecutttwz93n.htm&amp;amp;locale=en"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n0700fspmubii5n1vecutttwz93n.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each iteration for resolving an ampersand will consume a period. That's why you sometimes need several periods when dealing with multiple ampersands.&lt;/P&gt;</description>
    <pubDate>Sun, 11 Apr 2021 06:37:14 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2021-04-11T06:37:14Z</dc:date>
    <item>
      <title>How to do loop over a loop in SAS Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-loop-over-a-loop-in-SAS-Macro/m-p/732806#M228348</link>
      <description>&lt;P&gt;Hi, greetings,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you experts could help me with this coding, I would really appreciate it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have seven different datasets, whose names are USA, GBR, JPN,&amp;nbsp;Ind_73, Ind_36,&amp;nbsp;Ind_28, and Ind_60.&amp;nbsp;&lt;/P&gt;&lt;P&gt;(First three are countries, last four are industries).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By using SAS Macro, I am trying to split each dataset into 18 sub-datasets (say, annually, from 2000~2017).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I tried to put the names of the seven datasets into var1=, var2, ... var7,&amp;nbsp;&lt;/P&gt;&lt;P&gt;and using double do-loop (by year within by dataset) I tried to achieve this and export them into EXCEL.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My codes run as follows:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%put _global_;&lt;BR /&gt;%LET var1=USA; %LET var2=JPN; %LET var3=GBR; %LET var4=Ind_73; %LET var5=Ind_36;&lt;BR /&gt;%LET var6=Ind_28; %LET var7=Ind_60;&lt;BR /&gt;%MACRO Divide_year;&lt;BR /&gt;%DO i=1 %TO 7;&lt;BR /&gt;%DO j=2000 %TO 2017;&lt;BR /&gt;DATA &amp;amp;var&amp;amp;i&amp;amp;j; SET &amp;amp;var&amp;amp;i; IF Annc_fyear=&amp;amp;j; KEEP Source Target FIC SIC_2D; run;&lt;BR /&gt;PROC EXPORT data=&amp;amp;var&amp;amp;i&amp;amp;j dbms=xlsx&lt;BR /&gt;outfile="C:\Users\kchoi9\Desktop\Network and Finance\Data\Three_countries_four_industries\&amp;amp;var&amp;amp;i&amp;amp;j.xlsx"&lt;BR /&gt;replace; RUN;&lt;BR /&gt;%END; %END; %MEND Divide_year; %Divide_year;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I get a whole bunch of errors, first and foremost like the following:&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: File WORK.VAR1.DATA does not exist.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How should I solve this? Please help.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;KS -,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Apr 2021 23:54:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-loop-over-a-loop-in-SAS-Macro/m-p/732806#M228348</guid>
      <dc:creator>KS99</dc:creator>
      <dc:date>2021-04-10T23:54:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to do loop over a loop in SAS Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-loop-over-a-loop-in-SAS-Macro/m-p/732807#M228349</link>
      <description>&lt;P&gt;There will be warning messages in your log telling you that macro variables don't exists. The reason is the syntax in your do loops.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to address a macro variable &amp;amp;var2 where the numeric portion in the name also uses a macro variable then you need to pay attention on how SAS resolves things. So for the above example the name in the code should be: &amp;amp;&amp;amp;var&amp;amp;i&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET var1=USA;
%LET var2=JPN;
%LET var3=GBR;
%LET var4=Ind_73;
%LET var5=Ind_36;
%LET var6=Ind_28;
%LET var7=Ind_60;

%MACRO Divide_year;
  %DO i=1 %TO 7;
    %DO j=2000 %TO 2017;

      DATA &amp;amp;&amp;amp;var&amp;amp;i.._&amp;amp;j;
        SET &amp;amp;&amp;amp;var&amp;amp;i;

        IF Annc_fyear=&amp;amp;j;
        KEEP Source Target FIC SIC_2D;
      run;

      PROC EXPORT data=&amp;amp;&amp;amp;var&amp;amp;i.._&amp;amp;j dbms=xlsx
        outfile="C:\Users\kchoi9\Desktop\Network and Finance\Data\Three_countries_four_industries\&amp;amp;&amp;amp;var&amp;amp;i.._&amp;amp;j..xlsx"
        replace;
      RUN;

    %END;
  %END;
%MEND Divide_year;

%Divide_year;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also consider to make things a bit simpler for you using a code variation as below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%MACRO Divide_year(inds);
  %DO j=2000 %TO 2017;

    DATA &amp;amp;inds._&amp;amp;j;
      SET &amp;amp;inds;

      IF Annc_fyear=&amp;amp;j;
      KEEP Source Target FIC SIC_2D;
    run;

    PROC EXPORT data=&amp;amp;inds._&amp;amp;j dbms=xlsx
      outfile="C:\Users\kchoi9\Desktop\Network and Finance\Data\Three_countries_four_industries\&amp;amp;inds._&amp;amp;j..xlsx"
      replace;
    RUN;

  %END;
%MEND Divide_year;

%Divide_year(USA);
%Divide_year(JPN);
%Divide_year(GBR);
%Divide_year(Ind_73);
%Divide_year(Ind_36);
%Divide_year(Ind_28);
%Divide_year(Ind_60);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If there would be more macro calls then you could also use a SAS data _null_ step with call execute() to generate and execute the macro calls.&lt;/P&gt;</description>
      <pubDate>Sun, 11 Apr 2021 00:31:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-loop-over-a-loop-in-SAS-Macro/m-p/732807#M228349</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-04-11T00:31:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to do loop over a loop in SAS Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-loop-over-a-loop-in-SAS-Macro/m-p/732808#M228350</link>
      <description>Does data set Var1 exist? When SAS says it doesn't exist, then I believe SAS. Show us evidence that VAR1 exists (screen capture is okay).</description>
      <pubDate>Sun, 11 Apr 2021 00:22:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-loop-over-a-loop-in-SAS-Macro/m-p/732808#M228350</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-04-11T00:22:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to do loop over a loop in SAS Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-loop-over-a-loop-in-SAS-Macro/m-p/732812#M228353</link>
      <description>&lt;P&gt;Thank you Patrick!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Both sets of codes work perfectly!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I see was the hidden thorn bothering me for years.&amp;nbsp;&lt;/P&gt;&lt;PRE class="language-sas"&gt;&lt;CODE&gt;&amp;amp;&amp;amp;var&amp;amp;i.._&amp;amp;j;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;The double ampersand and two dots connecting macro indices..&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I can deal with Macro.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a nice weekend!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely,&amp;nbsp;&lt;/P&gt;&lt;P&gt;KS,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Apr 2021 03:40:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-loop-over-a-loop-in-SAS-Macro/m-p/732812#M228353</guid>
      <dc:creator>KS99</dc:creator>
      <dc:date>2021-04-11T03:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to do loop over a loop in SAS Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-loop-over-a-loop-in-SAS-Macro/m-p/732817#M228356</link>
      <description>&lt;P&gt;You can find this documented under below links.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n0vl7e3du5f7cjn1g4ajozqxe6b6.htm&amp;amp;locale=en"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n0vl7e3du5f7cjn1g4ajozqxe6b6.htm&amp;amp;locale=en&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n0700fspmubii5n1vecutttwz93n.htm&amp;amp;locale=en"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n0700fspmubii5n1vecutttwz93n.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each iteration for resolving an ampersand will consume a period. That's why you sometimes need several periods when dealing with multiple ampersands.&lt;/P&gt;</description>
      <pubDate>Sun, 11 Apr 2021 06:37:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-loop-over-a-loop-in-SAS-Macro/m-p/732817#M228356</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-04-11T06:37:14Z</dc:date>
    </item>
  </channel>
</rss>

