<?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: Macro to extract keyword text in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387571#M92926</link>
    <description>&lt;P&gt;Macro program is intended to generate code.&lt;/P&gt;
&lt;P&gt;You have already the working code.&lt;/P&gt;
&lt;P&gt;To convert your code into macro program, you should define,&lt;/P&gt;
&lt;P&gt;which parts of your program should be dynamic -&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suppose: name of input and output datasets, text arguments to search for, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you want to convert the search code into macro code ?&lt;/P&gt;</description>
    <pubDate>Sat, 12 Aug 2017 13:50:24 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2017-08-12T13:50:24Z</dc:date>
    <item>
      <title>Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387564#M92923</link>
      <description>&lt;P&gt;Hi everyone&lt;/P&gt;
&lt;P&gt;i am trying to convert the following code into macro, it basically breakes a text into sentences and then picks a sentence that has&amp;nbsp;&lt;/P&gt;
&lt;P&gt;keyword_1, and then extracts a number of words (5)&amp;nbsp;before and after that keyword and check if any of these words match&amp;nbsp;&lt;/P&gt;
&lt;P&gt;keyword_2&lt;/P&gt;
&lt;P&gt;so if you start with :&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;text&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;text&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;text&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;text&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;text&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;text&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;text&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;text&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;text&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;text .text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text .text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text .text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text . &amp;nbsp;&lt;STRONG&gt;GOOD SENTENCE HERE THAT SHOULD BE EXTRACTED&lt;/STRONG&gt;.&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;text&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if you use keywords "&lt;STRONG&gt;sentence&lt;/STRONG&gt;" and "&lt;STRONG&gt;should&lt;/STRONG&gt;", The first part of the code picks up:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;text&amp;nbsp;text . &amp;nbsp;&lt;STRONG&gt;GOOD SENTENCE HERE THAT SHOULD BE EXTRACTED&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;second part of the code will find the keyword "should" and return that keyword_2='present'&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the working code:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_1;
 set have;
 prxid=prxparse('/([^\.]|\.(?=\d))+\./o');
 start=1;stop=length(textstring);
 call prxnext(prxid,start,stop,textstring,position,length);

do while(position&amp;gt;0);
  hh=substr(textstring,position,length);
  if find(hh,'keyword_1','i') then output;
  call prxnext(prxid,start,stop,textstring,position,length);
 end;
drop start stop position length prxid;

run;

data want_2;
set want_1;
do i=1 to countw(hh," ");
   if scan(hh,i," ")="keyword_1"  then do;
      beforei=scan(hh,i-1," ");
	  beforei2=scan(hh,i-2," ");
	  beforei3=scan(hh,i-3," ");
	  beforei4=scan(hh,i-4," ");
	  beforei5=scan(hh,i-5," ");
	  afteri=scan(hh,i+1," ");
	  afteri2=scan(hh,i+2," ");
	  afteri3=scan(hh,i+3," ");
	  afteri4=scan(hh,i+4," ");
	  afteri5=scan(hh,i+5," ");
	j=trim(left(beforei5)) ||' '||trim(left(beforei4)) ||' '||trim(left(beforei3)) ||' '|| trim(left(beforei2)) ||' '|| trim(left(beforei)) ||' '|| 'keyword_1' ||' '|| trim(left(afteri)) ||' '|| trim(left(afteri2)) ||' '|| trim(left(afteri3)) ||' '|| trim(left(afteri4)) ||' '|| trim(left(afteri5));
    end;

end;
keyword_2='absent';
if prxmatch( '/\s(keyword_2)\s/i', j) then keyword_2='Present';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is an attempt at converting it to macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
 
%MACRO PRTMAC(string, match_a, match_b, wn);

	%local start stop position position; 
	%local RegexID found;
	%local i x st en;
	%global string_temp_1 string_temp_2 string_temp_3 retuen_variable;
	%let RegexID=%sysfunc(prxparse('/([^\.]|\.(?=\d))+\./o'));
	%let start=1;
	%let position=0;
	%let length=0;
	%let stop=%length(&amp;amp;string);

	
%syscall prxnext(RegexID,start,stop,string,position,length);

 %do %while(&amp;amp;position&amp;gt;0);
 %let string_temp_1=substr(&amp;amp;string,&amp;amp;position,&amp;amp;length);
	  %if %sysfunc(find(&amp;amp;string_temp_1,match_a)) %then %let string_temp_2=&amp;amp;string_temp_1;
	  %syscall prxnext(RegexID,start,stop,string,position,length);
 %end;


%do i=1 %to %sysfunc(countw(&amp;amp;string_temp_2," "));
   	%if %sysfunc(scan(&amp;amp;string_temp_2,i," "))=&amp;amp;match_a %then %do;
		%if &amp;amp;wn &amp;gt; &amp;amp;i %then %let st=1; 
			%else %let st=&amp;amp;i-&amp;amp;wn;
		%if &amp;amp;wn+&amp;amp;i &amp;gt; countw(&amp;amp;string_temp_2," ") %then %let en=countw(&amp;amp;string_temp_2," "); 
			%else %let en=&amp;amp;wn;
		%do x=&amp;amp;st %to &amp;amp;en;
			%let string_temp3=&amp;amp;string_temp3 ||' '|| %sysfunc(scan(string_temp_2,i-x," "));
		%end;
	%end;
%end;

%if %sysfunc(prxmatch( '/\s(match_b)\s/i', string_temp3)) %then %let retuen_variable='Present';
	%else %let retuen_variable='Absent';
data d;
set analyse;
String_1=&amp;amp;string_temp_1;
String_2=&amp;amp;string_temp_2;
String_3=&amp;amp;string_temp_3;
Return=&amp;amp;retuen_variable;
run;


%mend;

data want;
set have;

%PRTMAC(result, "keyword", "test", 3);


run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am not getting this to work, any suggestion on any changes pease?&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Aug 2017 08:20:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387564#M92923</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2017-08-12T08:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387569#M92924</link>
      <description>&lt;P&gt;My suggestion is to do it in a DATA step. &amp;nbsp;I have written some long macros. &amp;nbsp;To me, the more I use vanilla SAS and the less I use macro, the better the macro is written. &amp;nbsp;DATA step is really good at processing text that come from a user that might contain who knows what characters. &amp;nbsp;Macro with all of its quoting functions is more difficult. &amp;nbsp;If you need variations on a theme, use LINK and RETURN. &amp;nbsp;I did not try to figure out what you are doing wrong in macro, because I would never approach the problem that way.&lt;/P&gt;</description>
      <pubDate>Sat, 12 Aug 2017 12:14:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387569#M92924</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2017-08-12T12:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387571#M92926</link>
      <description>&lt;P&gt;Macro program is intended to generate code.&lt;/P&gt;
&lt;P&gt;You have already the working code.&lt;/P&gt;
&lt;P&gt;To convert your code into macro program, you should define,&lt;/P&gt;
&lt;P&gt;which parts of your program should be dynamic -&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suppose: name of input and output datasets, text arguments to search for, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you want to convert the search code into macro code ?&lt;/P&gt;</description>
      <pubDate>Sat, 12 Aug 2017 13:50:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387571#M92926</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-08-12T13:50:24Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387572#M92927</link>
      <description>&lt;P&gt;I don't think your "working" data step version works. Using the following example, want_1 ends up with NO records:&lt;/P&gt;
&lt;PRE&gt;data have;
  informat textstring $512.;
  input textstring &amp;amp; (keyword_1 keyword_2) ($);
  cards;
text text text text text text text text text text text text text text text text text text text text text text text text text text text text 5text. 4text. 3text. 2text. 1text.  GOOD SENTENCE HERE THAT SHOULD BE EXTRACTED. atext. btext. ctext. dtext. etext. text text text text text text text text text text text text text text  sentence  should
;

data want_1;
 set have;
 prxid=prxparse('/([^\.]|\.(?=\d))+\./o');
 start=1;stop=length(textstring);
 call prxnext(prxid,start,stop,textstring,position,length);

 do while(position&amp;gt;0);
   hh=substr(textstring,position,length);
   if find(hh,'keyword_1','i') then output;
   call prxnext(prxid,start,stop,textstring,position,length);
 end;
 drop start stop position length prxid;

run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Sat, 12 Aug 2017 13:58:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387572#M92927</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-08-12T13:58:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387601#M92936</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;&amp;nbsp;I dont think the code YOU added at the top is dooing what is expected to do, if you look at your "have" table, it dose not contain the sentence that I am interested in&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, put the keyword directly in the code I wrote, and IT DOSE WORK!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;data want_1;
 set have;
 prxid=prxparse('/([^\.]|\.(?=\d))+\./o');
 start=1;stop=length(textstring);
 call prxnext(prxid,start,stop,textstring,position,length);

 do while(position&amp;gt;0);
   hh=substr(textstring,position,length);
   if find(hh,'sentence','i') then output;
   call prxnext(prxid,start,stop,textstring,position,length);
 end;
 drop start stop position length prxid;

run;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Aug 2017 00:04:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387601#M92936</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2017-08-13T00:04:55Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387602#M92937</link>
      <description>&lt;P&gt;You have to provide your dataset HAVE in order for any of us to understand what your are trying to show. Since you hadn't done that initially, I had to guess at what you were trying to describe.&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>Sun, 13 Aug 2017 00:08:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387602#M92937</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-08-13T00:08:53Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387603#M92938</link>
      <description>&lt;P&gt;here you go &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;

textstring="text text text text text text text text text text text text text text text text text GOOD SENTENCE HERE THAT SHOULD BE EXTRACTED. text text 5text. 4text. 3text. 2text. 1text.  GOOD SENTENCE HERE THAT SHOULD BE EXTRACTED. atext. btext. ctext. dtext. etext. text text text text text text text text text text text text text text  ";
run;
data want_1;
 set have;
 prxid=prxparse('/([^\.]|\.(?=\d))+\./o');
 start=1;stop=length(textstring);
 call prxnext(prxid,start,stop,textstring,position,length);

 do while(position&amp;gt;0);
   hh=substr(textstring,position,length);
   if find(hh,'sentence','i') then output;
   call prxnext(prxid,start,stop,textstring,position,length);
 end;
 drop start stop position length prxid;

run;




data want_2;
set want_1;
do i=1 to countw(hh," ");
   if scan(lowcase(hh),i," ")="sentence"  then do;
      beforei=scan(hh,i-1," ");
	  beforei2=scan(hh,i-2," ");
	  beforei3=scan(hh,i-3," ");
	  beforei4=scan(hh,i-4," ");
	  beforei5=scan(hh,i-5," ");
	  afteri=scan(hh,i+1," ");
	  afteri2=scan(hh,i+2," ");
	  afteri3=scan(hh,i+3," ");
	  afteri4=scan(hh,i+4," ");
	  afteri5=scan(hh,i+5," ");
	j=trim(left(beforei5)) ||' '||trim(left(beforei4)) ||' '||trim(left(beforei3)) ||' '|| trim(left(beforei2)) ||' '|| trim(left(beforei)) ||' '|| 'keyword_1' ||' '|| trim(left(afteri)) ||' '|| trim(left(afteri2)) ||' '|| trim(left(afteri3)) ||' '|| trim(left(afteri4)) ||' '|| trim(left(afteri5));
    end;

end;
keyword_2='absent ';
if prxmatch( '/\s(should)\s/i', j) then keyword_2='Present';
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Aug 2017 00:27:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387603#M92938</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2017-08-13T00:27:16Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387604#M92939</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/24798"&gt;@ammarhm&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Get your SAS code as clean and simple as possible before you start macrotizing it.&lt;/P&gt;
&lt;P&gt;Below a RegEx which only returns a string if it finds your first keyword. It also only returns max 5 words before and after&amp;nbsp;your keyword&amp;nbsp;which are in the same sentence.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover ;
  input _str $255.;
  length string $ 2000;
  retain string;
  string=catx(' ',string,_str);
  if _n_=10 then output;
  drop _str;
  datalines;
text text text text text text text text text 
text .text text text text text 
text text text text .
text text text text text text text text 
text .text text text text text .  
GOOD SENTENCE HERE THAT 
SHOULD BE EXTRACTED. text 
text text text text text text 
text text text text text text 
text text text text text text 
;
run;

data want;
  set have;
  retain _prxid;
  if _n_=1 then _prxid=prxparse('/(\b\w+\b\s+){0,5}(\bsentence\b\s+)(\b\w+\b\s*){0,5}/i');
  _start=1;
  _stop=length(string);
  call prxnext(_prxid, _start, _stop, string, _pos, _len);
/*  sentence=substrn(string, _pos, _len);*/
  found= findw(substrn(string, _pos, _len),'extracted',1,' ','i') &amp;gt; 0;
  drop _prxid _start _stop _pos _len;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2017 01:38:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387604#M92939</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-08-13T01:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387609#M92941</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Thank you, very clever way of putting it together.&lt;/P&gt;
&lt;P&gt;How would you please convert this part to a macro to input keyword_1 and keyword_2 and then output the result of the macro (found) to a new column?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
  set have;
  retain _prxid;
  if _n_=1 then _prxid=prxparse('/(\b\w+\b\s+){0,5}(\b&lt;STRONG&gt;keyword_1&lt;/STRONG&gt;\b\s+)(\b\w+\b\s*){0,5}/i');
  _start=1;
  _stop=length(string);
  call prxnext(_prxid, _start, _stop, string, _pos, _len);
/*  sentence=substrn(string, _pos, _len);*/
  found= findw(substrn(string, _pos, _len),'&lt;STRONG&gt;keyword_2&lt;/STRONG&gt;',1,' ','i') &amp;gt; 0;
  drop _prxid _start _stop _pos _len;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2017 03:14:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387609#M92941</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2017-08-13T03:14:55Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387612#M92942</link>
      <description>&lt;P&gt;Here is what I have tried so far without luck, with (wn) being numerical representing number of words to extract:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO PRTMAC(string, match_a, match_b,wn);

	%local _prxid _start _stop  _pos _len; 
	%local sentence found;
	%if _n_=1 %then %let _prxid=%sysfunc(prxparse('/(\b\w+\b\s+){0,wn}(\b&amp;amp;match_a\b\s+)(\b\w+\b\s*){0,wn}/i'));
	%let _start=1;
	%let _pos=0;
	%let len=0;
	%let _stop=%length(&amp;amp;string);
%syscall prxnext(_prxid, _start, _stop, string, _pos, _len);
%let sentence=%sysfunc(substrn(&amp;amp;string, &amp;amp;_pos, &amp;amp;_len));
%if %sysfunc(findw(&amp;amp;sentence,&amp;amp;match_b,1,' ','i'))&amp;gt; 0 &amp;nbsp;%then %let found=1;
	 
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, this is still generating an error and I cant return the value of "found"&lt;/P&gt;
&lt;P&gt;Any help is appreciated&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2017 05:03:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387612#M92942</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2017-08-13T05:03:50Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387616#M92943</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/24798"&gt;@ammarhm&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Please answer first the questions&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;asked.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro code you've posted indicates that you eventually don't sufficiently understand how SAS macros should be used and how&amp;nbsp;they&amp;nbsp;work - "&lt;SPAN&gt;&lt;EM&gt;Macro program is intended to generate code"&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In order to really help you with a SAS macro, we need first to understand how you intend to use it / what problem you're trying to solve that requires a SAS macro - &lt;EM&gt;"&lt;/EM&gt;&lt;SPAN&gt;&lt;EM&gt;Why do you want to convert the search code into macro code?"&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2017 09:49:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387616#M92943</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-08-13T09:49:11Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387618#M92944</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;The reason is that I need to use this search function multiple times in a code i am writing, it seems easier to use a macro or function i can call everytime i need the code instead of copying the whole code multiple times in the code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a table&amp;nbsp;that contains multiple columns, each containing text that needs to be searched with different keywords. The final code would need to repeat this process &amp;gt;50 times, therefore pasting the same code as above 50 times but with different keywords is probably no&amp;nbsp;an effective way of doing things&lt;/P&gt;
&lt;P&gt;I hope this explains things&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2017 09:57:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387618#M92944</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2017-08-13T09:57:53Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387620#M92945</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/24798"&gt;@ammarhm&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;It does. The SAS macro should then only generate SAS code so all this code should remain on Base SAS code level. The only thing you need to macrotize are some variable names.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2017 09:58:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387620#M92945</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-08-13T09:58:32Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387621#M92946</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;isnt that what I am trying to achieve? making the variable names changeable..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;so what I am trying to achieve&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
Data want;
set have;
.
.
.
column_found_1=%macro(column_1,keyword_1,keyword_2)
.
.
.
column_found_n=%macro(column_n,keyword_x,keyword_y);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Aug 2017 10:11:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387621#M92946</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2017-08-13T10:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387622#M92947</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/24798"&gt;@ammarhm&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Compare below macro with what you've posted. Do you get the difference?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
options mprint;
%macro prtmac(string, match_a, match_b,wn, target_var);

  %global __counter;
  %let __counter=%eval(&amp;amp;__counter+1);

  retain _prxid_&amp;amp;__counter;
  if _n_=1 then _prxid_&amp;amp;__counter=prxparse("/(\b\w+\b\s+){0,&amp;amp;wn}(\b&amp;amp;match_a\b\s+)(\b\w+\b\s*){0,&amp;amp;wn}/i");
  _start=1;
  _stop=length(&amp;amp;string);
  call prxnext(_prxid_&amp;amp;__counter, _start, _stop, &amp;amp;string, _pos, _len);
  &amp;amp;target_var= findw(substrn(&amp;amp;string, _pos, _len),"&amp;amp;match_b",1,' ','i') &amp;gt; 0;
  call missing(_pos, _len);
  drop _prxid_&amp;amp;__counter _start _stop _pos _len;
	 
%mend;

data want;
  set have;
  %prtmac(string, sentence, extract, 5, found1)
  %prtmac(string, sentence, extracted, 5, found2)
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Aug 2017 10:20:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387622#M92947</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-08-13T10:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387623#M92948</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/24798"&gt;@ammarhm&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;BTW: Use the macro to search for multiple keywords. Use SAS Array processing if searching multiple variables for the same keywords.&lt;/P&gt;
&lt;P&gt;And of course: You can also combine the two approaches by wrapping the macro calls into a do loop over an array for searching multiple variables for the same and then have multiple such do loops for different search terms.&lt;/P&gt;
&lt;P&gt;...or even have an outer loop which passes in the different search terms.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2017 10:33:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387623#M92948</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-08-13T10:33:37Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387626#M92949</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;This is such a beautiful piece of code&lt;/P&gt;
&lt;P&gt;Thanks for all the help, much appreciated&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2017 10:51:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387626#M92949</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2017-08-13T10:51:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract keyword text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387627#M92950</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/24798"&gt;@ammarhm&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;If your real data can also contain commas and quotes then eventually add first a TRANSLATE() function and replace commas and quotes with blanks from your string.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2017 10:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-keyword-text/m-p/387627#M92950</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-08-13T10:56:59Z</dc:date>
    </item>
  </channel>
</rss>

