<?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: keep every nth record while reading in binary data file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368769#M87967</link>
    <description>&lt;P&gt;Until somebody else comes up with a better idea ... there are other ways to skip over lines when inputting data.&amp;nbsp; One possibility:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;input //// buffer1 RB4&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; Chain1 RB8&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; Chain2 RB8&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;Another:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;input;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;input;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;input;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;input;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;input buffer1 RB4. Chain1 RB8. Chain2 RB8.;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;While this looks silly, it's easier to adapt if you want to read every 100th line instead of every 5th line.&amp;nbsp; For example:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;do i=1 to 99;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;&amp;nbsp;&amp;nbsp; input;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;end;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;input buffer1 RB4. Chain1 RB8. Chain2 RB8.;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 20 Jun 2017 15:00:58 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-06-20T15:00:58Z</dc:date>
    <item>
      <title>keep every nth record while reading in binary data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368764#M87966</link>
      <description>&lt;P&gt;Suppose I have a very large (multiple GB) binary data file, that I'm want to read into SAS (using 9.3 at the moment). I don't want to read in the entire file, but, rather, I want to read in every nth record.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For simple data files (ASCII), this can be done fairly easily using #. For example, suppose I have some file called test.dat containin g 3 data files/columns (x,y and z). The following reads in every 5th record:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename in 'c:\users\userDesktop\test.dat';

data hold; infile in;
&amp;nbsp; &amp;nbsp;input #5 &amp;nbsp;x y z;
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;Works fine. But, for some reason, if test.dat is a binary file, this approach doesn't seem to work. To read in the particular binary data file, I use something like the following input syntax:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;input buffer1 RB4. Chain1 RB8. Chain2&amp;nbsp;RB8.;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Works fine. However,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;input #5 buffer1 RB4. Chain1 RB8. Chain2 RB8.;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;doesn't work as expected (or really, at all...).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I know I could probably do this using 2 steps: (i) read in the full binary file, and then (ii) use some 'tricks' with subsetting the data to keep only every nth record, but the original file is so large I'm trying to avoid having to read the entire thin in in the first place.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Suggestions/pointers to the obvious are welcomed.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 14:48:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368764#M87966</guid>
      <dc:creator>cooch17</dc:creator>
      <dc:date>2017-06-20T14:48:23Z</dc:date>
    </item>
    <item>
      <title>Re: keep every nth record while reading in binary data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368769#M87967</link>
      <description>&lt;P&gt;Until somebody else comes up with a better idea ... there are other ways to skip over lines when inputting data.&amp;nbsp; One possibility:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;input //// buffer1 RB4&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; Chain1 RB8&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; Chain2 RB8&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;Another:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;input;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;input;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;input;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;input;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;input buffer1 RB4. Chain1 RB8. Chain2 RB8.;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;While this looks silly, it's easier to adapt if you want to read every 100th line instead of every 5th line.&amp;nbsp; For example:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;do i=1 to 99;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;&amp;nbsp;&amp;nbsp; input;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;end;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;input buffer1 RB4. Chain1 RB8. Chain2 RB8.;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 15:00:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368769#M87967</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-06-20T15:00:58Z</dc:date>
    </item>
    <item>
      <title>Re: keep every nth record while reading in binary data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368776#M87968</link>
      <description>&lt;P&gt;Neat, except it doesn't work with binary files -- following from the log:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The '/' INPUT/PUT statement option is inconsistent with binary mode I/O. The execution of&lt;BR /&gt;the DATA STEP is being terminated.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 15:12:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368776#M87968</guid>
      <dc:creator>cooch17</dc:creator>
      <dc:date>2017-06-20T15:12:52Z</dc:date>
    </item>
    <item>
      <title>Re: keep every nth record while reading in binary data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368784#M87971</link>
      <description>&lt;P&gt;Are you using RECFM=N to read the file. If yes there are no records as the file is just a long string of bytes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does the file have just 3 data values and this group of values repeats till the end of the file, or are their many more different data values. Do you have "record layout" of the data values?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you run the following code (with your file) what does the log file look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  infile "yourfile" ;
  input char $1.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Jun 2017 15:28:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368784#M87971</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2017-06-20T15:28:00Z</dc:date>
    </item>
    <item>
      <title>Re: keep every nth record while reading in binary data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368792#M87973</link>
      <description>&lt;P&gt;Here is how I read in this particular data file - &amp;amp;MCMCfile, and recl are macro variables set earlier in the program. Note I'm using recfm=N - but I explicitly set the linesize, which 'forces' records (i.e., splits the big string into discrete lines):&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 MCMC;
	infile &amp;amp;MCMCfile linesize=&amp;amp;recl recfm=N;
	input buffer1 RB4. Chain1 RB8. Chain2 RB8. Chain3 RB8. buffer2 RB4.;&lt;BR /&gt;&lt;BR /&gt;drop buffer1 buffer2;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Each line (record) contains output from an MCMC sampler at each step (so, same number of 'variables' per line, different values for each variable).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As per OP, the following 2-step procedure works: (i) pull in the big binary file (into a data set I call MCMC), then (ii) subset it, keeping everynth record. For step (ii), I simply use the following (as one of a couple of approaches that probably would work), where thin is a macro variable I set earlier in the program.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;***************************
* thin the data set....   *
***************************;

data MCMC;
 do point = &amp;amp;thin to nobs by &amp;amp;thin;
    set MCMC point=point nobs=nobs;
	   output;
	end;
  stop;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works fine, but as per OP, seems annoyingly inefficient, since I'm basically taking 2 steps for something I'd like to do in 1 step (during the infile stage).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 15:42:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368792#M87973</guid>
      <dc:creator>cooch17</dc:creator>
      <dc:date>2017-06-20T15:42:13Z</dc:date>
    </item>
    <item>
      <title>Re: keep every nth record while reading in binary data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368808#M87975</link>
      <description>&lt;P&gt;If you know that the binary file has a fixed record length, use recfm=f and a proper lrecl. Then you can read every record and only do the output when mod(_n_,5) = 0.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 16:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368808#M87975</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-20T16:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: keep every nth record while reading in binary data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368816#M87976</link>
      <description>&lt;P&gt;Just use a conditional OUTPUT statement. &amp;nbsp;It doesn't really add much to "read" every record since you are not doing direct access to the file anyway. You could use +4 to skip reading the fields you are dropping.&lt;/P&gt;
&lt;P&gt;So to keep the 1st, 6th, 11th, ....record you could do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MCMC;
  infile &amp;amp;MCMCfile linesize=&amp;amp;recl recfm=N;
  input +4 Chain1 RB8. Chain2 RB8. Chain3 RB8. +4 ;
  if mod(_n_,5) = 1 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 16:31:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368816#M87976</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-06-20T16:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: keep every nth record while reading in binary data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368987#M88012</link>
      <description>This worked perfectly -- I thought I'd tried something like this, but apparently, messed something up in the attempt. Thanks very much.</description>
      <pubDate>Wed, 21 Jun 2017 01:38:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-every-nth-record-while-reading-in-binary-data-file/m-p/368987#M88012</guid>
      <dc:creator>cooch17</dc:creator>
      <dc:date>2017-06-21T01:38:46Z</dc:date>
    </item>
  </channel>
</rss>

