<?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 Writing a loop to append multiple datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-loop-to-append-multiple-datasets/m-p/888594#M351065</link>
    <description>&lt;P&gt;Hello.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have multiple datasets whose name has the following format: data_yyyymm.&amp;nbsp; For example, the names of the datasets are data_202211, data_202212, data_202301, and so on.&amp;nbsp; They all have same variables with year and month indicators in each dataset. I want to append these data so that I would have one master dataset that have all these datasets. I was thinking of using the following commands to work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data masterdataset;
set data_202211;
do i = 202212&amp;nbsp; to 202307;
&amp;nbsp; &amp;nbsp; &amp;nbsp; some kinds of commands for append
end;
run;&lt;/PRE&gt;&lt;P&gt;I was not sure how I could make this code work so that I can have one master dataset.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could anyone give me some suggestion? Thank you in advance for your time and help!&lt;/P&gt;</description>
    <pubDate>Wed, 09 Aug 2023 15:34:27 GMT</pubDate>
    <dc:creator>master_kim</dc:creator>
    <dc:date>2023-08-09T15:34:27Z</dc:date>
    <item>
      <title>Writing a loop to append multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-loop-to-append-multiple-datasets/m-p/888594#M351065</link>
      <description>&lt;P&gt;Hello.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have multiple datasets whose name has the following format: data_yyyymm.&amp;nbsp; For example, the names of the datasets are data_202211, data_202212, data_202301, and so on.&amp;nbsp; They all have same variables with year and month indicators in each dataset. I want to append these data so that I would have one master dataset that have all these datasets. I was thinking of using the following commands to work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data masterdataset;
set data_202211;
do i = 202212&amp;nbsp; to 202307;
&amp;nbsp; &amp;nbsp; &amp;nbsp; some kinds of commands for append
end;
run;&lt;/PRE&gt;&lt;P&gt;I was not sure how I could make this code work so that I can have one master dataset.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could anyone give me some suggestion? Thank you in advance for your time and help!&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2023 15:34:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-loop-to-append-multiple-datasets/m-p/888594#M351065</guid>
      <dc:creator>master_kim</dc:creator>
      <dc:date>2023-08-09T15:34:27Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a loop to append multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-loop-to-append-multiple-datasets/m-p/888598#M351067</link>
      <description>&lt;P&gt;What about shortcut lists instead?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data master;
set data_202211-data_202212 data_2023: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Aug 2023 15:38:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-loop-to-append-multiple-datasets/m-p/888598#M351067</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-08-09T15:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a loop to append multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-loop-to-append-multiple-datasets/m-p/888608#M351072</link>
      <description>&lt;P&gt;That is an amazing alternative and it worked perfectly!&lt;/P&gt;&lt;P&gt;Thank you very much for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2023 16:42:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-loop-to-append-multiple-datasets/m-p/888608#M351072</guid>
      <dc:creator>master_kim</dc:creator>
      <dc:date>2023-08-09T16:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a loop to append multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-loop-to-append-multiple-datasets/m-p/888610#M351074</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s reply is compact and effective:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;SPAN&gt;What about shortcut lists instead?&lt;/SPAN&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data master;
set data_202211-data_202212 data_2023: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But if this compact SET statement implies reading of a large number of datasets, just be aware that computer memory is allocated to build a buffer for each dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have dealt with this situation when working with daily TAQ (trade and/or quote) datasets from the us stock market, with names constructed with the pattern CTyyyymmdd.&amp;nbsp; If one were reading all the data for, say, June and July (i.e. SET CT202306: CT202307:), it would require 42 such buffers - one for each trading day.&amp;nbsp; &amp;nbsp;And you can be sure many researchers may not want to stop at analyzing two months of data.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The upshot is that this can become costly to performance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In certain cases, there is an escape:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If all those datasets had the same variables, then use of the statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;SET CT202306: CT202307:    open=defer;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;helps a lot.&amp;nbsp; The "open=defer" option tells SAS to defer opening the second dataset until the first is fully read. and apply the same principle to each of the datasets.&amp;nbsp; That is, it tells SAS to re-use the same buffer for each of the 42 datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even if they have different variables, there is a trivial workaround: create an empty dummy dataset with all the variables (but no observations), and then list it first in the SET statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dummy;
  set ct202306: ct202307: ;
  stop;
run;

data new;
  set dummy ct202306: ct202307: open=defer;
  ....
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BUT (you knew another "but" was coming, right?) it still doesn't allow OPEN=DEFER in the presence of a BY statement.&amp;nbsp; Consider this code (assume each dataset has one DATE value, but is sorted by STOCK_TICKER),&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set CT202306: CT202307: open=defer;
by stock_ticker&amp;nbsp;date;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This can't reduce the number of buffers, because SAS has to have all 42 datasets open in order to produce the interleaved sequencing demanded by the BY statement.&amp;nbsp; In fact, it won't run at all, but will generate&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;ERROR: OPEN=DEFER is incompatible with the BY statement.&lt;BR /&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;in the log.&lt;/SPAN&gt;&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>Wed, 09 Aug 2023 16:45:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-loop-to-append-multiple-datasets/m-p/888610#M351074</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-09T16:45:47Z</dc:date>
    </item>
  </channel>
</rss>

