<?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 Local macro problem in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Local-macro-problem/m-p/709035#M217938</link>
    <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Today, I ran a macro to run all sheets of an excel file with the same code, leaving an exception for sheet 1 as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options compress=yes reuse=yes;

%macro ImportAndTranspose(
      File=
      , StartSheet=
      , EndSheet=
   );
   
   %local i;
   
   %do i = &amp;amp;StartSheet. %to &amp;amp;EndSheet.;

    %if i=1 %then %do;
    proc import datafile= "&amp;amp;File." 
                  out= sheet1
                  dbms= xlsx 
                  replace;
          range= "Sheet1$A:X";
          getnames= yes;
      run;
      
      proc sort data= sheet1;
      by Type;
      run;
	%end;

    %else %if i ne 1 %then %do;
      proc import datafile= "&amp;amp;File." 
                  out= sheet&amp;amp;i.
                  dbms= xlsx 
                  replace;
          range= "Sheet&amp;amp;i.$A:AG";
          getnames= yes;
      run;
      
      proc sort data= sheet&amp;amp;i.;
      by Type;
      run;

      proc transpose data= sheet&amp;amp;i. 
            out= sheet&amp;amp;i._out(rename=(COL1=s&amp;amp;i. _NAME_=Year) drop=_label_);
         by Type;
         VAR '1988'N - '2019'N;
      run;
	  proc print data=sheet&amp;amp;i._out;
	  run;

      DATA sheet&amp;amp;i._outx;
      set sheet&amp;amp;i._out;

      if s&amp;amp;i. not in: ('NA', '$$') then s&amp;amp;i.2=input(s&amp;amp;i., 32.);
	  drop s&amp;amp;i.;
      run;
	  %end;

   %end;
   
%mend;

%ImportAndTranspose(
      File= C:\Users\pnguyen\Desktop\Argentina_.xlsx
      , StartSheet= 1
      , EndSheet= 4);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, the result turns out that SAS even read the column from A to AG in sheet1 and result in sheet1_outx as well while the code for sheet 1 is only&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if i=1 %then %do;
    proc import datafile= "&amp;amp;File." 
                  out= sheet1
                  dbms= xlsx 
                  replace;
          range= "Sheet1$A:X";
          getnames= yes;
      run;
      
      proc sort data= sheet1;
      by Type;
      run;
	%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am wondering if there is any point that I did wrong in the code.&lt;/P&gt;
&lt;P&gt;Many thanks and warmest regards.&lt;/P&gt;
&lt;DIV id="eJOY__extension_root" class="eJOY__extension_root_class" style="all: unset;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
    <pubDate>Sat, 02 Jan 2021 02:59:17 GMT</pubDate>
    <dc:creator>Phil_NZ</dc:creator>
    <dc:date>2021-01-02T02:59:17Z</dc:date>
    <item>
      <title>Local macro problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Local-macro-problem/m-p/709035#M217938</link>
      <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Today, I ran a macro to run all sheets of an excel file with the same code, leaving an exception for sheet 1 as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options compress=yes reuse=yes;

%macro ImportAndTranspose(
      File=
      , StartSheet=
      , EndSheet=
   );
   
   %local i;
   
   %do i = &amp;amp;StartSheet. %to &amp;amp;EndSheet.;

    %if i=1 %then %do;
    proc import datafile= "&amp;amp;File." 
                  out= sheet1
                  dbms= xlsx 
                  replace;
          range= "Sheet1$A:X";
          getnames= yes;
      run;
      
      proc sort data= sheet1;
      by Type;
      run;
	%end;

    %else %if i ne 1 %then %do;
      proc import datafile= "&amp;amp;File." 
                  out= sheet&amp;amp;i.
                  dbms= xlsx 
                  replace;
          range= "Sheet&amp;amp;i.$A:AG";
          getnames= yes;
      run;
      
      proc sort data= sheet&amp;amp;i.;
      by Type;
      run;

      proc transpose data= sheet&amp;amp;i. 
            out= sheet&amp;amp;i._out(rename=(COL1=s&amp;amp;i. _NAME_=Year) drop=_label_);
         by Type;
         VAR '1988'N - '2019'N;
      run;
	  proc print data=sheet&amp;amp;i._out;
	  run;

      DATA sheet&amp;amp;i._outx;
      set sheet&amp;amp;i._out;

      if s&amp;amp;i. not in: ('NA', '$$') then s&amp;amp;i.2=input(s&amp;amp;i., 32.);
	  drop s&amp;amp;i.;
      run;
	  %end;

   %end;
   
%mend;

%ImportAndTranspose(
      File= C:\Users\pnguyen\Desktop\Argentina_.xlsx
      , StartSheet= 1
      , EndSheet= 4);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, the result turns out that SAS even read the column from A to AG in sheet1 and result in sheet1_outx as well while the code for sheet 1 is only&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if i=1 %then %do;
    proc import datafile= "&amp;amp;File." 
                  out= sheet1
                  dbms= xlsx 
                  replace;
          range= "Sheet1$A:X";
          getnames= yes;
      run;
      
      proc sort data= sheet1;
      by Type;
      run;
	%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am wondering if there is any point that I did wrong in the code.&lt;/P&gt;
&lt;P&gt;Many thanks and warmest regards.&lt;/P&gt;
&lt;DIV id="eJOY__extension_root" class="eJOY__extension_root_class" style="all: unset;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sat, 02 Jan 2021 02:59:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Local-macro-problem/m-p/709035#M217938</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-02T02:59:17Z</dc:date>
    </item>
    <item>
      <title>Re: Local macro problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Local-macro-problem/m-p/709041#M217940</link>
      <description>&lt;P&gt;You need to use ampersand (&amp;amp;) to properly resolve your macro %do loop index variable &lt;STRONG&gt;i &lt;/STRONG&gt;in your macro test expression&lt;/P&gt;
&lt;P&gt;Change&lt;/P&gt;
&lt;PRE&gt;%if i=1 %then %do;&lt;/PRE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;PRE&gt;%if &amp;amp;i=1 %then %do;   /* all hail the important ampersand */
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What happened?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because the letter i is never literally 1 your first expected %then %do block did not execute&lt;/P&gt;
&lt;P&gt;Because the letter i is always never literally 1 the second %then %do block was executed for each sheet.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jan 2021 04:50:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Local-macro-problem/m-p/709041#M217940</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2021-01-02T04:50:08Z</dc:date>
    </item>
  </channel>
</rss>

