<?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 a complicated text file into a SAS data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973091#M377644</link>
    <description>If this is collected by one person, maybe it's easier to direct her to store the data in a more readable format. &lt;BR /&gt;If this data is entered manually it's error prone.</description>
    <pubDate>Fri, 22 Aug 2025 10:41:35 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2025-08-22T10:41:35Z</dc:date>
    <item>
      <title>Reading a complicated text file into a SAS data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973049#M377638</link>
      <description>&lt;P&gt;A friend of mine asked me a question that I could not answer. She's collecting hospital data in a Notepad file (argh!!!!) and wants to read it into a SAS data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've attached records from two hospitals (data were made up just to show what's needed): Cons.txt. Below is how her data set of 13 observations should look like.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Site&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ID&amp;nbsp; &amp;nbsp;Code&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; fts&amp;nbsp; &amp;nbsp;Heal pc&lt;BR /&gt;BTP04 1&amp;nbsp; &amp;nbsp; &amp;nbsp;xys&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; H9&amp;nbsp; &amp;nbsp;322&amp;nbsp; &amp;nbsp;11&lt;BR /&gt;BTP04 2&amp;nbsp; &amp;nbsp; &amp;nbsp;v3bwwwr15&amp;nbsp; &amp;nbsp;m2&amp;nbsp; &amp;nbsp;95&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;BR /&gt;BTP04 3&amp;nbsp; &amp;nbsp; &amp;nbsp;qd23&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;H4&amp;nbsp; &amp;nbsp;228&amp;nbsp; &amp;nbsp; 3&lt;BR /&gt;BTP04 4&amp;nbsp; &amp;nbsp; FRt5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m3&amp;nbsp; &amp;nbsp; 142&amp;nbsp; &amp;nbsp; 3&lt;BR /&gt;BTP04 5&amp;nbsp; &amp;nbsp; p002Ft&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;H4&amp;nbsp; &amp;nbsp; 361&amp;nbsp; &amp;nbsp; 9&lt;BR /&gt;BTP04 6&amp;nbsp; &amp;nbsp; NNNz13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; d1&amp;nbsp; &amp;nbsp; &amp;nbsp; 48&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;RRR02 1&amp;nbsp; &amp;nbsp; qus&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; H7&amp;nbsp; 204&amp;nbsp; &amp;nbsp;11&lt;BR /&gt;RRR02 2&amp;nbsp; &amp;nbsp;w752&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m3&amp;nbsp; &amp;nbsp; 95&amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;BR /&gt;RRR02 3&amp;nbsp; &amp;nbsp;rsss&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PT&amp;nbsp; 228&amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;BR /&gt;RRR02 4&amp;nbsp; 4562&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;m3&amp;nbsp; &amp;nbsp;142&amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;BR /&gt;RRR02 5&amp;nbsp; 4862&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;H4&amp;nbsp; &amp;nbsp;361&amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;BR /&gt;RRR02 6&amp;nbsp; bVf6RW&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; d1&amp;nbsp; &amp;nbsp; 122&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;RRR02 7&amp;nbsp; YyPpk8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; M3&amp;nbsp; 100&amp;nbsp; &amp;nbsp;12&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that the variable "Site" is captured from the first line of data for each hospital.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Andrew&lt;/P&gt;</description>
      <pubDate>Thu, 21 Aug 2025 18:19:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973049#M377638</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2025-08-21T18:19:08Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a complicated text file into a SAS data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973053#M377639</link>
      <description>&lt;P&gt;So are you saying you want help reading data that looks like the lines in your attached file?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;BTP04  Patients      Mags 1=23 2=15 3=9 4=6 5=4
___________________________________________________________________________________
ID   Code              fts     |LOS|BH  |pcE |pcEP|pcLP|pcSR|Spd  |X| CONSm|Heal|pc|
  1  xys                H9|    |   |    |    |    |    |   ||     | |      | 322|11|
  2  v3bwwwr15          m2|    |   |    |  3 |  3 |  3 |   ||  3  | |     3|  95| 5|
  3  qd23               H4|  3 | 2 |  3 |    |  1 |  1 | 3 || 21  | | 2 1 3| 228| 3|
  4  FRt5               m3|  2 | 3 |  2 |  1 |    |    | 1 || 23  | | 2 2 1| 142| 3|
  5  p002Ft             H4|    |   |    |    |    |    |   ||     | |      | 361| 9|
  6  NNNz13             d1|  1 | 1 |  1 |  2 |  2 |  2 | 2 || 37  | | 3 1  |  48| 1|

RRR02  Patients      Mags 1=23 2=15 3=9 4=6 5=4
___________________________________________________________________________________
ID   Code              fts     |LOS|BH  |pcE |pcEP|pcLP|pcSR|Spd||X| CONSm|Heal|pc|
  1  qus                H7|    |   |    |    |    |    |   ||     ||      | 204|11|
  2  w752               m3|    |   |    |  3 |  3 |  3 |   ||  3  ||     3|  95| 5|
  3  rsss               PT|  3 | 2 |  3 |    |  1 |  1 | 3 || 21  || 2 1 3| 228| 3|
  4  4562               m3|  2 | 3 |  2 |  1 |    |    | 1 || 23  || 2 2 1| 142| 3|
  5  4862               H4|    |   |    |    |    |    |   ||     ||      | 361| 9|
  6  bVf6RW             d1|  1 | 1 |  1 |  2 |  2 |  2 | 2 || 37  || 3 4  | 122| 1|
  7  YyPpk8             M3|  2 | 3 |  2 |  1 |  1 |  2 | 3 || 922 || 2 2 3| 100|12|
&lt;/PRE&gt;
&lt;P&gt;That looks like a pretty standard report file.&amp;nbsp; I am not sure why there seem to be more columns of data than names in the header row, but hopefully you know what that means.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To figure out what kind of line you are on so you know how to read the line take advantage of the trailing&amp;nbsp;@ on the INPUT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To capture the information in the "by" line between the blocks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;BTP04  Patients      Mags 1=23 2=15 3=9 4=6 5=4&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;take advantage of the RETAIN statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like this should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile text dlm=' |' truncover;
  length site $8 id 8;
  retain site;
  input id ?? @;
  if _infile_ = ' ' or _infile_ in: ('_' 'ID') then delete;
  if missing(id) then do;
    input @1 site ;
    delete;
  end;
  input code $ fts $;
  heal = input(scan(_infile_,-2,'|'),??32.);
  pc = input(scan(_infile_,-1,'|'),??32.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt; Obs    site     id    code        fts    heal    pc

   1    BTP04     1    xys         H9      322    11
   2    BTP04     2    v3bwwwr1    m2       95     5
   3    BTP04     3    qd23        H4      228     3
   4    BTP04     4    FRt5        m3      142     3
   5    BTP04     5    p002Ft      H4      361     9
   6    BTP04     6    NNNz13      d1       48     1
   7    RRR02     1    qus         H7      204    11
   8    RRR02     2    w752        m3       95     5
   9    RRR02     3    rsss        PT      228     3
  10    RRR02     4    4562        m3      142     3
  11    RRR02     5    4862        H4      361     9
  12    RRR02     6    bVf6RW      d1      122     1
  13    RRR02     7    YyPpk8      M3      100    12
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Aug 2025 19:03:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973053#M377639</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-08-21T19:03:08Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a complicated text file into a SAS data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973082#M377641</link>
      <description>&lt;P&gt;Here is what I got.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
infile "C:\Users\xiakeshan\Documents\Downloads\Consx.txt" encoding='utf8' termstr=crlf expandtabs;
input;
length site id code fts heal pc$ 80;
retain site;
if find(_infile_,'patients','it') then site=scan(_infile_,1,' ');
if prxmatch('/^\s*\d+/',_infile_) then do;
  id=scan(_infile_,1,' ');
  code=scan(_infile_,2,' ');
  fts=scan(_infile_,3,' |');
  heal=scan(_infile_,-2,'|');
  pc=scan(_infile_,-1,'|');
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Aug 2025 01:47:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973082#M377641</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-08-22T01:47:21Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a complicated text file into a SAS data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973091#M377644</link>
      <description>If this is collected by one person, maybe it's easier to direct her to store the data in a more readable format. &lt;BR /&gt;If this data is entered manually it's error prone.</description>
      <pubDate>Fri, 22 Aug 2025 10:41:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973091#M377644</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2025-08-22T10:41:35Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a complicated text file into a SAS data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973119#M377645</link>
      <description>&lt;P&gt;I spoke with my colleague and she said that your code worked like a charm. Thank you,&lt;/P&gt;</description>
      <pubDate>Fri, 22 Aug 2025 14:26:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973119#M377645</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2025-08-22T14:26:12Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a complicated text file into a SAS data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973134#M377646</link>
      <description>&lt;P&gt;I know I'm late to the party, but I always like show example of NAMED-INPUT and PARMCARDS&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname=any;
filename FT15F001 temp;
data test;
   retain;
   length dlm $1;
   infile FT15F001 dlm=dlm stopover;
   input @;
   if _infile_ eq ' ' or _infile_ eq: '_' then delete;
   if find(_infile_,'Patients') then do;
      dlm = ' ';
      input site:$5. @'Mags' ('1'n-'5'n)(=);
      delete;
      end;
   else if _infile_ eq: ' ' then do;
      _infile_ = tranwrd(_infile_,'||','| |');
      dlm=' ';
      input id code:$12. @;
      dlm='|';
      input fts:$2. (dum1 LOS BH  pcE pcEP pcLP pcSR dum2 spd X)(:) CONSm:$8. (Heal pc)(:);
      end;
   else delete;
   rename '1'n-'5'n=mag1-mag5;
   parmcards4;
BTP04  Patients      Mags 1=23 2=15 3=9 4=6 5=4
___________________________________________________________________________________
ID   Code              fts     |LOS|BH  |pcE |pcEP|pcLP|pcSR|Spd  |X| CONSm|Heal|pc|
  1  xys                H9|    |   |    |    |    |    |   ||     | |      | 322|11|
  2  v3bwwwr15          m2|    |   |    |  3 |  3 |  3 |   ||  3  | |     3|  95| 5|
  3  qd23               H4|  3 | 2 |  3 |    |  1 |  1 | 3 || 21  | | 2 1 3| 228| 3|
  4  FRt5               m3|  2 | 3 |  2 |  1 |    |    | 1 || 23  | | 2 2 1| 142| 3|
  5  p002Ft             H4|    |   |    |    |    |    |   ||     | |      | 361| 9|
  6  NNNz13             d1|  1 | 1 |  1 |  2 |  2 |  2 | 2 || 37  | | 3 1  |  48| 1|

RRR02  Patients      Mags 1=23 2=15 3=9 4=6 5=4
___________________________________________________________________________________
ID   Code              fts     |LOS|BH  |pcE |pcEP|pcLP|pcSR|Spd||X| CONSm|Heal|pc|
  1  qus                H7|    |   |    |    |    |    |   ||     ||      | 204|11|
  2  w752               m3|    |   |    |  3 |  3 |  3 |   ||  3  ||     3|  95| 5|
  3  rsss               PT|  3 | 2 |  3 |    |  1 |  1 | 3 || 21  || 2 1 3| 228| 3|
  4  4562               m3|  2 | 3 |  2 |  1 |    |    | 1 || 23  || 2 2 1| 142| 3|
  5  4862               H4|    |   |    |    |    |    |   ||     ||      | 361| 9|
  6  bVf6RW             d1|  1 | 1 |  1 |  2 |  2 |  2 | 2 || 37  || 3 4  | 122| 1|
  7  YyPpk8             M3|  2 | 3 |  2 |  1 |  1 |  2 | 3 || 922 || 2 2 3| 100|12|
;;;;
   run;

proc contents varnum;
proc print;
   run;
/*
Obs    site     mag1    mag2    mag3    mag4    mag5    id    code         fts    dum1    LOS    BH    pcE    pcEP    pcLP    pcSR    dum2    spd    X    CONSm    Heal    pc

  1    BTP04     23      15       9       6       4      1    xys          H9       .      .      .     .       .       .       .       .       .    .              322    11
  2    BTP04     23      15       9       6       4      2    v3bwwwr15    m2       .      .      .     3       3       3       .       .       3    .    3          95     5
  3    BTP04     23      15       9       6       4      3    qd23         H4       3      2      3     .       1       1       3       .      21    .    2 1 3     228     3
  4    BTP04     23      15       9       6       4      4    FRt5         m3       2      3      2     1       .       .       1       .      23    .    2 2 1     142     3
  5    BTP04     23      15       9       6       4      5    p002Ft       H4       .      .      .     .       .       .       .       .       .    .              361     9
  6    BTP04     23      15       9       6       4      6    NNNz13       d1       1      1      1     2       2       2       2       .      37    .    3 1        48     1
  7    RRR02     23      15       9       6       4      1    qus          H7       .      .      .     .       .       .       .       .       .    .              204    11
  8    RRR02     23      15       9       6       4      2    w752         m3       .      .      .     3       3       3       .       .       3    .    3          95     5
  9    RRR02     23      15       9       6       4      3    rsss         PT       3      2      3     .       1       1       3       .      21    .    2 1 3     228     3
 10    RRR02     23      15       9       6       4      4    4562         m3       2      3      2     1       .       .       1       .      23    .    2 2 1     142     3
 11    RRR02     23      15       9       6       4      5    4862         H4       .      .      .     .       .       .       .       .       .    .              361     9
 12    RRR02     23      15       9       6       4      6    bVf6RW       d1       1      1      1     2       2       2       2       .      37    .    3 4       122     1
 13    RRR02     23      15       9       6       4      7    YyPpk8       M3       2      3      2     1       1       2       3       .     922    .    2 2 3     100    12
*/
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109207i8F350A6B7FC5CD1F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Aug 2025 15:48:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-complicated-text-file-into-a-SAS-data-set/m-p/973134#M377646</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2025-08-22T15:48:09Z</dc:date>
    </item>
  </channel>
</rss>

