<?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 Can I write to stdin and read from stdout in the same data step? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Can-I-write-to-stdin-and-read-from-stdout-in-the-same-data-step/m-p/328971#M73535</link>
    <description>&lt;P&gt;SAS 9.3 on Windows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have an external program that can both read from stdin and write to stdout in the same call.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The&amp;nbsp;external program is something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;exe keyfile&amp;nbsp;col1 col2 output_type [input_file | -] [output_file | -]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If input file and output file are not specified (or "-"), the program reads from and writes to stdin and stdout respectively.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The transient external files can contain sensitive information, plus they can be big. &amp;nbsp;If I could avoid the external files that would be a nice-to-have.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options noquotelenmax; 
filename tmp pipe "&amp;amp;cryptPgmEncrypt &amp;amp;cryptKeysDir\&amp;amp;cryptKeyFile 1 10 OVERRIDE";
options quotelenmax;  

data _null_;
   set mysasdataset;
   infile tmp sharebuffers;  * also tried w/o sharebuffers ;
   file tmp;
   put @1 recnum z10.;
   input @1 recnum_e $13.;
   put recnum_e;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But SAS didn't like that:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;ERROR: Random access not allowed.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;I'm not seeing the random access - it looks pretty sequential to me.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;I also tried this, same results:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   infile tmp end=eof2;
   file tmp;
   do until (eof1);
      set mysasdataset end=eof1;
      put @1 recnum z10.;
   end;
   do until (eof2);
      input;
      put _infile_;
   end;
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Wondering if the output would be buffered somehow?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Of course, the workaround is to use external files, but I'm curious if the above can be done (on Windows)?&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 01 Feb 2017 22:31:56 GMT</pubDate>
    <dc:creator>ScottBass</dc:creator>
    <dc:date>2017-02-01T22:31:56Z</dc:date>
    <item>
      <title>Can I write to stdin and read from stdout in the same data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-I-write-to-stdin-and-read-from-stdout-in-the-same-data-step/m-p/328971#M73535</link>
      <description>&lt;P&gt;SAS 9.3 on Windows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have an external program that can both read from stdin and write to stdout in the same call.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The&amp;nbsp;external program is something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;exe keyfile&amp;nbsp;col1 col2 output_type [input_file | -] [output_file | -]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If input file and output file are not specified (or "-"), the program reads from and writes to stdin and stdout respectively.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The transient external files can contain sensitive information, plus they can be big. &amp;nbsp;If I could avoid the external files that would be a nice-to-have.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options noquotelenmax; 
filename tmp pipe "&amp;amp;cryptPgmEncrypt &amp;amp;cryptKeysDir\&amp;amp;cryptKeyFile 1 10 OVERRIDE";
options quotelenmax;  

data _null_;
   set mysasdataset;
   infile tmp sharebuffers;  * also tried w/o sharebuffers ;
   file tmp;
   put @1 recnum z10.;
   input @1 recnum_e $13.;
   put recnum_e;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But SAS didn't like that:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;ERROR: Random access not allowed.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;I'm not seeing the random access - it looks pretty sequential to me.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;I also tried this, same results:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   infile tmp end=eof2;
   file tmp;
   do until (eof1);
      set mysasdataset end=eof1;
      put @1 recnum z10.;
   end;
   do until (eof2);
      input;
      put _infile_;
   end;
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Wondering if the output would be buffered somehow?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Of course, the workaround is to use external files, but I'm curious if the above can be done (on Windows)?&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 22:31:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-I-write-to-stdin-and-read-from-stdout-in-the-same-data-step/m-p/328971#M73535</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2017-02-01T22:31:56Z</dc:date>
    </item>
    <item>
      <title>Re: Can I write to stdin and read from stdout in the same data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-I-write-to-stdin-and-read-from-stdout-in-the-same-data-step/m-p/328978#M73538</link>
      <description>&lt;P&gt;I didn't see random access either, but you are reading and writing to the same pipe.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Look at what happens with this&amp;nbsp;program, which (in the second data step) reads and writes to the same external disk file.&amp;nbsp;&amp;nbsp;In particular it&amp;nbsp;reads the first half of the file, and writes only half of that - after modifying content.&amp;nbsp; It neither reads nor writes the second half of the file.&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 _null_;
  file 'c:\temp\t.txt';
  do n=100 to 119; put n z3.; end;
run;

data _null_;
  infile 'c:\temp\t.txt';
  file 'c:\temp\t.txt';
  input txt $3. ;
  substr(txt,1,1)='X';
  if mod(_n_,2)=0 then put txt;
  if _n_&amp;gt;=10 then stop;
run;

data _null_;
  infile 'c:\temp\t.txt';
  input txt $3.;
  put _n_=z2. txt;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Take a look at the difference between the before and after for the file t.txt.&amp;nbsp; You'll see 20 lines in each , with lines 2,4,6,8,10 modified, and others left as is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, since the PUT statement is only executed 5 times, how did it know to write those five records in &lt;U&gt;&lt;EM&gt;alternative locations&lt;/EM&gt;&lt;/U&gt;, rather than consecutive locations?&amp;nbsp;It has to be because a kind of&amp;nbsp;"random" access to those lines was being supported, i.e. the INPUT statement advanced the destination of the PUT statement since the same file is being read and written.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe you wouldn't want to call that random access, but then why are all the other lines left in the file?&amp;nbsp; After all, if the destination of PUT was a new file it would have only 5 lines, not 20.&amp;nbsp; Lines are effectively being logically "inserted".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even if the file is stored as a sequential file, if it's the object of both INFILE and FILE, then apparently SAS needs to think of it as having random access.&amp;nbsp; Who knew?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I guess this "random access" is&amp;nbsp;supported for a single FILE used as both INPUT source and PUT target, but apparently a single PIPE is&amp;nbsp;not similarly supported.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe you need two pipes for the external process - one to designate the&amp;nbsp; process'&amp;nbsp; stdin and one for its stdout.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 05:36:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-I-write-to-stdin-and-read-from-stdout-in-the-same-data-step/m-p/328978#M73538</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-01T05:36:19Z</dc:date>
    </item>
  </channel>
</rss>

