<?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 SAS word from string with special characters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628397#M185678</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89720"&gt;@sameer112217&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an approach to achieve this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines truncover;
	input Description $500.;
	datalines;
i am repairing the system with 1.11.1.123 
I am done with system 1.23.1.120 and not 192.1.21.1 that lives in apt 5
i am not doing for 1.22.3.130 for person living in apartment 4
I am working on system 1.21.1.119
;
run;

proc sql noprint;
	select max(countw(Description,' ')), max(count(description,' 1.') + count(description,' 192.')) into:nb1,:nb2 from have;
quit;

data want;
	set have;
	array _IP (&amp;amp;nb1) $ 200;
	array IP (&amp;amp;nb2) $ 20;
	do i=1 to countw(Description,' ');
		if prxmatch('/(1\.)|(192\.)/',scan(Description,i,' ')) then _IP(i) = scan(Description,i,' ');
	end;
	IP_extract = catx(',',of _IP(*));
	do j=1 to dim(IP);
		IP(j) = scan(IP_extract,j,',');
	end;
	drop i j IP_extract _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture d’écran 2020-02-29 à 09.47.43.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36473i652A96BFD7501E38/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture d’écran 2020-02-29 à 09.47.43.png" alt="Capture d’écran 2020-02-29 à 09.47.43.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 29 Feb 2020 08:48:01 GMT</pubDate>
    <dc:creator>ed_sas_member</dc:creator>
    <dc:date>2020-02-29T08:48:01Z</dc:date>
    <item>
      <title>extracting SAS word from string with special characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628390#M185674</link>
      <description>&lt;P&gt;I need help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a variable with text description name as description&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Description&lt;/P&gt;&lt;P&gt;i am repairing the system with 1.11.1.123&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am done with system 1.23.1.120 and not 192.1.21.1 that lives in apt 5&lt;/P&gt;&lt;P&gt;i am not doing for 1.22.3.130 for person living in apartment 4&lt;/P&gt;&lt;P&gt;I am working on system 1.21.1.119&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I just want to extract only the ip address. the ip address starts with special character 1. or by 192.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so the output should be like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1.11.1.123&lt;/P&gt;&lt;P&gt;1.23.1.120&amp;nbsp; &amp;nbsp; 192.1.21.1&lt;/P&gt;&lt;P&gt;122.3.130&lt;/P&gt;&lt;P&gt;1.21.1.119&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried substring using index but it doesnt work out&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sameer&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Feb 2020 06:35:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628390#M185674</guid>
      <dc:creator>sameer112217</dc:creator>
      <dc:date>2020-02-29T06:35:11Z</dc:date>
    </item>
    <item>
      <title>Re: extracting SAS word from string with special characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628393#M185676</link>
      <description>&lt;P&gt;The safest option is probably to use PRX, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  prx_id=prxparse('/\b\d{1,3}\.d{1,3}\.d{1,3}\.d{1,3}\.\b/');
  start=1;
  stop=-1;
  do while(1);
    call prxnext(prx_id,start,stop,text_variable,position,length);
    if position=0 then leave;
    ip_addr=substr(text_variable,position,length);
    output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Yo can find the documentation for the PRX string here:&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p0s9ilagexmjl8n1u7e1t1jfnzlk.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;Tables of Perl Regular Expression (PRX) Metacharacters&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Feb 2020 07:27:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628393#M185676</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-02-29T07:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: extracting SAS word from string with special characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628397#M185678</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89720"&gt;@sameer112217&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an approach to achieve this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines truncover;
	input Description $500.;
	datalines;
i am repairing the system with 1.11.1.123 
I am done with system 1.23.1.120 and not 192.1.21.1 that lives in apt 5
i am not doing for 1.22.3.130 for person living in apartment 4
I am working on system 1.21.1.119
;
run;

proc sql noprint;
	select max(countw(Description,' ')), max(count(description,' 1.') + count(description,' 192.')) into:nb1,:nb2 from have;
quit;

data want;
	set have;
	array _IP (&amp;amp;nb1) $ 200;
	array IP (&amp;amp;nb2) $ 20;
	do i=1 to countw(Description,' ');
		if prxmatch('/(1\.)|(192\.)/',scan(Description,i,' ')) then _IP(i) = scan(Description,i,' ');
	end;
	IP_extract = catx(',',of _IP(*));
	do j=1 to dim(IP);
		IP(j) = scan(IP_extract,j,',');
	end;
	drop i j IP_extract _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture d’écran 2020-02-29 à 09.47.43.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36473i652A96BFD7501E38/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture d’écran 2020-02-29 à 09.47.43.png" alt="Capture d’écran 2020-02-29 à 09.47.43.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Feb 2020 08:48:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628397#M185678</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-29T08:48:01Z</dc:date>
    </item>
    <item>
      <title>Re: extracting SAS word from string with special characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628464#M185712</link>
      <description>&lt;P&gt;If you find regular expressions too confusing you could just use some simple SAS functions.&amp;nbsp; SCAN() and COMPRESS().&amp;nbsp; If you treat every non digit or period as a delimiter then SCAN() will find the candidates for you.&amp;nbsp; You will still need to do a little more validation, but you could just look for those that have exactly 3 periods.&lt;/P&gt;
&lt;P&gt;So if your dataset is named HAVE and your variable is named DESCRIPTION your code might look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  cnt=1;
  array ip[10] $15;
  do idx=1 by 1 ;
    ip[cnt]=scan(Description,idx,compress(Description,'.','d'));
    if ip[cnt]=' ' then leave;
    if countc(ip[cnt],'.')=3 then do;
      cnt+1;
      if cnt&amp;gt;dim(ip) then leave;
    end;
    else ip[cnt]=' ';
  end;
  cnt=cnt-1;
  drop idx;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs   Description

 1    i am repairing the system with 1.11.1.123
 2    I am done with system 1.23.1.120 and not 192.1.21.1 that lives in apt 5
 3    i am not doing for 1.22.3.130 for person living in apartment 4
 4    I am working on system 1.21.1.119
 5    There are 0 ip addresses here

Obs   cnt       ip1           ip2        ip3    ip4    ip5    ip6    ip7    ip8    ip9    ip10

 1     1     1.11.1.123
 2     2     1.23.1.120    192.1.21.1
 3     1     1.22.3.130
 4     1     1.21.1.119
 5     0
&lt;/PRE&gt;</description>
      <pubDate>Sat, 29 Feb 2020 18:19:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628464#M185712</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-29T18:19:54Z</dc:date>
    </item>
    <item>
      <title>Re: extracting SAS word from string with special characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628521#M185747</link>
      <description>&lt;P&gt;thanks tom, this is what i was looking exactly....&lt;/P&gt;</description>
      <pubDate>Sun, 01 Mar 2020 06:58:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628521#M185747</guid>
      <dc:creator>sameer112217</dc:creator>
      <dc:date>2020-03-01T06:58:28Z</dc:date>
    </item>
    <item>
      <title>Re: extracting SAS word from string with special characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628522#M185748</link>
      <description>&lt;P&gt;Thank you this is what i have been looking... i thank you so much for the help.&lt;/P&gt;</description>
      <pubDate>Sun, 01 Mar 2020 06:59:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-SAS-word-from-string-with-special-characters/m-p/628522#M185748</guid>
      <dc:creator>sameer112217</dc:creator>
      <dc:date>2020-03-01T06:59:21Z</dc:date>
    </item>
  </channel>
</rss>

