<?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: HELP - Import Multiple txt files with Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828401#M327242</link>
    <description>&lt;P&gt;Why are you using PROC IMPORT to read a text file?&amp;nbsp; The DATA step can easily read a text file. In fact all PROC IMPORT does is try to GUESS what variables you want to read from the file and generates a data step for you.&amp;nbsp; But that will cause a lot of headaches when you have multiple files.&amp;nbsp; It could make different GUESSes on different files.&amp;nbsp; Then it will be hard to combine the data or write code that uses the multiple files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do all of the files have the same format?&amp;nbsp; The same list of variables ("columns") in the same order?&amp;nbsp; If so then just write one data step to read them all.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data animals;
  length filename fname $200 ;
  infile "/home/u58922560/sasuser.v94/animal*.txt" dsd dlm='09'x truncover filename=fname;
  input @;
  if fname ne lag(fname) then delete;
  filename = scan(fname,-1,'/');
  input var1 var2 :$20. .... ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You just need to update the INPUT statement to read your actual variables.&amp;nbsp; Add any LABELs to the variables you want.&amp;nbsp; Attach and formats to variables that need them (most variables do NOT need to have formats attached to them).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 12 Aug 2022 02:31:15 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-08-12T02:31:15Z</dc:date>
    <item>
      <title>HELP - Import Multiple txt files with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828394#M327239</link>
      <description>&lt;P&gt;Hi everyone!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to import multiple files in txt format. Anyone know how i can do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code to import file?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc import file="/home/u58922560/sasuser.v94/animal01.txt"&lt;BR /&gt;out=work.animal01&lt;BR /&gt;dbms=tab&lt;BR /&gt;replace;&lt;BR /&gt;delimiter=';';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyone can help?&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2022 01:21:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828394#M327239</guid>
      <dc:creator>guilhermeagc</dc:creator>
      <dc:date>2022-08-12T01:21:08Z</dc:date>
    </item>
    <item>
      <title>Re: HELP - Import Multiple txt files with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828399#M327241</link>
      <description>&lt;P&gt;Step 1 : Get list of available files in that particular folder. To achieve this, use fopen and dread function in datastep and create dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Step 2: Read the each record available in this table using do loop and invoke proc import to import .txt file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your reference, providing some sample code. We can modify this as per your need.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*******************************/&lt;/P&gt;
&lt;P&gt;%macro import_file;&lt;BR /&gt;%let your_folder_loc = "&amp;lt;Enter Path here&amp;gt;";&lt;BR /&gt;&lt;BR /&gt;filename myfldr "&amp;amp;your_folder_loc.";&lt;/P&gt;
&lt;P&gt;data filenames;&lt;BR /&gt;length complete_filename $200;&lt;BR /&gt;did=dopen('myfldr');&lt;BR /&gt;mcount=dnum(did);&lt;/P&gt;
&lt;P&gt;do i=1 to mcount;&lt;BR /&gt;complete_filename=dread(did, i);&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;rc=dclose(did);&lt;BR /&gt;keep complete_filename ;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;filename myfldr clear;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;select count(*) into :m_file_lst_cnt from filenames;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;%let m_file_lst_cnt = &amp;amp;m_file_lst_cnt.;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;m_file_lst_cnt gt 0 %then&lt;BR /&gt;%do;&lt;/P&gt;
&lt;P&gt;%do dm_ds_pointer=1 %to &amp;amp;m_file_lst_cnt.;&lt;BR /&gt;%local complete_filename filename_wout_ext ;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;pointer=&amp;amp;dm_ds_pointer;&lt;BR /&gt;set filenames point=pointer;&lt;BR /&gt;call symput('filename_wout_ext', scan(complete_filename, 1, '.'));&lt;BR /&gt;call symput('complete_filename', complete_filename);&lt;BR /&gt;stop;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;%let complete_filename = &amp;amp;complete_filename.;&lt;BR /&gt;%let filename_wout_ext = &amp;amp;filename_wout_ext.;&lt;BR /&gt;filename input "&amp;amp;your_folder_loc./&amp;amp;complete_filename.";&lt;/P&gt;
&lt;P&gt;proc import datafile=input out=&amp;amp;filename_wout_ext. dbms=xlsx replace;&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%end;&lt;BR /&gt;%mend import_file;&lt;/P&gt;
&lt;P&gt;/***************************/&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2022 02:28:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828399#M327241</guid>
      <dc:creator>sanchit013</dc:creator>
      <dc:date>2022-08-12T02:28:13Z</dc:date>
    </item>
    <item>
      <title>Re: HELP - Import Multiple txt files with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828401#M327242</link>
      <description>&lt;P&gt;Why are you using PROC IMPORT to read a text file?&amp;nbsp; The DATA step can easily read a text file. In fact all PROC IMPORT does is try to GUESS what variables you want to read from the file and generates a data step for you.&amp;nbsp; But that will cause a lot of headaches when you have multiple files.&amp;nbsp; It could make different GUESSes on different files.&amp;nbsp; Then it will be hard to combine the data or write code that uses the multiple files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do all of the files have the same format?&amp;nbsp; The same list of variables ("columns") in the same order?&amp;nbsp; If so then just write one data step to read them all.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data animals;
  length filename fname $200 ;
  infile "/home/u58922560/sasuser.v94/animal*.txt" dsd dlm='09'x truncover filename=fname;
  input @;
  if fname ne lag(fname) then delete;
  filename = scan(fname,-1,'/');
  input var1 var2 :$20. .... ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You just need to update the INPUT statement to read your actual variables.&amp;nbsp; Add any LABELs to the variables you want.&amp;nbsp; Attach and formats to variables that need them (most variables do NOT need to have formats attached to them).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2022 02:31:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828401#M327242</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-12T02:31:15Z</dc:date>
    </item>
    <item>
      <title>Re: HELP - Import Multiple txt files with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828430#M327250</link>
      <description>Hello friend! Thanks for help.&lt;BR /&gt;&lt;BR /&gt;Yes, the files have the same name, bud 01, 02, 03, 04... etc&lt;BR /&gt;Also the same format (.txt) the same columns, at the same order.&lt;BR /&gt;&lt;BR /&gt;I never made a macro before, its the first time.</description>
      <pubDate>Fri, 12 Aug 2022 10:28:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828430#M327250</guid>
      <dc:creator>guilhermeagc</dc:creator>
      <dc:date>2022-08-12T10:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: HELP - Import Multiple txt files with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828431#M327251</link>
      <description>Thanks mate, i will try it.&lt;BR /&gt;Many thanks.</description>
      <pubDate>Fri, 12 Aug 2022 10:29:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828431#M327251</guid>
      <dc:creator>guilhermeagc</dc:creator>
      <dc:date>2022-08-12T10:29:28Z</dc:date>
    </item>
    <item>
      <title>Re: HELP - Import Multiple txt files with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828478#M327258</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/388043"&gt;@guilhermeagc&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hello friend! Thanks for help.&lt;BR /&gt;&lt;BR /&gt;Yes, the files have the same name, bud 01, 02, 03, 04... etc&lt;BR /&gt;Also the same format (.txt) the same columns, at the same order.&lt;BR /&gt;&lt;BR /&gt;I never made a macro before, its the first time.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then there is no need for a macro, and also not PROC IMPORT. Use the data step with a wildcard.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2022 14:59:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-Import-Multiple-txt-files-with-Macro/m-p/828478#M327258</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-08-12T14:59:31Z</dc:date>
    </item>
  </channel>
</rss>

