<?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: Identifying n-1 word in a string using a KEYWORD in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588792#M168332</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16518"&gt;@keen_sas&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Just scroll the text one word at a time, and if it is "data", append the prior word to the variable you want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                
  infile cards truncover ;                                                                 
  input have $ 1-100 ;                                                                     
  cards ;                                                                                  
Sports data is sufficient                                                                  
Basketball Players Data is listed on the Olympic data                                      
Soccer data and swimming data are not found in the Olympic data starting in the next month 
Data is the first word here - we need WANT blank in this case                              
;                                                                                          
run ;                                                                                      
                                                                                           
data want (drop = _:) ;                                                                    
  set have ;                                                                               
  length want _v $ 100 ;                                                                   
  do _x = 1 to countw (have) ;                                                             
    _w = scan (have, _x) ;                                                                 
    if lowcase (_w) = "data" then want = catx (" ", want, _v) ;                            
    _v = _w ;                                                                              
  end ;                                                                                    
run ;                                                                                      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 15 Sep 2019 03:09:08 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-09-15T03:09:08Z</dc:date>
    <item>
      <title>Identifying n-1 word in a string using a KEYWORD</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588749#M168307</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data new;&lt;BR /&gt;length have $1000. want $100.;&lt;BR /&gt;have ="Sports data is sufficient";&lt;BR /&gt;want ="sports";&lt;BR /&gt;output ;&lt;BR /&gt;have="Basketball Players Data is listed on the Olympic data";&lt;BR /&gt;want= "Players Olympic";&lt;BR /&gt;output ;&lt;BR /&gt;&lt;BR /&gt;have ="Soccer data and swimming data are not found in the Olympic data starting in the next month";&lt;BR /&gt;want="soccer swimming Olympic";&lt;BR /&gt;output ;&lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the above text DATA is the key word that is present once or more than once in each string (HAVE variable) . I have to identify the WORD before DATA in each string and i have to replace it with other word. In the first example SPORTS is the word present prior to the keyword DATA. Similarly in the second string DATA keyword is repeated twice and words present before DATA in it are Players and Olympic. Any method to identify the sub string/word before the keyword DATA from the above example. After identifying these words i will be replacing with some other words.&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2019 17:52:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588749#M168307</guid>
      <dc:creator>keen_sas</dc:creator>
      <dc:date>2019-09-14T17:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying n-1 word in a string using a KEYWORD</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588752#M168309</link>
      <description>&lt;P&gt;What 'other words' ?&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2019 18:54:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588752#M168309</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-09-14T18:54:18Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying n-1 word in a string using a KEYWORD</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588762#M168316</link>
      <description>&lt;P&gt;Please try the below code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input text&amp;amp;$200.; 
string=tranwrd(lowcase(text),'data','#');
cards;
Sports data is sufficient
Basketball Players Data is listed on the Olympic data
Soccer data and swimming data are not found in the Olympic data starting in the next month
;

proc sort data=have;
by string;
run;

data want(where=(count(string,'#')=i));
length new $100.;
set have;
by string;
do i = 1 to count(string,'#');
new=catx(' ', new, scan(scan(string,i,'#'),-1,' '));
output;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Sep 2019 20:23:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588762#M168316</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-09-14T20:23:36Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying n-1 word in a string using a KEYWORD</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588763#M168317</link>
      <description>&lt;P&gt;FWIW, My share of fun&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data new;
length have $1000. want $100.;
have ="Sports data is sufficient";
want ="sports";
output ;
have="Basketball Players data is listed on the Olympic data";
want= "Players Olympic";
output ;

have ="Soccer data and swimming data are not found in the Olympic data starting in the next month";
want="soccer swimming Olympic";
output ;
run ;


/*new_want is the output variable*/
data want;
set new;
_have=have;
length new_want $100;
do p=findw(_have,'data',' ','ei') by 0 while(p);
 new_want=catx(' ',new_want,scan(_have,p-1,' ','i'));
 call scan(_have, p, _p, _l,' ','i');
 substr(_have,_p,_l)='09'x;
 p=findw(_have,'data',' ','ei');
end;
drop _: p;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Sep 2019 20:43:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588763#M168317</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-14T20:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying n-1 word in a string using a KEYWORD</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588792#M168332</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16518"&gt;@keen_sas&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Just scroll the text one word at a time, and if it is "data", append the prior word to the variable you want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                
  infile cards truncover ;                                                                 
  input have $ 1-100 ;                                                                     
  cards ;                                                                                  
Sports data is sufficient                                                                  
Basketball Players Data is listed on the Olympic data                                      
Soccer data and swimming data are not found in the Olympic data starting in the next month 
Data is the first word here - we need WANT blank in this case                              
;                                                                                          
run ;                                                                                      
                                                                                           
data want (drop = _:) ;                                                                    
  set have ;                                                                               
  length want _v $ 100 ;                                                                   
  do _x = 1 to countw (have) ;                                                             
    _w = scan (have, _x) ;                                                                 
    if lowcase (_w) = "data" then want = catx (" ", want, _v) ;                            
    _v = _w ;                                                                              
  end ;                                                                                    
run ;                                                                                      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Sep 2019 03:09:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588792#M168332</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-15T03:09:08Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying n-1 word in a string using a KEYWORD</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588795#M168333</link>
      <description>&lt;P&gt;If the replacement word is always the same, then it is simple to do this with a regular expression substitution operation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
length have $1000. want $100.;
have ="Sports data is sufficient";
want ="sports";
output ;
have="Basketball Players Data is listed on the Olympic data";
want= "Players Olympic";
output ;
have ="Soccer data and swimming data are not found in the Olympic data starting in the next month";
want="soccer swimming Olympic";
output ;
run ;

data want;
set new;
length replaced $1000;
replaced = prxChange ("s/(\b\w+\b)(\s*data)/Other word\2/io",-1,have);
run;

proc print data=want noobs; var have replaced; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;have 	replaced
Sports data is sufficient 	Other word data is sufficient
Basketball Players Data is listed on the Olympic data 	Basketball Other word Data is listed on the Other word data
Soccer data and swimming data are not found in the Olympic data starting in the next month 	Other word data and Other word data are not found in the Other word data starting in the next month&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Sep 2019 04:03:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588795#M168333</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-09-15T04:03:27Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying n-1 word in a string using a KEYWORD</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588797#M168334</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;: What I've been waiting for.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Sep 2019 04:27:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-n-1-word-in-a-string-using-a-KEYWORD/m-p/588797#M168334</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-15T04:27:07Z</dc:date>
    </item>
  </channel>
</rss>

