<?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 Parse a string into different variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Parse-a-string-into-different-variables/m-p/45518#M9408</link>
    <description>Hi,&lt;BR /&gt;
I have one variable (example listed below) needs to be parsed into multiple variables. For example, the first listed (F1) 172/233 needs to be separated as 3 variables: F1, 172 and 233. The last listed M(850)84 F(832)9/358 needs to be separated as 6 variables: M850, 84, F832, 9 and 358. The length is not fixed. Even I use index function to locate the ( ) it still hard to extract the characters within ( ). Any help will be highly appreciated. Thanks,&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
(F1) 172/233&lt;BR /&gt;
(M20) 141&lt;BR /&gt;
(M279) 256&lt;BR /&gt;
(F125) 9/87&lt;BR /&gt;
M(603)90 F(666)10/170	&lt;BR /&gt;
M(850)84 F(832)9/358</description>
    <pubDate>Thu, 11 Sep 2008 22:04:40 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2008-09-11T22:04:40Z</dc:date>
    <item>
      <title>Parse a string into different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parse-a-string-into-different-variables/m-p/45518#M9408</link>
      <description>Hi,&lt;BR /&gt;
I have one variable (example listed below) needs to be parsed into multiple variables. For example, the first listed (F1) 172/233 needs to be separated as 3 variables: F1, 172 and 233. The last listed M(850)84 F(832)9/358 needs to be separated as 6 variables: M850, 84, F832, 9 and 358. The length is not fixed. Even I use index function to locate the ( ) it still hard to extract the characters within ( ). Any help will be highly appreciated. Thanks,&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
(F1) 172/233&lt;BR /&gt;
(M20) 141&lt;BR /&gt;
(M279) 256&lt;BR /&gt;
(F125) 9/87&lt;BR /&gt;
M(603)90 F(666)10/170	&lt;BR /&gt;
M(850)84 F(832)9/358</description>
      <pubDate>Thu, 11 Sep 2008 22:04:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parse-a-string-into-different-variables/m-p/45518#M9408</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-09-11T22:04:40Z</dc:date>
    </item>
    <item>
      <title>Re: Parse a string into different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parse-a-string-into-different-variables/m-p/45519#M9409</link>
      <description>A combination of SCAN or INDEX (to determine length), along with the SUBSTR function is appropriate.  Also, I see you will need to use COMPRESS to remove the parenthesis characters, possibly.  Remember the SCAN function accepts one or more delimiter characters as the second argument; also scan accepts a negative value to work from the right-most string location - not well documented, I don't believe.  And there are times where you may need to make multiple passes of the string/substring to extract the desired pieces.&lt;BR /&gt;
&lt;BR /&gt;
Also, if you speak Perl, there are some supported Perl-like functions now supported with current SAS.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 11 Sep 2008 22:14:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parse-a-string-into-different-variables/m-p/45519#M9409</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2008-09-11T22:14:26Z</dc:date>
    </item>
    <item>
      <title>Re: Parse a string into different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parse-a-string-into-different-variables/m-p/45520#M9410</link>
      <description>Hi yy1234&lt;BR /&gt;
&lt;BR /&gt;
Something like that:&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
  infile datalines missover;&lt;BR /&gt;
  input String $50.;&lt;BR /&gt;
  put String=;&lt;BR /&gt;
  array StringPart {100} $100.;&lt;BR /&gt;
  i=1;&lt;BR /&gt;
  do while(scan(String,i,') /') ne '');&lt;BR /&gt;
    StringPart{i}=scan(String,i,') /');&lt;BR /&gt;
    StringPart{i}=compress(StringPart{i},'(');&lt;BR /&gt;
    put StringPart{i}=;&lt;BR /&gt;
    i+1;&lt;BR /&gt;
  end;&lt;BR /&gt;
datalines;&lt;BR /&gt;
(F1) 172/233&lt;BR /&gt;
(M20) 141&lt;BR /&gt;
(M279) 256&lt;BR /&gt;
(F125) 9/87&lt;BR /&gt;
M(603)90 F(666)10/170 &lt;BR /&gt;
M(850)84 F(832)9/358 &lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
You could also first pass the data set and determine the necessary number of array elements before reading the "words" into separate variables.&lt;BR /&gt;
I've chosen 100 array elements. &lt;BR /&gt;
What could work to do it more dynamic is: length() of string - length() of string after compression of all delimiters - and then keeping the max value of this difference (call symput).&lt;BR /&gt;
Using this value (in macrovar) in array definition..... just an idea.&lt;BR /&gt;
&lt;BR /&gt;
And yes: There would also be some Perl RegEx which could be helpful - but I think in your case they wouldn't provide a simpler solution.&lt;BR /&gt;
&lt;BR /&gt;
Cheers&lt;BR /&gt;
Patrick</description>
      <pubDate>Fri, 12 Sep 2008 11:24:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parse-a-string-into-different-variables/m-p/45520#M9410</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2008-09-12T11:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: Parse a string into different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parse-a-string-into-different-variables/m-p/45521#M9411</link>
      <description>Hi Scott &amp;amp; Patrick,&lt;BR /&gt;
Thanks for all the suggestions. I tried the following. It works. gen_orig is the original variable I listed before. &lt;BR /&gt;
&lt;BR /&gt;
gentry=tranwrd(gen_orig, "F(", "(F");&lt;BR /&gt;
gentry2=tranwrd(gentry, "M(", "(M");&lt;BR /&gt;
genvar1=scan(gentry2, 1 );&lt;BR /&gt;
genvar2=scan(gentry2, 2);&lt;BR /&gt;
genvar3=scan(gentry2, 3);&lt;BR /&gt;
genvar4=scan(gentry2, 4);&lt;BR /&gt;
genvar5=scan(gentry2, 5);&lt;BR /&gt;
&lt;BR /&gt;
Best regards,&lt;BR /&gt;
Ying</description>
      <pubDate>Fri, 12 Sep 2008 16:33:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parse-a-string-into-different-variables/m-p/45521#M9411</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-09-12T16:33:33Z</dc:date>
    </item>
  </channel>
</rss>

