<?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 value of a string after a specific string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-value-of-a-string-after-a-specific-string/m-p/834780#M329992</link>
    <description>&lt;P&gt;One way&lt;/P&gt;
&lt;PRE&gt;data have;
   infile datalines dlm='|';
   input id1 $ id2:$30.;
datalines;
33567|23258:1 33567:4, 55765:10
11267|20135:2，55367:5, 54765:1
;


data want;
   set have;
   pos = findw(id2,strip(id1),' :,','E');
   if pos&amp;gt;0 then num = input(scan(id2,pos+1,' :,'),f5.);
   else num=0;
   drop pos;
run;
&lt;/PRE&gt;
&lt;P&gt;If your ID1 variable is actually numeric you need to convert it character for the FINDW. Replace Id1 with: put(id1,best6. -L)&lt;/P&gt;
&lt;P&gt;The FINDW used this way returns the number of the word in the string of the&amp;nbsp;&lt;STRONG&gt;first&amp;nbsp;&lt;/STRONG&gt;match or 0 if the value is not found in Id2.&lt;/P&gt;
&lt;P&gt;The uses Scan to extract the word following in use the Input function to convert to numeric.&lt;/P&gt;
&lt;P&gt;Note: if there is nothing following the found "word" you may get an invalid data from scan function or possibly the start of the next id:num pair.&lt;/P&gt;</description>
    <pubDate>Fri, 23 Sep 2022 01:52:21 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-09-23T01:52:21Z</dc:date>
    <item>
      <title>Extract value of a string after a specific string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-value-of-a-string-after-a-specific-string/m-p/834776#M329990</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset like this:&lt;/P&gt;
&lt;TABLE width="567"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="117.844px" height="30px"&gt;ID1&lt;/TD&gt;
&lt;TD width="448.156px" height="30px"&gt;ID2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="117.844px" height="30px"&gt;33567&lt;/TD&gt;
&lt;TD width="448.156px" height="30px"&gt;23258:1， 33567:4， 55765:10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="117.844px" height="30px"&gt;11267&lt;/TD&gt;
&lt;TD width="448.156px" height="30px"&gt;20135:2，55367:5， 54765:1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I want to do is: (1) determine if ID2 contains ID1; (2) if so, extract the number after it. Following is what I want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="631"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="118"&gt;ID1&lt;/TD&gt;
&lt;TD width="449"&gt;ID2&lt;/TD&gt;
&lt;TD width="64"&gt;Num&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;33567&lt;/TD&gt;
&lt;TD&gt;23258:1， 33567:4， 55765:10&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;11267&lt;/TD&gt;
&lt;TD&gt;20135:2，55367:5， 54765:1&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that scan and index cannot work directly here.&amp;nbsp; That would be great if someone can help here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Sep 2022 01:05:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-value-of-a-string-after-a-specific-string/m-p/834776#M329990</guid>
      <dc:creator>daradanye</dc:creator>
      <dc:date>2022-09-23T01:05:46Z</dc:date>
    </item>
    <item>
      <title>Re: Extract value of a string after a specific string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-value-of-a-string-after-a-specific-string/m-p/834780#M329992</link>
      <description>&lt;P&gt;One way&lt;/P&gt;
&lt;PRE&gt;data have;
   infile datalines dlm='|';
   input id1 $ id2:$30.;
datalines;
33567|23258:1 33567:4, 55765:10
11267|20135:2，55367:5, 54765:1
;


data want;
   set have;
   pos = findw(id2,strip(id1),' :,','E');
   if pos&amp;gt;0 then num = input(scan(id2,pos+1,' :,'),f5.);
   else num=0;
   drop pos;
run;
&lt;/PRE&gt;
&lt;P&gt;If your ID1 variable is actually numeric you need to convert it character for the FINDW. Replace Id1 with: put(id1,best6. -L)&lt;/P&gt;
&lt;P&gt;The FINDW used this way returns the number of the word in the string of the&amp;nbsp;&lt;STRONG&gt;first&amp;nbsp;&lt;/STRONG&gt;match or 0 if the value is not found in Id2.&lt;/P&gt;
&lt;P&gt;The uses Scan to extract the word following in use the Input function to convert to numeric.&lt;/P&gt;
&lt;P&gt;Note: if there is nothing following the found "word" you may get an invalid data from scan function or possibly the start of the next id:num pair.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Sep 2022 01:52:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-value-of-a-string-after-a-specific-string/m-p/834780#M329992</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-23T01:52:21Z</dc:date>
    </item>
  </channel>
</rss>

