<?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 How to read in txt file and only read in and keep certain columns with some key words in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-in-txt-file-and-only-read-in-and-keep-certain/m-p/350922#M81643</link>
    <description>&lt;P&gt;Hi All, I have a txt file that has three types of informaiton: name, address, and transations. There is one row per&amp;nbsp;customer. The data has position defined for name and address, but not for transation. Transation has several &amp;nbsp;types&amp;nbsp;and each type has its own length. &amp;nbsp;For example TRAN has length of 9, while SMET has length of 8. And each row can have any combination of TRAN and SMET. &amp;nbsp;Is there a way that I only read in transtions start with "TRAN" and keep each TRAN as a seperate row?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;raw data look like:&lt;/P&gt;&lt;P&gt;AMY &amp;nbsp; 205ABB street NY &amp;nbsp; TRAN90890 &amp;nbsp; TRAN89789 &amp;nbsp; SMET7485 &amp;nbsp; SMET7485&lt;BR /&gt;TED &amp;nbsp; 205BBC street CA &amp;nbsp; SMET7885 &amp;nbsp; &lt;SPAN&gt;SMET9885 &amp;nbsp; &lt;/SPAN&gt;TRAN23890 &amp;nbsp; TRAN89129 &amp;nbsp; SMET7655&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the sas data look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AMY &amp;nbsp; 205ABB street NY &amp;nbsp; TRAN90890&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AMY &amp;nbsp; 205ABB street NY &amp;nbsp; TRAN89789 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;TED &amp;nbsp; 205BBC street CA &amp;nbsp; TRAN23890 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;TED &amp;nbsp; 205BBC street CA &amp;nbsp; TRAN89129&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 18 Apr 2017 16:29:44 GMT</pubDate>
    <dc:creator>yymissing</dc:creator>
    <dc:date>2017-04-18T16:29:44Z</dc:date>
    <item>
      <title>How to read in txt file and only read in and keep certain columns with some key words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-in-txt-file-and-only-read-in-and-keep-certain/m-p/350922#M81643</link>
      <description>&lt;P&gt;Hi All, I have a txt file that has three types of informaiton: name, address, and transations. There is one row per&amp;nbsp;customer. The data has position defined for name and address, but not for transation. Transation has several &amp;nbsp;types&amp;nbsp;and each type has its own length. &amp;nbsp;For example TRAN has length of 9, while SMET has length of 8. And each row can have any combination of TRAN and SMET. &amp;nbsp;Is there a way that I only read in transtions start with "TRAN" and keep each TRAN as a seperate row?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;raw data look like:&lt;/P&gt;&lt;P&gt;AMY &amp;nbsp; 205ABB street NY &amp;nbsp; TRAN90890 &amp;nbsp; TRAN89789 &amp;nbsp; SMET7485 &amp;nbsp; SMET7485&lt;BR /&gt;TED &amp;nbsp; 205BBC street CA &amp;nbsp; SMET7885 &amp;nbsp; &lt;SPAN&gt;SMET9885 &amp;nbsp; &lt;/SPAN&gt;TRAN23890 &amp;nbsp; TRAN89129 &amp;nbsp; SMET7655&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the sas data look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AMY &amp;nbsp; 205ABB street NY &amp;nbsp; TRAN90890&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AMY &amp;nbsp; 205ABB street NY &amp;nbsp; TRAN89789 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;TED &amp;nbsp; 205BBC street CA &amp;nbsp; TRAN23890 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;TED &amp;nbsp; 205BBC street CA &amp;nbsp; TRAN89129&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 16:29:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-in-txt-file-and-only-read-in-and-keep-certain/m-p/350922#M81643</guid>
      <dc:creator>yymissing</dc:creator>
      <dc:date>2017-04-18T16:29:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to read in txt file and only read in and keep certain columns with some key words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-in-txt-file-and-only-read-in-and-keep-certain/m-p/350928#M81644</link>
      <description>&lt;P&gt;Sort of.&amp;nbsp; What you will end up doing is reading in all of the possibilities, and only outputting the correct ones.&amp;nbsp; This example assumes you will replace the columns for the first two variables with the correct columns:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;infile rawdata truncover;&lt;/P&gt;
&lt;P&gt;length TRAN $ 9;&lt;/P&gt;
&lt;P&gt;input name $ 1-5&amp;nbsp; address $ 6-25 @ ;&lt;/P&gt;
&lt;P&gt;do until (tran=' ');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; input tran $ @ ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if tran =: 'TRAN' then output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 16:48:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-in-txt-file-and-only-read-in-and-keep-certain/m-p/350928#M81644</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-18T16:48:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to read in txt file and only read in and keep certain columns with some key words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-in-txt-file-and-only-read-in-and-keep-certain/m-p/350929#M81645</link>
      <description>&lt;P&gt;Here is one way you can do it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
  infile 'c:\art\textfile.txt' lrecl=200 truncover;
  informat name $20.;
  informat address $50.;
  informat tran $9.;
  input name address &amp;amp; @;
  do until (missing(tran));
    input @'TRAN' tran @;
    if not missing(tran) then do;
      tran=catt('TRAN',tran);
      output;
    end;
  end;
  input;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 16:48:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-in-txt-file-and-only-read-in-and-keep-certain/m-p/350929#M81645</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-04-18T16:48:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to read in txt file and only read in and keep certain columns with some key words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-in-txt-file-and-only-read-in-and-keep-certain/m-p/350930#M81646</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; have;&lt;/P&gt;&lt;P&gt;infile datalines truncover;&lt;/P&gt;&lt;P&gt;input&amp;nbsp; name $ address &amp;amp; $20. @;&lt;/P&gt;&lt;P&gt;i=&lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;do until(scan(_infile_ ,i)=' ');&lt;/P&gt;&lt;P&gt;input dummy :$10.&amp;nbsp; @;&lt;/P&gt;&lt;P&gt;if dummy=:'TRAN' then output;&lt;/P&gt;&lt;P&gt;i+&lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;drop i;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;AMY&amp;nbsp;&amp;nbsp; 205ABB street NY&amp;nbsp;&amp;nbsp; TRAN90890&amp;nbsp;&amp;nbsp; TRAN89789&amp;nbsp;&amp;nbsp; SMET7485&amp;nbsp;&amp;nbsp; SMET7485&lt;/P&gt;&lt;P&gt;TED&amp;nbsp;&amp;nbsp; 205BBC street CA&amp;nbsp;&amp;nbsp; SMET7885&amp;nbsp;&amp;nbsp; SMET9885&amp;nbsp;&amp;nbsp; TRAN23890&amp;nbsp;&amp;nbsp; TRAN89129&amp;nbsp;&amp;nbsp; SMET7655&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Naveen Srinivasan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 16:52:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-in-txt-file-and-only-read-in-and-keep-certain/m-p/350930#M81646</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-04-18T16:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to read in txt file and only read in and keep certain columns with some key words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-in-txt-file-and-only-read-in-and-keep-certain/m-p/350934#M81650</link>
      <description>&lt;P&gt;Your raw data would work better if it was delimited (like comma, tab, pipe '|', etc). &amp;nbsp;Otherwise it will be hard to find the end of the address. &amp;nbsp;Let's first look at a solution that assumes it is easy to read the values. &amp;nbsp;For example if the values where tab delimited.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  infile 'myfile.txt' dsd dlm='09'x truncover ;
  length name $20 address $100 transaction $10 ;
  input name address transaction @ ;
  do col=1 by 1 until(transaction=' ');
     if col=1 or transaction ne ' ' then output;
     input transaction @;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Another possibility is that the values are space delimited, but there are always two spaces after the address and the addresses do not contain embedded double spaces. Then you can use the &amp;amp; modifier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  infile 'myfile.txt' truncover ;
  length name $20 address $100 transaction $10 ;
  input name address &amp;amp; transaction @ ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't have either of these then perhaps you can take advantage of the fact that your transactions always start with specific characters. &amp;nbsp;Probably better to use regular expression to search for the start of the transactions, but perhaps this would also work?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  infile cards truncover ;
  length name $20 address $100 transaction $10 ;
  input @ ;
  name = scan(_infile_,1);
  loc1 = index(_infile_,'TRAN');
  loc2 = index(_infile_,'SMET');
  if loc1 and loc2 then loc=min(loc1,loc2);
  else loc=max(loc1,loc2);
  drop loc1 loc2 loc ;
  address = substr(substrn(_infile_,1,loc-1),length(name)+2);
  input @loc transaction @;
  do col=1 by 1 until(transaction=' ');
     if col=1 or transaction ne ' ' then output;
     input transaction @;
  end;
cards;
AMY   205ABB street NY   TRAN90890   TRAN89789   SMET7485   SMET7485
TED   205BBC street CA   SMET7885   SMET9885   TRAN23890   TRAN89129   SMET7655
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 17:02:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-in-txt-file-and-only-read-in-and-keep-certain/m-p/350934#M81650</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-18T17:02:39Z</dc:date>
    </item>
  </channel>
</rss>

