<?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 Do LOOP  not in a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-not-in-a-macro/m-p/709546#M218212</link>
    <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Today I try to merge 34 sheets in one file excel together, however, my "do...to" does not work. I also cross-check whether "do...to" can be used outside a macro and the answer is yes.&lt;/P&gt;
&lt;P&gt;Manually speaking, my code should be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merge1;
merge
sheet2_outx (keep type year s22)
sheet3_outx (keep type year s32)
sheet4_outx (keep type year s42)
sheet5_outx (keep type year s52)
/* It will be like that till sheet33*/
sheet34_out (keep type year s34)
/* sheet34 I just have sheet34_out instead of sheet34_outx and s34 instead of s342*/
by type year;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I&amp;nbsp; try to use a "DO to" to make the work become easier and shorter&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merge1;
 do i=1 to 33;
 merge 
  sheeti._outx(keep=type year si.2)
  ;
 by type year;
 end;
 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the error is that&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;28         data merge1;
29          do i=1 to 33;
30          merge
31           sheeti._outx(keep=type year si.2)
ERROR: Libref SHEETI is not assigned.
32           ;
33          by type year;
34          end;
35         
36         run;

NOTE: Compression was disabled for data set WORK.MERGE1 because compression overhead would increase the size of the data set.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.MERGE1 may be incomplete.  When this step was stopped there were 0 observations and 1 variables.
WARNING: Data set WORK.MERGE1 was not replaced because this step was stopped.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am not sure whether "Do...to"&amp;nbsp; in a macro is a must in this case?&lt;/P&gt;
&lt;P&gt;Many thanks!&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>Wed, 06 Jan 2021 03:46:36 GMT</pubDate>
    <dc:creator>Phil_NZ</dc:creator>
    <dc:date>2021-01-06T03:46:36Z</dc:date>
    <item>
      <title>Do LOOP  not in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-not-in-a-macro/m-p/709546#M218212</link>
      <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Today I try to merge 34 sheets in one file excel together, however, my "do...to" does not work. I also cross-check whether "do...to" can be used outside a macro and the answer is yes.&lt;/P&gt;
&lt;P&gt;Manually speaking, my code should be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merge1;
merge
sheet2_outx (keep type year s22)
sheet3_outx (keep type year s32)
sheet4_outx (keep type year s42)
sheet5_outx (keep type year s52)
/* It will be like that till sheet33*/
sheet34_out (keep type year s34)
/* sheet34 I just have sheet34_out instead of sheet34_outx and s34 instead of s342*/
by type year;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I&amp;nbsp; try to use a "DO to" to make the work become easier and shorter&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merge1;
 do i=1 to 33;
 merge 
  sheeti._outx(keep=type year si.2)
  ;
 by type year;
 end;
 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the error is that&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;28         data merge1;
29          do i=1 to 33;
30          merge
31           sheeti._outx(keep=type year si.2)
ERROR: Libref SHEETI is not assigned.
32           ;
33          by type year;
34          end;
35         
36         run;

NOTE: Compression was disabled for data set WORK.MERGE1 because compression overhead would increase the size of the data set.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.MERGE1 may be incomplete.  When this step was stopped there were 0 observations and 1 variables.
WARNING: Data set WORK.MERGE1 was not replaced because this step was stopped.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am not sure whether "Do...to"&amp;nbsp; in a macro is a must in this case?&lt;/P&gt;
&lt;P&gt;Many thanks!&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>Wed, 06 Jan 2021 03:46:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-not-in-a-macro/m-p/709546#M218212</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-06T03:46:36Z</dc:date>
    </item>
    <item>
      <title>Re: Do LOOP  not in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-not-in-a-macro/m-p/709549#M218215</link>
      <description>&lt;P&gt;I also try to write a macro to deal with it but I feel something wrong, can you please help me to spot it out?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro merge (start=, end=);
  %local i;
  %do i=&amp;amp;start. %to &amp;amp;end.;
	%if (&amp;amp;i ne &amp;amp;start.) and (&amp;amp;i. ne &amp;amp;end.)%then %do;
	data merge1;
	 merge
	  sheet&amp;amp;i._outx(keep=type year s&amp;amp;i.2)
     by type year;
    run;
   %end;
   %else %if &amp;amp;i=&amp;amp;end. %then %do;
	data merge2;
	 merge
	  merge1
	  sheet&amp;amp;end._out(keep=type year s&amp;amp;end.)
     by type year;
    run;
   %else %if &amp;amp;i=&amp;amp;start. %then %do;
    data merge3;
	 merge
	  merge2
	  sheet&amp;amp;start.
	  ;
	by type;
	run;
	%end;
   %end;
%mend;

%merge(start=1, end=34);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log is like that&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;45            %else %if &amp;amp;i=&amp;amp;start. %then %do;
ERROR: There is no matching %IF statement for the %ELSE.
ERROR: A dummy macro will be compiled.
46             data merge3;
47         	 merge
48         	  merge2
49         	  sheet&amp;amp;start.
50         	  ;
51         	by type;
52         	run;
53         	%end;
54            %end;
55         %mend;
2                                                          The SAS System                           16:18 Wednesday, January 6, 2021

56         
57         %merge(start=1, end=34);
           _
           180
WARNING: Apparent invocation of macro MERGE not resolved.

ERROR 180-322: Statement is not valid or it is used out of proper order.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Many thanks in advance!&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>Wed, 06 Jan 2021 04:16:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-not-in-a-macro/m-p/709549#M218215</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-06T04:16:09Z</dc:date>
    </item>
    <item>
      <title>Re: Do LOOP  not in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-not-in-a-macro/m-p/709555#M218219</link>
      <description>&lt;P&gt;You can't create dataset names using a normal loop in a data-step. You could use macro-code, but i strongly recommend not write any macro-code until you fully understand the data-step.&lt;/P&gt;
&lt;P&gt;In SAS you don't have sheets - this is a concept only valid in excel (and LibreOffice Calc), in SAS you have datasets, and while both look alike at a quick glance, they are totally different when it comes to using them. So, if all those sheet dataset are created by importing excel files, it is very likely that they all appear to have the same structure, but are different. You should, before you start writing the code merging all those datasets, have a look at the metadata, proc contents is a great help, to verify that at least type and year have the same data-type and the same length in all datasets. If you find differences it would be best to drop Excel as data source and switch to csv-files. Those files can be imported by using a data step granting you full control over the result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And there is another problem with the loop: the name of the third variable in the keep-option differs from dataset to dataset. If the name of that variable is "s342" for the 34th dataset, you could use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro merger;
  %local i;
  
  data merge1;
    merge
	  %do i = 1 %to 34;
	    sheet&amp;amp;i._outx(keep= type year s&amp;amp;i.2)
	  %end;
	;
	by type year;
  run;
%mend;

%merger;
&lt;/CODE&gt;&lt;/PRE&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>Wed, 06 Jan 2021 06:13:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-not-in-a-macro/m-p/709555#M218219</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-01-06T06:13:04Z</dc:date>
    </item>
    <item>
      <title>Re: Do LOOP  not in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-not-in-a-macro/m-p/709560#M218224</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much for your reply!&lt;/P&gt;
&lt;P&gt;The sheets I mentioned in the code is the name of the dataset from the previous code.&lt;/P&gt;
&lt;P&gt;I conduct a code that work well and successfully. Because the structure of sheet1 and 34 are quite different from other sheets so I need to have 3 merge files. I am wondering if there is any way to optimize such a code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And there is another interesting point to me. It is even I did not put the semicolon ";" at the end of macro merger, my SAS&amp;nbsp; even runs and did not announce any error or warning, so surprising to me!!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  %macro merger (start=, end=);
  %local i;
  
  data merge1;
     merge
	  %do i =&amp;amp;start.+1 %to &amp;amp;end.-1;
	    sheet&amp;amp;i._outx(keep= type year s&amp;amp;i.2)
	  %end;
	;
	by type year;
  run;

  data merge2;
     merge
	  merge1
	  sheet&amp;amp;end._out (keep=type year s&amp;amp;end.)
	  ;
	 by type year;
  run;
 data merge3;
     merge
	  sheet1
	  merge2 
	 ;
	 by type;
 run;
 %mend;

 %merger(start=1, end=34)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV id="eJOY__extension_root" class="eJOY__extension_root_class" style="all: unset;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Wed, 06 Jan 2021 06:40:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-not-in-a-macro/m-p/709560#M218224</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-06T06:40:19Z</dc:date>
    </item>
    <item>
      <title>Re: Do LOOP  not in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-not-in-a-macro/m-p/709564#M218228</link>
      <description>&lt;P&gt;If a macro creates complete statements, no semicolon is needed after the call. Keep in mind that a macro can also be called to resolve to a word (e.g. a library name):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set %deptlib(&amp;amp;user.).dataset;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;where you must not have a semicolon.&lt;/P&gt;
&lt;P&gt;A semicolon is only needed if the code created by the macro demands it, like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set
  %datasets(&amp;amp;today.)
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Jan 2021 08:37:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-not-in-a-macro/m-p/709564#M218228</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-06T08:37:10Z</dc:date>
    </item>
  </channel>
</rss>

