<?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: Using a DO loop to import/export datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761021#M240740</link>
    <description>&lt;P&gt;Thanks, you paper helped a lot! Here's what I ended up doing. I do have to list out the file names unfortunately, they aren't consistent.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let var1=example1;&lt;BR /&gt;%let var2=example2;&lt;BR /&gt;%let var3=example3;&lt;/P&gt;&lt;P&gt;n=1;&lt;/P&gt;&lt;P&gt;%macro importex;&lt;BR /&gt;%do n=1 %to 3;&lt;BR /&gt;proc import datafile="pathname\&amp;amp;var&amp;amp;n...txt"&lt;BR /&gt;out=example&amp;amp;n&lt;BR /&gt;dbms = tab replace;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%mend n;&lt;/P&gt;&lt;P&gt;%importex&lt;/P&gt;</description>
    <pubDate>Wed, 11 Aug 2021 21:22:39 GMT</pubDate>
    <dc:creator>smg3141</dc:creator>
    <dc:date>2021-08-11T21:22:39Z</dc:date>
    <item>
      <title>Using a DO loop to import/export datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761002#M240731</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a large number of .txt files I would like to import, manipulate in the same manner, and export. Is it possible to use a DO loop for this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I would like to use a DO loop to import file1, file2, file3, etc. instead of typing out an import step for each individual file.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Right now I'm using&lt;/P&gt;&lt;P&gt;%let var1=file1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc import datafile="pathname\&amp;amp;var1..txt"&lt;BR /&gt;out=example1&lt;BR /&gt;dbms = tab replace;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then changing the macro to file2, file3, etc, and rerunning each time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Wed, 11 Aug 2021 20:19:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761002#M240731</guid>
      <dc:creator>smg3141</dc:creator>
      <dc:date>2021-08-11T20:19:41Z</dc:date>
    </item>
    <item>
      <title>Re: Using a DO loop to import/export datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761010#M240733</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324306"&gt;@smg3141&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a large number of .txt files I would like to import, manipulate in the same manner, and export. Is it possible to use a DO loop for this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, I would like to use a DO loop to import file1, file2, file3, etc. instead of typing out an import step for each individual file.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Right now I'm using&lt;/P&gt;
&lt;P&gt;%let var1=file1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc import datafile="pathname\&amp;amp;var1..txt"&lt;BR /&gt;out=example1&lt;BR /&gt;dbms = tab replace;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then changing the macro to file2, file3, etc, and rerunning each time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, you can use the FILEVAR.&amp;nbsp; You could also use a macro do loop (%DO - %END).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See&amp;nbsp;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi27/p082-27.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi27/p082-27.pdf&lt;/A&gt;&amp;nbsp;for further info on FILEVAR.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Wed, 11 Aug 2021 20:50:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761010#M240733</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-11T20:50:17Z</dc:date>
    </item>
    <item>
      <title>Re: Using a DO loop to import/export datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761015#M240735</link>
      <description>&lt;P&gt;Depending on exactly what you're doing you may be able to do it in a single data step using FILEVAR, but otherwise, using macros is a good option. See some examples and tutorials below.&lt;BR /&gt;&lt;BR /&gt;Tutorial on converting a working program to a macro&lt;BR /&gt;&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Examples of common macro usage&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UCLA introductory tutorial on macro variables and macros&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324306"&gt;@smg3141&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a large number of .txt files I would like to import, manipulate in the same manner, and export. Is it possible to use a DO loop for this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, I would like to use a DO loop to import file1, file2, file3, etc. instead of typing out an import step for each individual file.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Right now I'm using&lt;/P&gt;
&lt;P&gt;%let var1=file1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc import datafile="pathname\&amp;amp;var1..txt"&lt;BR /&gt;out=example1&lt;BR /&gt;dbms = tab replace;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then changing the macro to file2, file3, etc, and rerunning each time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Aug 2021 20:52:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761015#M240735</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-08-11T20:52:20Z</dc:date>
    </item>
    <item>
      <title>Re: Using a DO loop to import/export datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761021#M240740</link>
      <description>&lt;P&gt;Thanks, you paper helped a lot! Here's what I ended up doing. I do have to list out the file names unfortunately, they aren't consistent.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let var1=example1;&lt;BR /&gt;%let var2=example2;&lt;BR /&gt;%let var3=example3;&lt;/P&gt;&lt;P&gt;n=1;&lt;/P&gt;&lt;P&gt;%macro importex;&lt;BR /&gt;%do n=1 %to 3;&lt;BR /&gt;proc import datafile="pathname\&amp;amp;var&amp;amp;n...txt"&lt;BR /&gt;out=example&amp;amp;n&lt;BR /&gt;dbms = tab replace;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%mend n;&lt;/P&gt;&lt;P&gt;%importex&lt;/P&gt;</description>
      <pubDate>Wed, 11 Aug 2021 21:22:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761021#M240740</guid>
      <dc:creator>smg3141</dc:creator>
      <dc:date>2021-08-11T21:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: Using a DO loop to import/export datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761025#M240743</link>
      <description>You may also want to consider this macro then, especially if you can place all the files to be processed in the same folder. &lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n0ctmldxf23ixtn1kqsoh5bsgmg8.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n0ctmldxf23ixtn1kqsoh5bsgmg8.htm&lt;/A&gt;</description>
      <pubDate>Wed, 11 Aug 2021 21:50:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761025#M240743</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-08-11T21:50:16Z</dc:date>
    </item>
    <item>
      <title>Re: Using a DO loop to import/export datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761038#M240747</link>
      <description>&lt;P&gt;If your data sets are supposed to be the same variables and such just coming in different files you may not be happy with the results of Proc Import on multiple files as you may end up with&amp;nbsp; mixed variable types and lengths of character variables.&lt;/P&gt;
&lt;P&gt;You might reduce that by adding the Guessingrows=max; to the import code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If they should all be the same then writing a data step to read them instead of Import may be worth the effort.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Aug 2021 23:12:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-DO-loop-to-import-export-datasets/m-p/761038#M240747</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-08-11T23:12:59Z</dc:date>
    </item>
  </channel>
</rss>

