<?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 can I do a loop to faster my data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584815#M166654</link>
    <description>&lt;P&gt;&lt;SPAN&gt;&lt;EM&gt;&amp;gt;It takes longer for me to run DATA instead of PROC APPEND. These 9 files are pretty big. So if there's an option, I'd avoid going through each obs in the dataset.&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fair point and good on you for considering efficiency.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you keep data steps, consider increasing the BUFNO and BUFSIZE for faster I/O.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 29 Aug 2019 00:08:02 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2019-08-29T00:08:02Z</dc:date>
    <item>
      <title>How can I do a loop to faster my data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584803#M166642</link>
      <description>&lt;P&gt;Hi there,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I have 9 datasets which are quite big and I just want to keep some variables from those datasets. At last, I want to append/merge those 9 files to create a master file. What I have done so far is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA TEST1; SET DSN1; KEEP &amp;amp;outcome_vars ;RUN;&lt;BR /&gt;DATA TEST2; SET DSN2; KEEP &amp;amp;outcome_vars ;RUN;&lt;BR /&gt;DATA TEST3; SET DSN3; KEEP &amp;amp;outcome_vars ;RUN;&lt;BR /&gt;DATA TEST4; SET DSN4; KEEP &amp;amp;outcome_vars ;RUN;&lt;/P&gt;&lt;P&gt;.....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can tell, it's very tedious. Is there a more efficient way?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regarding append/merge, I'm thinking using:&lt;/P&gt;&lt;P&gt;PROC APPEND BASE = TEST1 DATA = TEST2 OUT = TEST;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;PROC APPEND BASE = TEST1 DATA = TEST3 OUT = TEST;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;OR&lt;/P&gt;&lt;P&gt;data master;&lt;/P&gt;&lt;P&gt;merge test1 test2 test3 test4;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;(I'm not sure if I can merge &amp;gt;2 files at once.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 22:18:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584803#M166642</guid>
      <dc:creator>maxjiang6999</dc:creator>
      <dc:date>2019-08-28T22:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: How can I do a loop to faster my data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584807#M166646</link>
      <description>&lt;P&gt;Have you tried this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MASTER;
  set TEST1 (keep=&amp;amp;vars) 
      TEST2 (keep=&amp;amp;vars) 
      TEST3 (keep=&amp;amp;vars) 
      TEST4 (keep=&amp;amp;vars) ;
run;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Aug 2019 22:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584807#M166646</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-08-28T22:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: How can I do a loop to faster my data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584809#M166648</link>
      <description>&lt;P&gt;Maybe you don't need to create the large table though, maybe a view is enough.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MASTER_V/view=MASTER_V;
  set TEST1 (keep=&amp;amp;vars) 
      TEST2 (keep=&amp;amp;vars) 
      TEST3 (keep=&amp;amp;vars) 
      TEST4 (keep=&amp;amp;vars) ;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 22:31:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584809#M166648</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-08-28T22:31:58Z</dc:date>
    </item>
    <item>
      <title>Re: How can I do a loop to faster my data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584810#M166649</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;thanks for the quick reply.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Before merging data, I'm thinking to make my DATA step more efficient. I don't want to manually write 9 lines of code in the form of:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;DATA TEST1; SET DSN1; KEEP &amp;amp;outcome_vars ;RUN;&lt;BR /&gt;DATA TEST2; SET DSN2; KEEP &amp;amp;outcome_vars ;RUN;&lt;BR /&gt;DATA TEST3; SET DSN3; KEEP &amp;amp;outcome_vars ;RUN;&lt;BR /&gt;DATA TEST4; SET DSN4; KEEP &amp;amp;outcome_vars ;RUN;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 22:34:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584810#M166649</guid>
      <dc:creator>maxjiang6999</dc:creator>
      <dc:date>2019-08-28T22:34:08Z</dc:date>
    </item>
    <item>
      <title>Re: How can I do a loop to faster my data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584811#M166650</link>
      <description>It takes longer for me to run DATA instead of PROC APPEND. These 9 files are pretty big. So if there's an option, I'd avoid going through each obs in the dataset.</description>
      <pubDate>Wed, 28 Aug 2019 22:38:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584811#M166650</guid>
      <dc:creator>maxjiang6999</dc:creator>
      <dc:date>2019-08-28T22:38:30Z</dc:date>
    </item>
    <item>
      <title>Re: How can I do a loop to faster my data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584813#M166652</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/283179"&gt;@maxjiang6999&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The question is why you've got these 9 source tables in first place. If that's something you create then consider if a change upstream could allow you to process all the data in 1 go.&lt;/P&gt;
&lt;P&gt;It's true that an append is faster than a data&amp;nbsp; step set BUT you can only append 1 table at a time so you would have to write 1 data step which creates the table structure (using your first table) and then write 8 separate Proc Append statements to append the other 8 tables - or you need to generate it using SAS Macro language.&lt;/P&gt;
&lt;P&gt;So... if performance is not that critical then I'd go for what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;suggested: A single data step where you just "set" all the tables.&lt;/P&gt;
&lt;P&gt;If these tables follow a naming convention as in your code sample then it could even be as simple as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MASTER_V;
  set TEST1 - test9;
  keep=&amp;amp;vars; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...and also the proposed data step view is a viable option if the only purpose of this master is to have a way to read all the tables at once for downstream processing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 23:09:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584813#M166652</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-08-28T23:09:16Z</dc:date>
    </item>
    <item>
      <title>Re: How can I do a loop to faster my data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584814#M166653</link>
      <description>&lt;P&gt;This then?&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;proc append base=MASTER_V data=TEST1 &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;keep&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;vars&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; ;
&lt;/SPAN&gt;&lt;SPAN class="token procnames"&gt;proc append base=&lt;/SPAN&gt;MASTER_V data=TEST2 &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;keep&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;vars&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; ;
&lt;SPAN class="token procnames"&gt;proc append base=&lt;/SPAN&gt;MASTER_V data=TEST3 &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;keep&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;vars&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; ;
&lt;SPAN class="token procnames"&gt;proc append base=MASTER_V data=TEST4 &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;keep&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;vars&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; ;
run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 29 Aug 2019 00:04:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584814#M166653</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-08-29T00:04:20Z</dc:date>
    </item>
    <item>
      <title>Re: How can I do a loop to faster my data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584815#M166654</link>
      <description>&lt;P&gt;&lt;SPAN&gt;&lt;EM&gt;&amp;gt;It takes longer for me to run DATA instead of PROC APPEND. These 9 files are pretty big. So if there's an option, I'd avoid going through each obs in the dataset.&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fair point and good on you for considering efficiency.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you keep data steps, consider increasing the BUFNO and BUFSIZE for faster I/O.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 00:08:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584815#M166654</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-08-29T00:08:02Z</dc:date>
    </item>
    <item>
      <title>Re: How can I do a loop to faster my data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584822#M166658</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/283179"&gt;@maxjiang6999&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;The main direction of your intended method is right since APPEND is the fastest tool for the job. It's faster than concatenating the files in the DATA step or UNIONizing them via SQL, because APPEND stacks the DATA= file atop the BASE= file as a block, thus avoiding extruding every record through the PDV. As to MERGE, it's a wrong tool for the job - you want to stack the files vertically, while MERGE does it, roughly speaking, horizontally.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, your APPEND syntax is wrong: It has no OUT= option but merely appends DATA= to BASE=. Furthermore, there's no need to create the interim files TEST1, TEST2, etc. Hence, your process should look schematically as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc append base = master data = dsn1 (keep = &amp;amp;outcome_vars) ; run ;
proc append base = master data = dsn2 (keep = &amp;amp;outcome_vars) ; run ;
proc append base = master data = dsn3 (keep = &amp;amp;outcome_vars) ; run ;
proc append base = master data = dsn4 (keep = &amp;amp;outcome_vars) ; run ;
proc append base = master data = dsn5 (keep = &amp;amp;outcome_vars) ; run ;
proc append base = master data = dsn6 (keep = &amp;amp;outcome_vars) ; run ;
proc append base = master data = dsn7 (keep = &amp;amp;outcome_vars) ; run ;
proc append base = master data = dsn8 (keep = &amp;amp;outcome_vars) ; run ;
proc append base = master data = dsn9 (keep = &amp;amp;outcome_vars) ; run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Frankly, with only 9 files to append, not quite tedious. But supposing that in a different situation you can have more, you may want to generate code like above given the number of files N as a parameter. There're many ways to do this. Using a macro usually comes to mind first, especially since in this case it's very simple:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro stack (base=, prefix=, keep=, N=) ;                    
  %do i = 1 %to &amp;amp;N ;                                          
    proc append base = &amp;amp;base data = &amp;amp;prefix&amp;amp;i (keep = &amp;amp;keep) ;
    run ;                                                     
  %end ;                                                      
%mend ;                                                       
                                                              
%stack (base=master, prefix=dsn, keep=&amp;amp;outcome_vars, N=9)     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Some people like CALL EXECUTE better:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;                                                                                                   
  retain base "master" prefix "dsn" keep "&amp;amp;outcome_vars" N 9 ;                                                  
  do i = 1 to N ;                                                                                               
    call execute (catx (" ", "proc append base=", base, "data=", cats (prefix, N), "(keep=", keep, "); run;")) ;
  end ;                                                                                                         
run ;                                                                                                           
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Suum cuique.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 01:43:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-do-a-loop-to-faster-my-data-step/m-p/584822#M166658</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-29T01:43:52Z</dc:date>
    </item>
  </channel>
</rss>

