<?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: Extract words between strings/characters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760140#M240339</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
A='[{"name": [{"given": ["TESTG1 "], "given": ["TESTG2 "], "family": "TESTF1", "family": "TESTF2"}], "identifier": [{"value": "TESTNO", "system": "TESTSTRING"}]}]';
output;
A='[{"name": [{"given": ["TESTG "], "family": "TESTF"}], "identifier": [{"value": "TESTNO", "system": "TESTSTRING"}]}]';
output;
run;
data want;
 set have;
 id+1;
 do i=1 to countw(a,'{},');
  temp=scan(a,i,'{},');
  vname=dequote(strip(scan(strip(temp),1,':[]')));
  value=dequote(strip(scan(strip(temp),-1,':[]')));
  if vname in ('given' 'family') then output;
 end;
 keep id vname value;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 07 Aug 2021 11:09:43 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-08-07T11:09:43Z</dc:date>
    <item>
      <title>Extract words between strings/characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760100#M240315</link>
      <description>&lt;P&gt;I am trying to get the given name and family name from the following string:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;[{"name": [{"given": ["TESTG "], "family": "TESTF"}], "identifier": [{"value": "TESTNO", "system": "TESTSTRING"}]}]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code works for the above string:&amp;nbsp;&lt;/P&gt;&lt;P&gt;First_Name = compress(strip(scan(authors,5,':[]')),'"');&lt;BR /&gt;Last_Name = compress(strip(scan(authors,7,':[]}')),'"');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, the characters are not consistent, so in some cases I have:&lt;/P&gt;&lt;P&gt;[{"name": [{"given": ["TESTG1 "], "given": ["TESTG2 "], "family": "TESTF1", "family": "TESTF2"}], "identifier": [{"value": "TESTNO", "system": "TESTSTRING"}]}]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any way I can extract the first and last names for the first person and then the first and last names for the second person?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Aug 2021 22:11:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760100#M240315</guid>
      <dc:creator>monali</dc:creator>
      <dc:date>2021-08-06T22:11:58Z</dc:date>
    </item>
    <item>
      <title>Re: Extract words between strings/characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760104#M240319</link>
      <description>Have you tried parsing your file with the XML or JSON libraries instead of manually?</description>
      <pubDate>Fri, 06 Aug 2021 22:27:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760104#M240319</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-08-06T22:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Extract words between strings/characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760106#M240321</link>
      <description>Nope, this is already parsed and this is how I see it in my table.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 06 Aug 2021 22:56:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760106#M240321</guid>
      <dc:creator>monali</dc:creator>
      <dc:date>2021-08-06T22:56:17Z</dc:date>
    </item>
    <item>
      <title>Re: Extract words between strings/characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760112#M240324</link>
      <description>&lt;P&gt;I would key off of "given" and "family".&amp;nbsp; See code, below.&amp;nbsp; In the second type, there are two given names and two family names.&amp;nbsp; I'm just giving you the first, but you could modify the code if you need both.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I went step by step.&amp;nbsp; It's late in the day here, and I'm a bit tired.&amp;nbsp; Brain not working well.&amp;nbsp; You could nest the functions and make a much more compact program.&amp;nbsp; No doubt&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;or someone who knows regular expressions far better than I do will come along and do it all in 2 lines or less.&amp;nbsp; &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;%LET	Author1	=	%BQUOTE([{"name": [{"given": ["TESTG "], "family": "TESTF"}], "identifier": [{"value": "TESTNO", "system": "TESTSTRING"}]}]);
%LET	Author2	=	%BQUOTE([{"name": [{"given": ["TESTG1 "], "given": ["TESTG2 "], "family": "TESTF1", "family": "TESTF2"}], "identifier": [{"value": "TESTNO", "system": "TESTSTRING"}]}]);

DATA	Want;
	DROP	_:;

	_Authors="&amp;amp;Author1";
	_Temp		=	COMPRESS(_Authors,'[]{} :,');
	_Start		=	INDEX(LOWCASE(STRIP(_Temp)), 'given');
	_Start		=	_Start	+	7;
	_Temp2		=	SUBSTR(_Temp, _Start);
	_Length		=	INDEX(_Temp2, '"') - 1;
	First_Name	=	SUBSTR(STRIP(_Temp), _Start, _Length);
	_Start		=	INDEX(LOWCASE(STRIP(_Temp)), 'family');
	_Start		=	_Start	+	8;
	_Temp2		=	SUBSTR(_Temp, _Start);
	_Length		=	INDEX(_Temp2, '"') - 1;
	Last_Name	=	SUBSTR(STRIP(_Temp), _Start, _Length);
	PUTLOG	"NOTE:  1. "  First_Name=  Last_Name=;
	OUTPUT;

	_Authors="&amp;amp;Author2";
	_Temp		=	COMPRESS(_Authors,'[]{} :,');
	_Start		=	INDEX(LOWCASE(STRIP(_Temp)), 'given');
	_Start		=	_Start	+	7;
	_Temp2		=	SUBSTR(_Temp, _Start);
	_Length		=	INDEX(_Temp2, '"') - 1;
	First_Name	=	SUBSTR(STRIP(_Temp), _Start, _Length);
	_Start		=	INDEX(LOWCASE(STRIP(_Temp)), 'family');
	_Start		=	_Start	+	8;
	_Temp2		=	SUBSTR(_Temp, _Start);
	_Length		=	INDEX(_Temp2, '"') - 1;
	Last_Name	=	SUBSTR(STRIP(_Temp), _Start, _Length);
	PUTLOG	"NOTE:  2. "  First_Name=  Last_Name=;
	PUTLOG	"NOTE-  ";
	OUTPUT;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1628294574719.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62364iF5CB7038B069C27C/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1628294574719.png" alt="jimbarbour_0-1628294574719.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Sat, 07 Aug 2021 00:04:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760112#M240324</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-07T00:04:56Z</dc:date>
    </item>
    <item>
      <title>Re: Extract words between strings/characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760130#M240335</link>
      <description>&lt;P&gt;My take:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
A='[{"name": [{"given": ["TESTG1 "], "given": ["TESTG2 "], "family": "TESTF1", "family": "TESTF2"}], "identifier": [{"value": "TESTNO", "system": "TESTSTRING"}]}]';
output;
A='[{"name": [{"given": ["TESTG "], "family": "TESTF"}], "identifier": [{"value": "TESTNO", "system": "TESTSTRING"}]}]';
output;
run;

data WANT;
  set HAVE;  
  B=compress(A,'"{}[] ');
  POS1=1;
  POS2=1;
  do until(POS1=0);
    POS1=find(B,'given:',POS1);
    if POS1 then do;
      FIRST=scan(substr(B,POS1),2,':,'); 
      POS2 =find(B,'family:',POS2);
      LAST =scan(substr(B,POS2),2,':,'); 
      output;
      POS1+1;
      POS2+1;
    end;
  end;
  keep FIRST LAST;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE id="dgrid_32-header" class="dgrid-row-table" role="presentation"&gt;&lt;/TABLE&gt;
&lt;TABLE class="dgrid-row-table" role="presentation"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-0 field-col0 obsNumCell"&gt;1&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-1 field-col1"&gt;TESTG1&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-2 field-col2"&gt;TESTF1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;DIV id="dgrid_32-row-1" class=" dgrid-row dgrid-row-odd ui-state-default" role="row"&gt;
&lt;TABLE class="dgrid-row-table" role="presentation"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-0 field-col0 obsNumCell"&gt;2&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-1 field-col1"&gt;TESTG2&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-2 field-col2"&gt;TESTF2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;DIV id="dgrid_32-row-2" class=" dgrid-row ui-state-default dgrid-row-even dgrid-selected ui-state-active" role="row"&gt;
&lt;TABLE class="dgrid-row-table" style="width: 201px;" role="presentation" width="180px"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="44.45px" class="dgrid-cell dgrid-cell-padding dgrid-column-0 field-col0 obsNumCell"&gt;3&lt;/TD&gt;
&lt;TD width="71.2833px" class="dgrid-cell dgrid-cell-padding dgrid-column-1 field-col1"&gt;TESTG&lt;/TD&gt;
&lt;TD width="84.4333px" class="dgrid-cell dgrid-cell-padding dgrid-column-2 field-col2 dgrid-focus"&gt;TESTF&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 07 Aug 2021 06:30:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760130#M240335</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-08-07T06:30:13Z</dc:date>
    </item>
    <item>
      <title>Re: Extract words between strings/characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760140#M240339</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
A='[{"name": [{"given": ["TESTG1 "], "given": ["TESTG2 "], "family": "TESTF1", "family": "TESTF2"}], "identifier": [{"value": "TESTNO", "system": "TESTSTRING"}]}]';
output;
A='[{"name": [{"given": ["TESTG "], "family": "TESTF"}], "identifier": [{"value": "TESTNO", "system": "TESTSTRING"}]}]';
output;
run;
data want;
 set have;
 id+1;
 do i=1 to countw(a,'{},');
  temp=scan(a,i,'{},');
  vname=dequote(strip(scan(strip(temp),1,':[]')));
  value=dequote(strip(scan(strip(temp),-1,':[]')));
  if vname in ('given' 'family') then output;
 end;
 keep id vname value;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Aug 2021 11:09:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760140#M240339</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-08-07T11:09:43Z</dc:date>
    </item>
    <item>
      <title>Re: Extract words between strings/characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760225#M240372</link>
      <description>&lt;P&gt;Another way I remember reading about, that would be appropriate for parsing this string: Using the&amp;nbsp;&lt;FONT face="courier new,courier"&gt;@'string'&lt;/FONT&gt; syntax of the &lt;FONT face="courier new,courier"&gt;input&lt;/FONT&gt; statement. It can probably be coded better.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data T;
   infile cards truncover;
   input @;
   do until(LASTOBS);
     set SASHELP.CLASS end=LASTOBS;
     _INFILE_=catt('age:', AGE, ',name:', upcase(NAME));
     input @1 @'name:' NM $ @;
     output;
   end;
   keep NAME NM;
cards;
dummy
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The input statement is doing all the positioning and extracting work here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Aug 2021 10:06:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760225#M240372</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-08-08T10:06:48Z</dc:date>
    </item>
    <item>
      <title>Re: Extract words between strings/characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760355#M240436</link>
      <description>thank you! this worked perfectly!</description>
      <pubDate>Mon, 09 Aug 2021 13:28:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760355#M240436</guid>
      <dc:creator>monali</dc:creator>
      <dc:date>2021-08-09T13:28:40Z</dc:date>
    </item>
    <item>
      <title>Re: Extract words between strings/characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760356#M240437</link>
      <description>This solution works too!! thank you so much.</description>
      <pubDate>Mon, 09 Aug 2021 13:29:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760356#M240437</guid>
      <dc:creator>monali</dc:creator>
      <dc:date>2021-08-09T13:29:01Z</dc:date>
    </item>
    <item>
      <title>Re: Extract words between strings/characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760491#M240487</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Interesting technique using a "dummy" datalines (cards) to generate a test file.&amp;nbsp; Kind of handy.&amp;nbsp; I had not seen that before.&lt;/P&gt;
&lt;P&gt;Also an interesting technique using the INPUT with&amp;nbsp;@'literal'.&amp;nbsp; I didn't even think of that although I have (rarely) seen it used before.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I noticed that you used a comma as a separator.&amp;nbsp; I believe that if there were additional data beyond the name that you would pull in that data.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1628548115772.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62423iE8D696F566478E97/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_0-1628548115772.png" alt="jimbarbour_0-1628548115772.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I modified it to a space, and it worked as coded:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data T;
   infile cards truncover;
   input @;
   do until(LASTOBS);
     set SASHELP.CLASS end=LASTOBS;
	 LENGTH	NM	$32;
     _INFILE_=catt('age:', AGE, ' name:', upcase(NAME), ' sex:', UPCASE(SEX));
	 PUTLOG	"NOTE:  "  _INFILE_;
     input @1 @'name:' NM $ @;
     output;
   end;
   keep NAME NM;
   LENGTH	NM	$32;
cards;
dummy
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Alternatively, one could specify a comma delimiter.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;INFILE	Cards	DSD	DLM=',';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Yes, sorry.&amp;nbsp; Fussing over small details, I know, but I like to know how things work.&amp;nbsp; I was curious about the comma and wondered if I understood the syntax of&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;input @1 @'name:' NM $ @;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;so I just had to play with it.&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&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;</description>
      <pubDate>Mon, 09 Aug 2021 22:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-words-between-strings-characters/m-p/760491#M240487</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-09T22:31:29Z</dc:date>
    </item>
  </channel>
</rss>

