<?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: Parsing String in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587494#M167816</link>
    <description>&lt;P&gt;How about this one ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input have $200.;
cards;
Street 1: ABC PL, HSE No: 100, Street 2: ATTN  xxxx / City: Loredo, State: TX, Zip: 77702-2600, / Country: US
Street 1: Cochte Rd, HSE No: 770, / City: FRAMI, State: MI, Zip: 01221-4666, / Country: US
Street 1: S 6th St,  Street 2: Mail Stop , / City: Manglore, State: MN, Zip: 66402-3601, / Country: US
Street 1: THOMAS PL, / City: EVERMAN, State: TX, Zip: 76140-4610, / Country: US
;
data temp;
 set have;
 pid=prxparse('/Street 1:|Street 2:|City:|State:|Zip:|Country:/i');
 s=1;e=length(have);
 id+1;
 call prxnext(pid,s,e,have,p,l);
 do while(p&amp;gt;0);
  name=substr(have,p,l); output;
  call prxnext(pid,s,e,have,p,l);
 end;
run;
data want;
 merge temp temp(firstobs=2 keep=id p rename=(id=_id p=_p));
 if id=_id then value=substr(have,p+l,_p-p-l);
  else value=substr(have,p+l,e-p-l+1);
 keep id name value;
run;
proc transpose data=want out=final_want;
by id;
id name;
var value;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 10 Sep 2019 13:05:11 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2019-09-10T13:05:11Z</dc:date>
    <item>
      <title>Parsing String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587242#M167708</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can some one please help as how to parse the address string into street, city, state, zip and Country?&amp;nbsp; The challenge is the string has different delimiters.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the sample string: (Note : some data line has street 2 and some don't.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Street 1&lt;/STRONG&gt;: ABC PL, HSE No: 100, &lt;STRONG&gt;Street 2&lt;/STRONG&gt;: ATTN&amp;nbsp; xxxx &lt;STRONG&gt;/ City:&lt;/STRONG&gt;&amp;nbsp;Loredo, &lt;STRONG&gt;State:&lt;/STRONG&gt; TX, &lt;STRONG&gt;Zip:&lt;/STRONG&gt; 77702-2600,&lt;STRONG&gt; / Country:&lt;/STRONG&gt; US&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Street 1:&lt;/STRONG&gt; Cochte Rd, HSE No: 770,&lt;STRONG&gt; / City:&lt;/STRONG&gt; FRAMI, &lt;STRONG&gt;State:&lt;/STRONG&gt; MI, &lt;STRONG&gt;Zip:&lt;/STRONG&gt; 01221-4666, &lt;STRONG&gt;/ Country:&lt;/STRONG&gt; US&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Street 1: S 6th St,&amp;nbsp; Street 2: Mail Stop , / City: Manglore, State: MN, Zip: 66402-3601, / Country: US&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Street 1: THOMAS PL, / City: EVERMAN, State: TX, Zip: 76140-4610, / Country: US&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Appreciate any help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-------------------------------------------&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 14:03:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587242#M167708</guid>
      <dc:creator>BhargavDesai</dc:creator>
      <dc:date>2019-09-09T14:03:35Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587247#M167710</link>
      <description>&lt;P&gt;What do you expect as result?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that this problem could be solved by using regular expressions, maybe just one with multiple groups. But all depends on the structure you want as result.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 14:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587247#M167710</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-09-09T14:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587249#M167711</link>
      <description>&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I am looking for is string parsed in following fields&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;STREET1&amp;nbsp; &amp;nbsp; Street2&amp;nbsp; &amp;nbsp;City State Zip Country.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does this make sense?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 14:45:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587249#M167711</guid>
      <dc:creator>BhargavDesai</dc:creator>
      <dc:date>2019-09-09T14:45:35Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587283#M167729</link>
      <description>&lt;P&gt;Replace&amp;nbsp; your colons with equal sign and use named input.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use TRANSLATE to replace the colon with equal sign.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=n1w749t788cgi2n1txpuccsuqtro.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#n1l7p7qnzo8tj2n1r0ejf1vhrbq6"&gt;https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=n1w749t788cgi2n1txpuccsuqtro.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#n1l7p7qnzo8tj2n1r0ejf1vhrbq6&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also just use SCAN() and a loop.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. use countc to count the number of colons.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Use the result from 1 to loop your data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Use SCAN() to filter variable and value&lt;/P&gt;
&lt;P&gt;4. Transpose data&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 15:35:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587283#M167729</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-09T15:35:23Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587494#M167816</link>
      <description>&lt;P&gt;How about this one ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input have $200.;
cards;
Street 1: ABC PL, HSE No: 100, Street 2: ATTN  xxxx / City: Loredo, State: TX, Zip: 77702-2600, / Country: US
Street 1: Cochte Rd, HSE No: 770, / City: FRAMI, State: MI, Zip: 01221-4666, / Country: US
Street 1: S 6th St,  Street 2: Mail Stop , / City: Manglore, State: MN, Zip: 66402-3601, / Country: US
Street 1: THOMAS PL, / City: EVERMAN, State: TX, Zip: 76140-4610, / Country: US
;
data temp;
 set have;
 pid=prxparse('/Street 1:|Street 2:|City:|State:|Zip:|Country:/i');
 s=1;e=length(have);
 id+1;
 call prxnext(pid,s,e,have,p,l);
 do while(p&amp;gt;0);
  name=substr(have,p,l); output;
  call prxnext(pid,s,e,have,p,l);
 end;
run;
data want;
 merge temp temp(firstobs=2 keep=id p rename=(id=_id p=_p));
 if id=_id then value=substr(have,p+l,_p-p-l);
  else value=substr(have,p+l,e-p-l+1);
 keep id name value;
run;
proc transpose data=want out=final_want;
by id;
id name;
var value;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 Sep 2019 13:05:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587494#M167816</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-09-10T13:05:11Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587625#M167856</link>
      <description>&lt;P&gt;Something like this might be able to read the file properly to begin with.&lt;/P&gt;
&lt;P&gt;I may have included more checks for existing values but once you have one set of inconsistent values such as Street2 and the House number everything becomes questionable in my book.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't want the house number the drop it. You may need to set the length of some variables longer.&lt;/P&gt;
&lt;PRE&gt;data work.want;
   infile datalines dlm=',/' truncover;
   input @"Street 1: " Street1 :$15. @;
   if findw(_infile_,"HSE No:")&amp;gt; 0 then input  @"HSE No: "   Housenum :$15.  @;
   if findw(_infile_,"Street 2:")&amp;gt; 0 then input           @"Street 2: " Street2 :$15. @;
   if findw(_infile_,"City:")&amp;gt; 0 then input           @"City: "     City :$15. @;
   if findw(_infile_,"State:")&amp;gt; 0 then input           @"State: "    State :$15. @;
   if findw(_infile_,"Zip:")&amp;gt; 0 then input           @"Zip: "      Zip :$15. @;
   if findw(_infile_,"Country:")&amp;gt; 0 then input           @"Country:"   Country :$15. @;
   input;
   ;
datalines;
Street 1: ABC PL, HSE No: 100, Street 2: ATTN  xxxx / City: Loredo, State: TX, Zip: 77702-2600,/ Country: US
Street 1: Cochte Rd, HSE No: 770,/City: FRAMI, State: MI, Zip: 01221-4666,/Country: US
Street 1: S 6th St,  Street 2: Mail Stop ,/City: Manglore, State: MN, Zip: 66402-3601,/Country: US
Street 1: THOMAS PL,/City: EVERMAN, State: TX, Zip: 76140-4610,/Country: US
;&lt;/PRE&gt;
&lt;P&gt;Please post code and data into a code box opened using the forum's {I} icon. The forum main message windows will reformat text to some extent and what we see may not be what you actually have.&lt;/P&gt;
&lt;P&gt;Plus extra line feeds aren't helpful.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Sep 2019 18:22:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-String/m-p/587625#M167856</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-09-10T18:22:26Z</dc:date>
    </item>
  </channel>
</rss>

