<?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 To write SAS macro to import multiple txt files with sequentially incremental name into one database in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/To-write-SAS-macro-to-import-multiple-txt-files-with/m-p/811200#M40629</link>
    <description>&lt;P&gt;&lt;SPAN&gt;I have multiple textfiles with sequentially incremental names, say test_1, test_2, ..., test_1000, each of them have exactly the same format (i.e. number of columns and delimited by comma). My intention is to append all these textfiles into one SAS database following the sequence of their filename. However, with the coding that I am currently using (as attached below), despite it will append all the textfiles, but the appending sequence is not as per my expectation (i.e, it will append test_1,test_10,...,test_19,test_100,...,test_199,test_1000,test_2,test_20,...,test_29,test_200,...,test_299,test_3,test_30,...,test_39,test_300,...,test_399,... instead of test_1, test_2, test_3, ..., test_1000).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;As such, I am trying to explore the feasibility of using SAS macro to get the intended result. Appreciate if I could get any advice from the community to start off with the SAS macro, thank you very much in advance.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data libname.test;
	infile "&amp;amp;dlpath.\test_*.txt"
		delimiter=','
		missover
		firstobs=1
		DSD
		lrecl = 32767;
	format column1 $12.;
	format column2 yymmdds10.;
	format column3 best12.;
	input
		column1 $
		column2 :yymmdd8.
		column3;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 03 May 2022 12:31:01 GMT</pubDate>
    <dc:creator>AZFXL</dc:creator>
    <dc:date>2022-05-03T12:31:01Z</dc:date>
    <item>
      <title>To write SAS macro to import multiple txt files with sequentially incremental name into one database</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/To-write-SAS-macro-to-import-multiple-txt-files-with/m-p/811200#M40629</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I have multiple textfiles with sequentially incremental names, say test_1, test_2, ..., test_1000, each of them have exactly the same format (i.e. number of columns and delimited by comma). My intention is to append all these textfiles into one SAS database following the sequence of their filename. However, with the coding that I am currently using (as attached below), despite it will append all the textfiles, but the appending sequence is not as per my expectation (i.e, it will append test_1,test_10,...,test_19,test_100,...,test_199,test_1000,test_2,test_20,...,test_29,test_200,...,test_299,test_3,test_30,...,test_39,test_300,...,test_399,... instead of test_1, test_2, test_3, ..., test_1000).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;As such, I am trying to explore the feasibility of using SAS macro to get the intended result. Appreciate if I could get any advice from the community to start off with the SAS macro, thank you very much in advance.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data libname.test;
	infile "&amp;amp;dlpath.\test_*.txt"
		delimiter=','
		missover
		firstobs=1
		DSD
		lrecl = 32767;
	format column1 $12.;
	format column2 yymmdds10.;
	format column3 best12.;
	input
		column1 $
		column2 :yymmdd8.
		column3;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2022 12:31:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/To-write-SAS-macro-to-import-multiple-txt-files-with/m-p/811200#M40629</guid>
      <dc:creator>AZFXL</dc:creator>
      <dc:date>2022-05-03T12:31:01Z</dc:date>
    </item>
    <item>
      <title>Re: To write SAS macro to import multiple txt files with sequentially incremental name into one data</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/To-write-SAS-macro-to-import-multiple-txt-files-with/m-p/811201#M40630</link>
      <description>&lt;P&gt;Can't you just sort the information by file name after SAS does all of this appending? You can use the FILENAME= option of the INFILE statement to create a variable in the SAS data set with the name of the original file.&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2022 12:37:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/To-write-SAS-macro-to-import-multiple-txt-files-with/m-p/811201#M40630</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-05-03T12:37:32Z</dc:date>
    </item>
    <item>
      <title>Re: To write SAS macro to import multiple txt files with sequentially incremental name into one data</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/To-write-SAS-macro-to-import-multiple-txt-files-with/m-p/811219#M40631</link>
      <description>&lt;P&gt;So you are saying you want sort by the numeric suffix on the filename instead of sorting on the full filename?&lt;/P&gt;
&lt;P&gt;Do the files already have a variable that contains that number from the filename?&amp;nbsp; Or do you need to create it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There should not be any need for macro coding to solve this, since there is no need to generate code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the FILENAME= option to get the name of the file currently being read.&amp;nbsp; You could then extract the number from that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data libname.test;
  length fname $256;
  infile "&amp;amp;dlpath.\test_*.txt" dsd truncover filename=fname;
  length filenum row 8 column1 $12 column2 8 column3 8;
  format column2 yymmdds10.;
  informat column2 yymmdd.;
  input column1 -- column3;
  if fname ne lag(fname) then row=0;
  row+1;
  filenum=input(scan(fname,-2,'._'),32.);
run;

proc sort data=libname.test ;
  by filenum row;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you could generate a list of file names and use the FILEVAR= option of the INFILE statement instead.&amp;nbsp; Make sure to use the END= option also.&amp;nbsp; So if you had a dataset named FILELIST with a variable named FILENAME that had the files in the order you want to read them then just use a data step like this to read all of the files in that order.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data libname.test;
  set filelist;
  infile TXT filevar=filename dsd truncover end=eof;
  do while not(eof);
      ..... Use the same code to define variables and INPUT one line ....
     output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2022 15:04:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/To-write-SAS-macro-to-import-multiple-txt-files-with/m-p/811219#M40631</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-03T15:04:53Z</dc:date>
    </item>
    <item>
      <title>Re: To write SAS macro to import multiple txt files with sequentially incremental name into one data</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/To-write-SAS-macro-to-import-multiple-txt-files-with/m-p/811310#M40632</link>
      <description>&lt;P&gt;What about sorting the file after the fact instead?&lt;/P&gt;
&lt;P&gt;The filename variable is character so that's a character ordering, test_1, test_100.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can capture the filename in a variable and then extract the number. Then sort by that number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data libname.test;
	infile "&amp;amp;dlpath.\test_*.txt"
		delimiter=','
		missover
		firstobs=1
		DSD
		lrecl = 32767 filename=filevar;
	format column1 $12.;
	format column2 yymmdds10.;
	format column3 best12.;
       file_name=filevar;
      *this may need to be modified to capture the number based on the full path;
       file_num=input(compress(file_name, , 'kd'), 8.);
	input
		column1 $
		column2 :yymmdd8.
		column3;
run;

proc sort data=libname.test;
by file_num column1 column2 column3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/421695"&gt;@AZFXL&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN&gt;I have multiple textfiles with sequentially incremental names, say test_1, test_2, ..., test_1000, each of them have exactly the same format (i.e. number of columns and delimited by comma). My intention is to append all these textfiles into one SAS database following the sequence of their filename. However, with the coding that I am currently using (as attached below), despite it will append all the textfiles, but the appending sequence is not as per my expectation (i.e, it will append test_1,test_10,...,test_19,test_100,...,test_199,test_1000,test_2,test_20,...,test_29,test_200,...,test_299,test_3,test_30,...,test_39,test_300,...,test_399,... instead of test_1, test_2, test_3, ..., test_1000).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;As such, I am trying to explore the feasibility of using SAS macro to get the intended result. Appreciate if I could get any advice from the community to start off with the SAS macro, thank you very much in advance.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data libname.test;
	infile "&amp;amp;dlpath.\test_*.txt"
		delimiter=','
		missover
		firstobs=1
		DSD
		lrecl = 32767;
	format column1 $12.;
	format column2 yymmdds10.;
	format column3 best12.;
	input
		column1 $
		column2 :yymmdd8.
		column3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2022 20:00:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/To-write-SAS-macro-to-import-multiple-txt-files-with/m-p/811310#M40632</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-05-03T20:00:36Z</dc:date>
    </item>
  </channel>
</rss>

