<?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: PIPE Command Start of NEW File in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/650649#M195128</link>
    <description>&lt;P&gt;Here is a simplified example of the "dynamic pipe". It combines the use of PIPE, FILEVAR= and END= options of the infile statement. To simulate running an external command on a file, I used "cat".&lt;/P&gt;
&lt;P&gt;First, create some files to read:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set sashelp.class;
file '$HOME/class1.csv' dlm=',';
put name sex age;
run;

data _null_;
set sashelp.class;
file '$HOME/class2.csv' dlm=',';
put name sex age;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Create a list of filenames to process; this dataset might be created by the "ls" pipe you posted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filenames;
input fname $80.;
datalines;
$HOME/class1.csv
$HOME/class2.csv
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now we use this dataset to control the creation of a command for the PIPE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set filenames;
length fvar $200 name $8 sex $1;
fvar = "cat " !! trim(fname);
infile dummy pipe filevar=fvar end=done dlm=',';
/* since we have a "change of file" for every data stepiteration, here is the place to insert a special value */
name = "start";
output;
/* now read the result of the pipe in a loop */
do while (not done);
  input name sex age;
  output;
end;
drop fname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's see if this worked:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;name       sex    age

start               .
Alfred      M      14
Alice       F      13
Barbara     F      13
Carol       F      14
Henry       M      14
James       M      12
Jane        F      12
Janet       F      15
Jeffrey     M      13
John        M      12
Joyce       F      11
Judy        F      14
Louise      F      12
Mary        F      15
Philip      M      16
Robert      M      12
Ronald      M      15
Thomas      M      11
William     M      15
start               .
Alfred      M      14
Alice       F      13
Barbara     F      13
Carol       F      14
Henry       M      14
James       M      12
Jane        F      12
Janet       F      15
Jeffrey     M      13
John        M      12
Joyce       F      11
Judy        F      14
Louise      F      12
Mary        F      15
Philip      M      16
Robert      M      12
Ronald      M      15
Thomas      M      11
William     M      15
&lt;/PRE&gt;
&lt;P&gt;Voila! We've successfully inserted a value at the start of processing each separate file.&lt;/P&gt;</description>
    <pubDate>Tue, 26 May 2020 10:04:07 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-05-26T10:04:07Z</dc:date>
    <item>
      <title>PIPE Command Start of NEW File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/647925#M193944</link>
      <description>&lt;P&gt;Hi - I've used the PIPE command to import multiple files at once, which comes in quite handy. Is there any way, using PIPE, to tell whether a NEW file is encountered? Because what I have to do is take some action once a NEW file is imported. Example: using PIPE, I import 2 files. At the start and end of each file, I have to insert a value. My uncertainty is, how to detect when I reach a new file because the inserts have to be done each time a new file is encountered in the PIPE. Thanks for any tips.&lt;/P&gt;</description>
      <pubDate>Thu, 14 May 2020 21:32:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/647925#M193944</guid>
      <dc:creator>shl007</dc:creator>
      <dc:date>2020-05-14T21:32:16Z</dc:date>
    </item>
    <item>
      <title>Re: PIPE Command Start of NEW File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/647927#M193945</link>
      <description>FILENAME has EOF and EOV options to help with this now, if you can go that route instead of the PIPE option. Depends on how you define a 'new' file which I don't quite understand from your question.</description>
      <pubDate>Thu, 14 May 2020 21:46:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/647927#M193945</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-14T21:46:32Z</dc:date>
    </item>
    <item>
      <title>Re: PIPE Command Start of NEW File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/647935#M193950</link>
      <description>&lt;P&gt;It might help to show us how you are actually using the files, i.e. example code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 May 2020 22:45:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/647935#M193950</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-14T22:45:28Z</dc:date>
    </item>
    <item>
      <title>Re: PIPE Command Start of NEW File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/647936#M193951</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt; I've used the PIPE command to import multiple files at once&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Pipe runs a system command, it does not import files.&lt;/P&gt;
&lt;P&gt;Seeing your code would help understand what you are doing. Including the filename statement.&lt;/P&gt;</description>
      <pubDate>Thu, 14 May 2020 22:46:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/647936#M193951</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-05-14T22:46:35Z</dc:date>
    </item>
    <item>
      <title>Re: PIPE Command Start of NEW File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/647957#M193971</link>
      <description>&lt;P&gt;Depending on the command you are PIPEing, it's not likely, in the case of multiple files, that file end/file start information is available to SAS. &amp;nbsp; If so, you either have to modify the pipe to insert signals between the files, or else pipe the files one-at-a-time such that your program knows the source for each data record.&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 02:45:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/647957#M193971</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-05-15T02:45:42Z</dc:date>
    </item>
    <item>
      <title>Re: PIPE Command Start of NEW File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/647966#M193977</link>
      <description>&lt;P&gt;The PIPE receives an amorphous stream of bytes from the external command, it can only detect when the stream ends, but not when there is a change of files on the input side of the external command. If the files that are read have header lines, you can use that to detect a file change; if not, you need to structure your external command in a way that inserts markers.&lt;/P&gt;
&lt;P&gt;Alternatively, you could use a dynamic pipe, with a dataset of filenames and the filevar= option.&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 05:12:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/647966#M193977</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-05-15T05:12:52Z</dc:date>
    </item>
    <item>
      <title>Re: PIPE Command Start of NEW File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/648059#M194040</link>
      <description>I am invoking PIPE this way with a macro variable embedded to look for certain key word(s). I knew about the "END" option but wasn't aware there were also EOF and EOV options, but from Kurt's response above, it sounds like those won't do me any good, which explains why my testing has not been successful to date.&lt;BR /&gt;&lt;BR /&gt;Unfortunately, the files don't have any header line ... Kurt or anyone, can you elaborate on what you mean by dynamic pipe? I think I may already be using it because I am using filevar option to store the filename. So I can use the filevar option to see if the filename changes? Would I need a LAG function or something like it to compare previous to current value? I had tried LAG on the filevar variable, but SAS didn't like it.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;filename output pipe "ls -a /myfolder/subfolder/&amp;amp;other.*";</description>
      <pubDate>Fri, 15 May 2020 15:03:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/648059#M194040</guid>
      <dc:creator>shl007</dc:creator>
      <dc:date>2020-05-15T15:03:07Z</dc:date>
    </item>
    <item>
      <title>Re: PIPE Command Start of NEW File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/648060#M194041</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/216466"&gt;@shl007&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;BR /&gt;filename output pipe "ls -a /myfolder/subfolder/&amp;amp;other.*";&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This pipe creates only one stream of &lt;EM&gt;filenames&lt;/EM&gt;, not the contents of the files.&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 15:06:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/648060#M194041</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-05-15T15:06:58Z</dc:date>
    </item>
    <item>
      <title>Re: PIPE Command Start of NEW File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/648062#M194042</link>
      <description>&lt;P&gt;The pipe you showed is good to create a dataset with filenames, but if you want to read those files without any non-SAS processing in between, you can use the same wildcard, and the filename= option of the INFILE statement to get the name of the file that is currently read; a change in that variable signals a switch between input files.&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 15:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/648062#M194042</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-05-15T15:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: PIPE Command Start of NEW File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/650649#M195128</link>
      <description>&lt;P&gt;Here is a simplified example of the "dynamic pipe". It combines the use of PIPE, FILEVAR= and END= options of the infile statement. To simulate running an external command on a file, I used "cat".&lt;/P&gt;
&lt;P&gt;First, create some files to read:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set sashelp.class;
file '$HOME/class1.csv' dlm=',';
put name sex age;
run;

data _null_;
set sashelp.class;
file '$HOME/class2.csv' dlm=',';
put name sex age;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Create a list of filenames to process; this dataset might be created by the "ls" pipe you posted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filenames;
input fname $80.;
datalines;
$HOME/class1.csv
$HOME/class2.csv
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now we use this dataset to control the creation of a command for the PIPE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set filenames;
length fvar $200 name $8 sex $1;
fvar = "cat " !! trim(fname);
infile dummy pipe filevar=fvar end=done dlm=',';
/* since we have a "change of file" for every data stepiteration, here is the place to insert a special value */
name = "start";
output;
/* now read the result of the pipe in a loop */
do while (not done);
  input name sex age;
  output;
end;
drop fname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's see if this worked:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;name       sex    age

start               .
Alfred      M      14
Alice       F      13
Barbara     F      13
Carol       F      14
Henry       M      14
James       M      12
Jane        F      12
Janet       F      15
Jeffrey     M      13
John        M      12
Joyce       F      11
Judy        F      14
Louise      F      12
Mary        F      15
Philip      M      16
Robert      M      12
Ronald      M      15
Thomas      M      11
William     M      15
start               .
Alfred      M      14
Alice       F      13
Barbara     F      13
Carol       F      14
Henry       M      14
James       M      12
Jane        F      12
Janet       F      15
Jeffrey     M      13
John        M      12
Joyce       F      11
Judy        F      14
Louise      F      12
Mary        F      15
Philip      M      16
Robert      M      12
Ronald      M      15
Thomas      M      11
William     M      15
&lt;/PRE&gt;
&lt;P&gt;Voila! We've successfully inserted a value at the start of processing each separate file.&lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2020 10:04:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PIPE-Command-Start-of-NEW-File/m-p/650649#M195128</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-05-26T10:04:07Z</dc:date>
    </item>
  </channel>
</rss>

