<?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: Extracting word embedded in square brackets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/673679#M202709</link>
    <description>&lt;PRE&gt;data have;
length have $100.;
 have='[ONE] "/" [TWO] "/(" [THREE] "-" [FOUR] ")\" [FIVE]'; output;
 have='[SIX] "(" [SEVEN] ")" [EIGHT] "-" [NINE] "--" ';	output;
 have='[ONE] "#" [TWO] "have" [THREE] "$" [NINE] "with" '; output;
 have=' "combine" [TWO] "with" [THREE] "[" [NINE] "]" '; output;
run ;

data want;
 set have;
 length want $ 100;
 pid=prxparse('/\[\w+?\]/');
 s=1;e=length(have);
 call prxnext(pid,s,e,have,p,l);
 do while(p&amp;gt;0);
   want=catx(',',want,compress(substr(have,p,l),'[]'));
   call prxnext(pid,s,e,have,p,l);
 end;
drop pid s e p l;
run;&lt;/PRE&gt;</description>
    <pubDate>Fri, 31 Jul 2020 11:50:48 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2020-07-31T11:50:48Z</dc:date>
    <item>
      <title>Extracting word embedded in square brackets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/673627#M202692</link>
      <description>&lt;P&gt;Hi All,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i am having a text embedded in square brackets with multiple delimiters in the text . Text in square brackets are considered as variable names. I want to extract these variable names and store in another variable. In some cases the square brackets&amp;nbsp;itself&amp;nbsp; act as delimiter.I have tried this using SCAN and COUNTW, but countw is counting each '[' separately but not combinely as single '[]' into one,hence the count is becoming many and loop is executing multiple times. Any suggestions ,may be i am missing the basic logic here.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
length have $100. want $50.;
 have='[ONE] "/" [TWO] "/(" [THREE] "-" [FOUR] ")\" [FIVE]';
 want="ONE,TWO,THREE,FOUR,FIVE";OUTPUT;
 have='[SIX] "(" [SEVEN] ")" [EIGHT] "-" [NINE] "--" ';
 want="SIX,SEVEN,EIGHT,NINE";OUTPUT;
 have='[ONE] "#" [TWO] "have" [THREE] "$" [NINE] "with" ';
 want="ONE,TWO,THREE,NINE";output;
 have=' "combine" [TWO] "with" [THREE] "[" [NINE] "]" ';
 want="TWO,THREE,NINE";OUTPUT;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Jul 2020 06:44:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/673627#M202692</guid>
      <dc:creator>keen_sas</dc:creator>
      <dc:date>2020-07-31T06:44:36Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting word embedded in square brackets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/673635#M202698</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16518"&gt;@keen_sas&lt;/a&gt;,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's a bit tricky, but it's just text, and it can be parsed.&amp;nbsp; I added a little sub-routine to your DATA step.&amp;nbsp; It puts out two lines for each "have" data that you start with.&amp;nbsp; The first of the two is "Want" -- this is what you want to end up with.&amp;nbsp; The second is "Got" which is what the sub-routine is actually producing.&amp;nbsp; Want and Got should be identical if the sub-routine is working properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	DROP	have;
	LENGTH	Status	$6;
	length have $100. want $50.;

	have='[ONE] "/" [TWO] "/(" [THREE] "-" [FOUR] ")\" [FIVE]';
	want="ONE,TWO,THREE,FOUR,FIVE";	Status='Want'; OUTPUT;
	LINK	Transform_Data;

	have='[SIX] "(" [SEVEN] ")" [EIGHT] "-" [NINE] "--" ';
	want="SIX,SEVEN,EIGHT,NINE";Status='Want'; OUTPUT;
	LINK	Transform_Data;

	have='[ONE] "#" [TWO] "have" [THREE] "$" [NINE] "with" ';
	want="ONE,TWO,THREE,NINE";Status='Want'; output;
	LINK	Transform_Data;

	have=' "combine" [TWO] "with" [THREE] "[" [NINE] "]" ';
	want="TWO,THREE,NINE";Status='Want'; OUTPUT;
	LINK	Transform_Data;

	RETURN;

	Transform_Data:
		Want=COMPRESS(TRANWRD(TRANWRD(TRANWRD(TRANSLATE(TRANWRD(COMPRESS(TRANWRD(TRANWRD(TRANWRD(have, '/(', '/'),')\','\'),'--',''),']" '),'[[',','),',,,,,,,','/\-()#$'),'with',','),'have',','),'combine',''),'[ ');
		IF	SUBSTR(Want, LENGTH(Want), 1)	=	','	THEN
			WANT							=	SUBSTR(Want, 1, (LENGTH(Want) - 1));
		Status='Got';
		OUTPUT;
	RETURN;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And below are the results.&amp;nbsp; The "Want" and the "Got" are in fact identical, so the sub-routine is confirmed to be working correctly.&lt;/P&gt;
&lt;PRE&gt;Obs    Status    want

 1      Want     ONE,TWO,THREE,FOUR,FIVE
 2      Got      ONE,TWO,THREE,FOUR,FIVE
 3      Want     SIX,SEVEN,EIGHT,NINE   
 4      Got      SIX,SEVEN,EIGHT,NINE   
 5      Want     ONE,TWO,THREE,NINE     
 6      Got      ONE,TWO,THREE,NINE     
 7      Want     TWO,THREE,NINE         
 8      Got      TWO,THREE,NINE       &lt;/PRE&gt;
&lt;P&gt;Hope that helps,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&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;</description>
      <pubDate>Fri, 31 Jul 2020 08:07:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/673635#M202698</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-07-31T08:07:00Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting word embedded in square brackets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/673662#M202707</link>
      <description>&lt;P&gt;Another way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  WANT=compress(transtrn(prxchange('s/( *|"[^"]*")//' ,-1,HAVE),'][',','),'[]');
  put WANT=;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;WANT=ONE,TWO,THREE,FOUR,FIVE&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;WANT=SIX,SEVEN,EIGHT,NINE&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;WANT=ONE,TWO,THREE,NINE&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;WANT=TWO,THREE,NINE&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 10:59:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/673662#M202707</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-07-31T10:59:28Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting word embedded in square brackets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/673679#M202709</link>
      <description>&lt;PRE&gt;data have;
length have $100.;
 have='[ONE] "/" [TWO] "/(" [THREE] "-" [FOUR] ")\" [FIVE]'; output;
 have='[SIX] "(" [SEVEN] ")" [EIGHT] "-" [NINE] "--" ';	output;
 have='[ONE] "#" [TWO] "have" [THREE] "$" [NINE] "with" '; output;
 have=' "combine" [TWO] "with" [THREE] "[" [NINE] "]" '; output;
run ;

data want;
 set have;
 length want $ 100;
 pid=prxparse('/\[\w+?\]/');
 s=1;e=length(have);
 call prxnext(pid,s,e,have,p,l);
 do while(p&amp;gt;0);
   want=catx(',',want,compress(substr(have,p,l),'[]'));
   call prxnext(pid,s,e,have,p,l);
 end;
drop pid s e p l;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Jul 2020 11:50:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/673679#M202709</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-07-31T11:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting word embedded in square brackets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/673961#M202853</link>
      <description>&lt;P&gt;Here a variation to what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;proposed.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length have $100.;
  have='[ONE] "/" [TWO] "/(" [THREE] "-" [FOUR] ")\" [FIVE]';
  output;
  have='[SIX] "(" [SEVEN] ")" [EIGHT] "-" [NINE] "--" ';
  output;
  have='[ONE] "#" [TWO] "have" [THREE] "$" [NINE] "with" ';
  output;
  have=' "combine" [TWO] "with" [THREE] "[" [NINE] "]" ';
  output;
  have='"[" [] "]"  "combine" [TWO] "with" [THREE] "[" [x] "]"  "[" [NINE] "]" ';
  output;
run;

data want(drop=_:);
  set have;
  length want $ 100;
  _pid=prxparse('/\[([^\[]*?)\]/');
  _start=1;
  _stop=length(have);
  do until(_pos&amp;lt;=0);
    call prxnext(_pid,_start,_stop,have,_pos,_len);
    want=catx(',',want,prxposn(_pid, 1, have));
  end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1596329470466.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47865i5F53B911B729E683/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1596329470466.png" alt="Patrick_0-1596329470466.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Aug 2020 01:14:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/673961#M202853</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-08-02T01:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting word embedded in square brackets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/674026#M202894</link>
      <description>&lt;P&gt;A slightly better one-liner:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WANT=compress(transtrn(prxchange('s/.*?(\[\w*\])[^\[]*/\1/' ,-1,HAVE),'][',','),'[]');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;ONE,TWO,THREE,FOUR,FIVE&lt;BR /&gt;SIX,SEVEN,EIGHT,NINE&lt;BR /&gt;ONE,TWO,THREE,NINE&lt;BR /&gt;TWO,THREE,NINE&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Aug 2020 23:43:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-word-embedded-in-square-brackets/m-p/674026#M202894</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-08-02T23:43:13Z</dc:date>
    </item>
  </channel>
</rss>

