<?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: Reading .dat file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343872#M78976</link>
    <description>&lt;P&gt;Use PROC TRANSPOSE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=want ;
  by id ;
  id code ;
  var value ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 23 Mar 2017 20:49:39 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-03-23T20:49:39Z</dc:date>
    <item>
      <title>Reading .dat file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343801#M78951</link>
      <description>&lt;P&gt;I could use help reading a file that looks like this (below)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first six columns are irrelevant.&amp;nbsp; The ID starts at column 7, and is 9 digits long (7-15). At 16-19 is irrelevent.&amp;nbsp; The variable titles (or Item Codes, variable codes) are four digits long beginning at Column 20-23, followed by the value associated with the variable code in columns 24-33 (9 digits long); followed by another Var item code (length 4), followed by the value of item code (9 columns)… repeated 4 times (four item codes per line of data). &amp;nbsp;&amp;nbsp;Each ID (7-15) has up to 191 possible item codes (variables) and can take up several lines in the data file. The following IDs are truncated (varied in length). &amp;nbsp;No important information existes beyond column 72 (111,211 are irrelevent). &amp;nbsp; Any ideas on reading this. &amp;nbsp;My code doesn't work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data batchX;&lt;BR /&gt; array ic (1:191) p0001-p0191; /* These are the variable names (Item Codes) */&lt;BR /&gt; infile "F:\DATA\batch01.dat" lrecl=85 missover;&lt;BR /&gt; input @7 ID 9. @; &lt;BR /&gt;&amp;nbsp;x=20;&lt;BR /&gt;&amp;nbsp;do while (x&amp;lt;72);&lt;BR /&gt;&amp;nbsp; &amp;nbsp;input @x code 4. @;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;x=x+4;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;if code gt 0 then do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;input @x ic{code} 9. @; &lt;BR /&gt;&amp;nbsp; &amp;nbsp;x=x+9;&lt;BR /&gt;&amp;nbsp;end;&lt;BR /&gt; end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;19000190703674001010170000000006017900000000201800000000020181000000003 211&lt;BR /&gt;19000190703674001010182000000002018300000000201840000000020185000000003 211&lt;BR /&gt;19000190703674001010186000000002018700000000201880000000020189000000002 211&lt;BR /&gt;190001907036740010101900000000020191000000001 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 211&lt;BR /&gt;19000194754213001010001000000004000200000000100030000000100004000002015 111&lt;BR /&gt;19000194754213001010005000002000000600000018300070000000010008000000300 211&lt;BR /&gt;19000194754213001010009000000012001000000000200110000000500012000000015 211&lt;BR /&gt;19000194754213001010014000000006001500000000500230000000010026000000001 211&lt;BR /&gt;19000194754213001010165000000001016700000000301730000000090179000000002 211&lt;BR /&gt;19000194754213001010184000000002018500000000101860000000030187000000003 211&lt;BR /&gt;19000194754213001010188000000003018900000000201900000000020191000000001 211&lt;BR /&gt;1900019475421300101 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 211&lt;BR /&gt;19000195700697001010001000000003000200000000201920000000140191000000002 111&lt;BR /&gt;1900019570069700101 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;211&lt;BR /&gt;19000130127654001010001000000001000200000000201690000000050179000000003 111&lt;BR /&gt;19000130127654001010180000000003018100000000301820000000030183000000003 211&lt;BR /&gt;19000130127654001010184000000003018500000000301860000000010187000000001 211&lt;BR /&gt;19000130127654001010188000000001018900000000101900000000010191000000002 211&lt;BR /&gt;1900013012765400101 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 211&lt;BR /&gt;19000190701425001010001000000005000200000000201920000000140191000000002 111&lt;BR /&gt;1900019070142500101 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 211&lt;BR /&gt;19000186716534001010001000000006000200000000201650000000010167000000003 111&lt;BR /&gt;19000186716534001010169000000005017900000000301800000000020181000000002 211&lt;BR /&gt;19000186716534001010182000000002018300000000201840000000010185000000001 211&lt;BR /&gt;19000186716534001010186000000002018700000000201880000000020189000000003 211&lt;BR /&gt;190001867165340010101900000000030191000000001 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 211&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2017 17:58:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343801#M78951</guid>
      <dc:creator>jakestat</dc:creator>
      <dc:date>2017-03-23T17:58:38Z</dc:date>
    </item>
    <item>
      <title>Re: Reading .dat file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343815#M78956</link>
      <description>&lt;P&gt;What would the desired output look like for that data? Are your codes actually numeric or would a leading zero be needed later on in the processing? For instance, if I count correctly, on the first row of example data you have 0601 in columns 32-35. Do you need "0601" or 601?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a potential of 191 codes I would tend to think of creating an array of boolean values that indicate whether that code is present or not. Or if the number of times a code appears within and ID value is critical then a count. Which would turn this, at least partially into a table look up type problem as you grab pieces of the input line past column 19. If so then may help to provide a list of all of the possible legal values, the 191 codes.&lt;/P&gt;
&lt;P&gt;And in you actual data file are the "blanks" in the line, such as on line4 actually TAB characters or spaces? When I paste your example data into a text editor I get tab behavior. Which bit would be important to know.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And considering the what I think of as a pretty stupid file format this seems likely to be medical data of some form.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2017 18:32:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343815#M78956</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-23T18:32:03Z</dc:date>
    </item>
    <item>
      <title>Re: Reading .dat file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343829#M78957</link>
      <description>&lt;P&gt;I would read it into a vertical table. &amp;nbsp;So this program will read the CODE/VALUE pairs as separate observations. It adds the variable N to count which position it was in.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
 input id 7-15 @20 @ ;
 retain n ;
 if id ne lag(id) then n=1 ;
 do n=n to n+3 ;
   input code 4. value 9. @;
   if n(code,value) &amp;gt; 0 then output;
 end;
cards;
19000190703674001010170000000006017900000000201800000000020181000000003 211
19000190703674001010182000000002018300000000201840000000020185000000003 211
19000190703674001010186000000002018700000000201880000000020189000000002 211
190001907036740010101900000000020191000000001                           211
19000194754213001010001000000004000200000000100030000000100004000002015 111
19000194754213001010005000002000000600000018300070000000010008000000300 211
19000194754213001010009000000012001000000000200110000000500012000000015 211
19000194754213001010014000000006001500000000500230000000010026000000001 211
19000194754213001010165000000001016700000000301730000000090179000000002 211
19000194754213001010184000000002018500000000101860000000030187000000003 211
19000194754213001010188000000003018900000000201900000000020191000000001 211
1900019475421300101                                                     211
19000195700697001010001000000003000200000000201920000000140191000000002 111
1900019570069700101                                                     211
19000130127654001010001000000001000200000000201690000000050179000000003 111
19000130127654001010180000000003018100000000301820000000030183000000003 211
19000130127654001010184000000003018500000000301860000000010187000000001 211
19000130127654001010188000000001018900000000101900000000010191000000002 211
1900013012765400101                                                     211
19000190701425001010001000000005000200000000201920000000140191000000002 111
1900019070142500101                                                     211
19000186716534001010001000000006000200000000201650000000010167000000003 111
19000186716534001010169000000005017900000000301800000000020181000000002 211
19000186716534001010182000000002018300000000201840000000010185000000001 211
19000186716534001010186000000002018700000000201880000000020189000000003 211
190001867165340010101900000000030191000000001                           211
;;;;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Mar 2017 19:30:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343829#M78957</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-03-23T19:30:35Z</dc:date>
    </item>
    <item>
      <title>Re: Reading .dat file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343830#M78958</link>
      <description>&lt;P&gt;If your example data are correct, then your codes actually go up to 192. Guesstimating that your example data represent six subjects, then the following (I think) does what you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data batchX;
  array ic (1:192) p0001-p0192; /* These are the variable names (Item Codes) */
  infile "F:\DATA\batch01.dat" lrecl=85 end=eof;
  retain ic;
  input @7 ID 9. @; 
  if id ne lag(id) and _n_ gt 1 then output;
  x=20;
  do while (x&amp;lt;72);
    input @x code 4. @;
    x=x+4;
    if code gt 0 then do;
      input @x ic{code} 9. @; 
      x=x+9;
    end;
  end;
  input;
  if eof then output;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2017 19:27:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343830#M78958</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-23T19:27:06Z</dc:date>
    </item>
    <item>
      <title>Re: Reading .dat file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343832#M78960</link>
      <description>&lt;P&gt;Ballardw, the end result will be a dataset with an ID, and 191 vars for a total of 192 columns. &amp;nbsp;There hundreds of IDs. And an unknown number of batches waiting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the var list will be p0001 through p0191.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The value of 6 that you are referring to in column 32 belongs to the var code 0170 in columns 20-23&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2017 19:30:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343832#M78960</guid>
      <dc:creator>jakestat</dc:creator>
      <dc:date>2017-03-23T19:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: Reading .dat file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343850#M78969</link>
      <description>&lt;P&gt;Tom, it reads in great. But I can't get it to transpose so that the 'code' becomes the header for row 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help would be appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;jake&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2017 20:19:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343850#M78969</guid>
      <dc:creator>jakestat</dc:creator>
      <dc:date>2017-03-23T20:19:25Z</dc:date>
    </item>
    <item>
      <title>Re: Reading .dat file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343857#M78970</link>
      <description>&lt;P&gt;Art, I'm getting an ERROR: Array subscript out of range at line 32, column 16.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do you understand the reason?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2017 20:29:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343857#M78970</guid>
      <dc:creator>jakestat</dc:creator>
      <dc:date>2017-03-23T20:29:35Z</dc:date>
    </item>
    <item>
      <title>Re: Reading .dat file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343872#M78976</link>
      <description>&lt;P&gt;Use PROC TRANSPOSE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=want ;
  by id ;
  id code ;
  var value ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Mar 2017 20:49:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343872#M78976</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-03-23T20:49:39Z</dc:date>
    </item>
    <item>
      <title>Re: Reading .dat file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343890#M78986</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/2434"&gt;@jakestat&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Art, I'm getting an ERROR: Array subscript out of range at line 32, column 16.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do you understand the reason?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thank you!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Most likely is that somehow a value greater than 192 or less than 1 ended up in the CODE position.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose the code was supposed to be 107 and some accidentally typed a 1007 for the code value. Then the program looks for an array element 1007 but the array has a maximum of 192 spaces assigned. So the array index of 1007 is greater than 192 and you get that message. Similar if the value is 0000, 0 was not defined as an expected index for this array.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2017 21:46:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343890#M78986</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-23T21:46:13Z</dc:date>
    </item>
    <item>
      <title>Re: Reading .dat file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343892#M78987</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/2434"&gt;@jakestat&lt;/a&gt;: your error message should have also included the value of _n_ where the error occured. Knowing which record where the discrepancy existed should make it easy to discover what the offending value was&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I'd mentioned in my original post, that was how I discovered the code 192 in your example data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2017 21:52:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-dat-file/m-p/343892#M78987</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-23T21:52:08Z</dc:date>
    </item>
  </channel>
</rss>

