<?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: concatenating external files based on file name in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262679#M7219</link>
    <description>&lt;P&gt;Hi Jan,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actuallt it is a very SASy question because here I realize that I will need some sort of a macro but I can't figure out how to write and would greatly appreciate a first push!!&amp;nbsp;&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>Sat, 09 Apr 2016 14:43:46 GMT</pubDate>
    <dc:creator>ilikesas</dc:creator>
    <dc:date>2016-04-09T14:43:46Z</dc:date>
    <item>
      <title>concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262671#M7216</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;In a previous question I asked how to concatenate files and from the answers given realized that it can be done directly with command prompt with : type "file1" "file2" ... "filen" &amp;gt; "allfiles"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But suppose that I want to concatenate files based on their names. For example, I have the files f1,f2, fh1, fh2, is it possible to dynamically concatenate all the f's together and the fh's together etc?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2016 13:44:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262671#M7216</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-04-09T13:44:00Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262672#M7217</link>
      <description>&lt;P&gt;How about "type fh*.* &amp;gt; allfhfiles". But don't you have a SAS question? Those are more fun.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2016 13:56:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262672#M7217</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2016-04-09T13:56:22Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262675#M7218</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12982"&gt;@ilikesas﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this is a recurring task, you could write a SAS macro which takes the common prefix (e.g. fh), the input and output folder names, the name of the output file and possibly instructions regarding the numbering as parameters. Then you could call the macro once for each set of files to be concatenated and it would build and execute the appropriate X statement.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2016 14:10:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262675#M7218</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-09T14:10:29Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262679#M7219</link>
      <description>&lt;P&gt;Hi Jan,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actuallt it is a very SASy question because here I realize that I will need some sort of a macro but I can't figure out how to write and would greatly appreciate a first push!!&amp;nbsp;&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>Sat, 09 Apr 2016 14:43:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262679#M7219</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-04-09T14:43:46Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262681#M7220</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do I have first to import the names of the files? I know how to import files into SAs but here I don't need to imprt the actual files, here I use SAS as an intermediary sorter. Its just that I have difficulty starting and would greatly appreciate if you could give me some simple code as a hint.&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>Sat, 09 Apr 2016 14:52:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262681#M7220</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-04-09T14:52:56Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262682#M7221</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12982"&gt;@ilikesas﻿&lt;/a&gt; no problem. I have done many a file- and directory manipulation in my life. What proved the most efficient, reliable and auditable (a big thing in the branches I work) is the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Design a dataset that keeps track of files and their properties, status in the process (new, done, error, ...) with corrsponding timestamps etc&lt;/LI&gt;
&lt;LI&gt;Run a macro that generates a directory listing. Update above dataset and det4ermine what files need work (status new) based on whatever criteria you desire.&lt;/LI&gt;
&lt;LI&gt;In a datastep you can eg. read files with names like fh* by using
&lt;UL&gt;
&lt;LI&gt;a set statement of above dataset; WHERE status=new&lt;/LI&gt;
&lt;LI&gt;Read the selected files using the INPUT/PUT statements and the FILEVAR option. This way you can have a data driven process of reading and writing files in a single datastep. You can also use CALL EXECUTE on each individual file to run macro's using the name as a parameter.&lt;/LI&gt;
&lt;LI&gt;The datastep creates a table of files processed.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
Use the generated dataset to update the dataset used to keep track of work done. Update status to whatever is next.&lt;/UL&gt;
&lt;P&gt;Many macros exist on the web that create directory listings. The gist of my suggestion is using the powerful FILEVAR option to cycle through a list of files. &lt;A href="https://support.sas.com/techsup/technote/ts581.pdf" target="_self"&gt;TS-DOC 581&lt;/A&gt; gives a good idea of the possibilities.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2016 14:56:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262682#M7221</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2016-04-09T14:56:06Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262683#M7222</link>
      <description>&lt;P&gt;Do you care the order that the files are concatenated? &amp;nbsp;Are you just interested in concatenating the raw source files or are you actually interested in generating a concatenated DATASET?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you just want to use SAS to&amp;nbsp;copy the raw files together and you can use a simple (single wildcard) pattern to match the file names&amp;nbsp;then a simple date step will do.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   infile 'fh*.txt' ;
   file 'all_fh.txt' ;
   input;
   put _infile_;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it is more complex then make a dataset with the list of filenames and then use that to drive the step that reads and copies the files.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filelist ;
   infile 'dir /b f*.txt' pipe truncover ;
   input filename $256. ;
   if filename =: 'fh' then do; 
     target = 'fh_files.txt';
     number = input(substr(scan(filename,1,'.'),3),32.);
     output;
   end;
   else if filename =: 'f' then do; 
     target = 'f_files.txt';
     number = input(substr(scan(filename,1,'.'),2),32.);
     output;
   end;
run;
proc sort ; by target number ; run;
data _null_;
   set filelist ;
   infile source filevar=filename end=eof ;
   file target filevar=target ;
   do while (not eof);
     input;
     put _infile_;
  end;
run;

   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2016 14:59:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262683#M7222</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-04-09T14:59:03Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262689#M7223</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12982"&gt;@ilikesas﻿&lt;/a&gt;&amp;nbsp;How is it a SAS question? You can wildcard the command and execute from SAS I suppose? It seems very OS related, witht he exception of calling the command from SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're concatenating so you can read the files via SAS that's not required since you can use the FILEVAR option in an infile statement.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2016 15:49:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262689#M7223</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-09T15:49:28Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262691#M7225</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;its true that just concateneating the files is purely OS (you actually showed me how to do it in my previous question!)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But here I need to dynamically concatenate based on file names and I guess that here SAS is the program that actually does the management of which files get concatenated together (and personally to me it seems to be the only way I can think of, but again I am a relative beginner and my knowledge is somewhat limited...)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2016 16:31:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262691#M7225</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-04-09T16:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262693#M7226</link>
      <description>Wildcards in both SAS are valid as someone indicated. &lt;BR /&gt;&lt;BR /&gt;Use the SCAN funtion within Toms code to extract just the filename.</description>
      <pubDate>Sat, 09 Apr 2016 16:33:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262693#M7226</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-09T16:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262697#M7227</link>
      <description>&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; filelist &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token statement"&gt;infile&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;'dir /b *.txt'&lt;/SPAN&gt; pipe truncover &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;filename&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;256&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/CODE&gt;run;&lt;/PRE&gt;
&lt;P&gt;The snippet above (from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom﻿&lt;/a&gt;) creates the&amp;nbsp;&amp;nbsp;file list dataset. H&lt;/P&gt;
&lt;P&gt;His code is correct, but you may want to start from the above to understand what's going on.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2016 18:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262697#M7227</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-09T18:01:45Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262708#M7228</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;here is the code that I did by modifying Tom's code and I ALMOST got what I wanted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filelist ;
   infile 'dir C:\files\ /b *.txt' pipe truncover ;
   input filename $256. ;
directory = 'C:\files\';
file_path=directory || filename; /*create the pathname */
name=substr(filename, 1, length(filename)-4); /*delete the .txt*/
file_number = compress(name,'','A'); /*extract the file number*/
unique_name = compress(name, file_number); /*extract the unique name*/
target_extension= '_all.txt';
target = directory || unique_name || target_extension;
run;


data _null_;
   set filelist ;
   infile source filevar=file_path end=eof ;
   file target filevar=target ;
   do while (not eof);
     input;
     put _infile_;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The only minor problem is that when I execute the code I get my 2 files (one concatenating f1 and f2, and one concatenating fh1 and fh2) but they are not in txt format, to open them I actually need to choose the program with which to open.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;its probably related to :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;target_extension= '_all.txt';
target = directory || unique_name || target_extension;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;because when I looked at my SAS table "filelist" the target_extension is not fully concatenated to the other part, and I get something like:&lt;/P&gt;
&lt;P&gt;C:\files\f&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_all.txt&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for the variable "target"&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2016 22:00:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262708#M7228</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-04-09T22:00:50Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating external files based on file name</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262709#M7229</link>
      <description>&lt;P&gt;Don't use the double pipe for concatenation use Catt or Cats function. For one they remove extra spaces and another is they deal with conversion from numeric to character so you can avoid explicitly converting types.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2016 22:53:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/concatenating-external-files-based-on-file-name/m-p/262709#M7229</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-09T22:53:15Z</dc:date>
    </item>
  </channel>
</rss>

