<?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 read underscore in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326076#M72573</link>
    <description>&lt;P&gt;&lt;BR /&gt;there are text files of varying lengths. I would like to get the first part of files before processing them.&lt;BR /&gt;like if there are fles like this:&lt;/P&gt;&lt;P&gt;credit_scores_201501_usa.txt&lt;BR /&gt;mortgage_scores_201501_usa_st.txt&lt;BR /&gt;I only want credit_scores_201501 and mortgage_scores_201501.&lt;BR /&gt;There is either one underscore or two underscores before '.' that will give me the file name I'm interested in.&lt;/P&gt;&lt;P&gt;how to read number of underscores in a text file&lt;/P&gt;</description>
    <pubDate>Thu, 19 Jan 2017 18:35:10 GMT</pubDate>
    <dc:creator>SASPhile</dc:creator>
    <dc:date>2017-01-19T18:35:10Z</dc:date>
    <item>
      <title>read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326076#M72573</link>
      <description>&lt;P&gt;&lt;BR /&gt;there are text files of varying lengths. I would like to get the first part of files before processing them.&lt;BR /&gt;like if there are fles like this:&lt;/P&gt;&lt;P&gt;credit_scores_201501_usa.txt&lt;BR /&gt;mortgage_scores_201501_usa_st.txt&lt;BR /&gt;I only want credit_scores_201501 and mortgage_scores_201501.&lt;BR /&gt;There is either one underscore or two underscores before '.' that will give me the file name I'm interested in.&lt;/P&gt;&lt;P&gt;how to read number of underscores in a text file&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 18:35:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326076#M72573</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2017-01-19T18:35:10Z</dc:date>
    </item>
    <item>
      <title>read underscores in text file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326072#M72577</link>
      <description>&lt;P&gt;&lt;BR /&gt;there are text files of varying lengths. I would like to get the first part of files before processing them.&lt;BR /&gt;like if there are fles like this:&lt;/P&gt;&lt;P&gt;credit_scores_201501_usa.txt&lt;BR /&gt;mortgage_scores_201501_usa_st.txt&lt;BR /&gt;I only want credit_scores_201501 and mortgage_scores_201501.&lt;BR /&gt;There is either one underscore or two underscores before '.' that will give me the file name I'm interested in.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;And how to read number of underscores in a text file?&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 18:24:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326072#M72577</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2017-01-19T18:24:48Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326079#M72574</link>
      <description>&lt;P&gt;Counting the number of underscores is relatively straightforward:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;n_underscores = countc(filename, '_');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That doesn't finish the job though.&amp;nbsp; Are you comfortable with the rest of it, or do you need further help?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This might be easier:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;new_filename = catx('_', scan(filename, 1, '_'), scan(filename, 2, '_'), scan(filename, 3, '_') );&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 18:46:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326079#M72574</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-19T18:46:19Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326082#M72575</link>
      <description>&lt;P&gt;There are not just three underscores in the file.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;i wan the file names&amp;nbsp;will have either 8&amp;nbsp;or 9&amp;nbsp;underscores. if it has 8 i want the the file name before&amp;nbsp;the 7th underscore. if it has 9 I want&amp;nbsp;&lt;SPAN&gt; file name before&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;the 8th underscore.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 18:59:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326082#M72575</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2017-01-19T18:59:45Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326083#M72576</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;countw(string,'_') - 1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will give you the number of underscores in string.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 19:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326083#M72576</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-01-19T19:04:10Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326086#M72578</link>
      <description>&lt;P&gt;OK, working with that idea ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;n_underscores = countc(filename, '_');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;new_filename = scan(filename, 1, '_');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do i=2 to n_underscores-1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; new_filename = catx('_', new_filename, scan(filename, i, '_') );&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I'm understanding you correctly, this does it.&amp;nbsp; But there is a possiblity you may need to change n_underscores-1 to n_underscores-2.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 19:18:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326086#M72578</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-19T19:18:26Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326094#M72580</link>
      <description>&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;but the idea is to output the file name that is either one &amp;nbsp;underscores or two&amp;nbsp;&lt;SPAN&gt;underscores before '.' based on the length of file name.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 19:45:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326094#M72580</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2017-01-19T19:45:26Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326109#M72583</link>
      <description>&lt;P&gt;Now I'm confused.&amp;nbsp; Are you saying that you want only the date piece, or all the pieces up to and including the date piece?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 20:16:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326109#M72583</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-19T20:16:34Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326122#M72584</link>
      <description>&lt;P&gt;&lt;SPAN&gt;all the pieces up to and including the date piece&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 20:51:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326122#M72584</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2017-01-19T20:51:12Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326126#M72586</link>
      <description>&lt;P&gt;Doesn't my code in message #4 do exactly that?&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 21:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326126#M72586</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-19T21:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326586#M72760</link>
      <description>&lt;P&gt;Its reading the first three words.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Jan 2017 15:47:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326586#M72760</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2017-01-22T15:47:01Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326595#M72763</link>
      <description>&lt;P&gt;Yes, my original response reads three words. &amp;nbsp;But my second response in message #4 should read more than that.&lt;/P&gt;</description>
      <pubDate>Sun, 22 Jan 2017 18:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326595#M72763</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-22T18:05:10Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326604#M72766</link>
      <description>&lt;P&gt;If sometimes you remove two underscore-preceded "words" and sometimes you remove one, then getting a count of underscores would not seem to be the main issue.&amp;nbsp; It's coming up with a criterion to stop removing words.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It appears that you really want to remove trailing words until the last word is all numeric.&amp;nbsp; If that's the case then you want to see if the NOTDIGIT function applied to that last word returns a zero,&amp;nbsp; i.e.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Keep removing last word&amp;nbsp;until&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;( NOTDIGIT(last word of phrase)=0).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Getting the last underscore-separated word (trimmed of trailing blanks) is just the function&amp;nbsp;&amp;nbsp; trim(SCAN(phrase,-1,'_')).&amp;nbsp; So the stopping criterion is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; until (NOTDIGIT(trim(scan(phrase,-1,'_')))=0)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This program uses that approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input phrase $40. ;
datalines;
credit_scores_201501_usa.txt
mortgage_scores_201501_usa_st.txt
run;

data want;
  set have;
  length rslt $40;
  rslt=phrase;
  do until (notdigit(trim(scan(rslt,-1,'_')))=0);
    rslt=tranwrd(rslt,cats('_',scan(rslt,-1,'_')),'');
  end;
  put rslt=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The tranwrd function takes the full phrase in RSLT and "translates" the last word (including leading underscore) to a blank - i.e. it removes the last word.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This needs no further modification unless you expecte a trailing word you wish to remove to be exactly duplicated in some earlier word (i.e. if you had a phrase like "mortgage_scores_usa_201501_usa_st.txt").&amp;nbsp; If that were the case, then - inside the TRANWRD function - temporarily&amp;nbsp;make the last word absolutely unique by appending&amp;nbsp;something like an exclamation to it, and remove it in the TRANWRD function, as in&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Sasfont"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rslt=tranwrd(cats(rslt,&lt;/FONT&gt;&lt;FONT color="#800080" face="Sasfont"&gt;'!'&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;),cats(&lt;/FONT&gt;&lt;FONT color="#800080" face="Sasfont"&gt;'_'&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;,scan(rslt,-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;,&lt;/FONT&gt;&lt;FONT color="#800080" face="Sasfont"&gt;'_'&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;),&lt;/FONT&gt;&lt;FONT color="#800080" face="Sasfont"&gt;'!'&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;),&lt;/FONT&gt;&lt;FONT color="#800080" face="Sasfont"&gt;''&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Jan 2017 20:00:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326604#M72766</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-01-22T20:00:47Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326611#M72771</link>
      <description>&lt;P&gt;2 possible one-liners:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  input NAME : $30.;
  SHORT=substr(NAME,1,anydigit(NAME)+5);
  putlog SHORT=;
  SHORT=prxchange('s/(.*\d{6}).*/$1/',1,NAME);
  putlog SHORT=;
cards;
credit_scores_201501_usa.txt
mortgage_score_s_201501_usa_st.txt
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SHORT=credit_scores_201501&lt;BR /&gt;SHORT=credit_scores_201501&lt;BR /&gt;SHORT=mortgage_score_s_201501&lt;BR /&gt;SHORT=mortgage_score_s_201501&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2017 01:13:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326611#M72771</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-01-23T01:13:07Z</dc:date>
    </item>
    <item>
      <title>Re: read underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326612#M72772</link>
      <description>&lt;P&gt;If you want to ensure that the date is surrounded by underscores:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  SHORT=prxchange('s/(.*_\d{6})_.*/$1/',1,NAME);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 22 Jan 2017 23:01:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-underscore/m-p/326612#M72772</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-01-22T23:01:06Z</dc:date>
    </item>
  </channel>
</rss>

