<?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: Separating  string(variable name) and sub-string into separate variables with multiple delimiter in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Separating-string-variable-name-and-sub-string-into-separate/m-p/614192#M179478</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16518"&gt;@keen_sas&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;1. Treat non-alphanumerics as delimiters.&lt;/P&gt;
&lt;P&gt;2. Treat alphanumerics as delimiters.&lt;/P&gt;
&lt;P&gt;3. In case #2, kill the &lt;EM&gt;endpoint&lt;/EM&gt; commas separately.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                             
  have = 'SIFI, " " SIUNIT " (", ATOX ", " GRADE ")"' ; output ;                                                                        
  have = 'SIFI " " SIUNIT " (" ATOX ", " GRADE ")"'   ; output ;                                                                        
run ;                                                                                                                                   
                                                                                                                                        
data want (keep = have var:) ;                                                                                                          
  set have ;                                                                                                                            
  array vv $ 8 var1-var8 v ;                                                                                                            
  do _n_ = 1 to 4 ;                                                                                                                     
    vv [2 * _n_ - 1] = scan (have, _n_, , "kad") ;                                                                                      
    v = scan (have, _n_, , "ad") ;                                                                                                      
    if char (v,         1 ) = "," then v = substr (v, 2) ;                                                                              
    if char (v, length (v)) = "," then v = substr (v, 1, length (v) - 1) ;                                                              
    vv [2 * _n_] = v ;                                                                                                                  
  end ;                                                                                                                                 
run ;           
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: I assumed the longest parsed component is $8. Adjust if need be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 28 Dec 2019 05:44:00 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-12-28T05:44:00Z</dc:date>
    <item>
      <title>Separating  string(variable name) and sub-string into separate variables with multiple delimiters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-string-variable-name-and-sub-string-into-separate/m-p/614070#M179448</link>
      <description>&lt;P&gt;I have a string with a combination of both variable names and string separated by multiple delimiters as below. Sometimes the sub-string are separated by space and some times by comma (,). These have to be identified and separate the variables and sub string into each unique variable. Commas and spaces are also part of the sub-string when embedded in quotes as below .&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have ;
Have='SIFI " " SIUNIT " (" ATOX ", " GRADE ")"' ;output 
Have='SIFI, " " SIUNIT " (", ATOX ", " GRADE ")"' ;output 
run ;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Required Output: Each variable or substring should be separated into individual variable&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;var1	var2	var3	var4	var5	var6	var7	var8
SIFI	" "	SIUNIT	" ("	ATOX	", "	GRADE	")"
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks in advance for your inputs&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Dec 2019 13:31:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-string-variable-name-and-sub-string-into-separate/m-p/614070#M179448</guid>
      <dc:creator>keen_sas</dc:creator>
      <dc:date>2019-12-27T13:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: Separating  string(variable name) and sub-string into separate variables with multiple delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-string-variable-name-and-sub-string-into-separate/m-p/614098#M179465</link>
      <description>&lt;P&gt;Your problem description is a bit incomplete. If you expect a value of " " for variable from that string the a simple space is not a delimiter because that value contains a space. Also you have a ", " desired value which contains two delimiters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, please provide a more exhaustive set of rules for parsing this data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Dec 2019 15:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-string-variable-name-and-sub-string-into-separate/m-p/614098#M179465</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-12-27T15:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: Separating  string(variable name) and sub-string into separate variables with multiple delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-string-variable-name-and-sub-string-into-separate/m-p/614192#M179478</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16518"&gt;@keen_sas&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;1. Treat non-alphanumerics as delimiters.&lt;/P&gt;
&lt;P&gt;2. Treat alphanumerics as delimiters.&lt;/P&gt;
&lt;P&gt;3. In case #2, kill the &lt;EM&gt;endpoint&lt;/EM&gt; commas separately.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                             
  have = 'SIFI, " " SIUNIT " (", ATOX ", " GRADE ")"' ; output ;                                                                        
  have = 'SIFI " " SIUNIT " (" ATOX ", " GRADE ")"'   ; output ;                                                                        
run ;                                                                                                                                   
                                                                                                                                        
data want (keep = have var:) ;                                                                                                          
  set have ;                                                                                                                            
  array vv $ 8 var1-var8 v ;                                                                                                            
  do _n_ = 1 to 4 ;                                                                                                                     
    vv [2 * _n_ - 1] = scan (have, _n_, , "kad") ;                                                                                      
    v = scan (have, _n_, , "ad") ;                                                                                                      
    if char (v,         1 ) = "," then v = substr (v, 2) ;                                                                              
    if char (v, length (v)) = "," then v = substr (v, 1, length (v) - 1) ;                                                              
    vv [2 * _n_] = v ;                                                                                                                  
  end ;                                                                                                                                 
run ;           
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: I assumed the longest parsed component is $8. Adjust if need be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Dec 2019 05:44:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-string-variable-name-and-sub-string-into-separate/m-p/614192#M179478</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-12-28T05:44:00Z</dc:date>
    </item>
    <item>
      <title>Re: Separating  string(variable name) and sub-string into separate variables with multiple delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-string-variable-name-and-sub-string-into-separate/m-p/614193#M179479</link>
      <description>&lt;P&gt;You can use the Q modifier on SCAN() and COUNTW() functions.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  infile cards truncover ;
  row+1;
  input have $200.;
cards;
SIFI " " SIUNIT " (" ATOX ", " GRADE ")"
SIFI, " " SIUNIT " (", ATOX ", " GRADE ")"
;

data want;
  set have ;
  do index=1 to countw(have,' ','q');
    length term $100 ;
    term=scan(have,index,' ','q');
    output;
  end;
  drop have;
run ;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    row    index    term

  1     1       1      SIFI
  2     1       2      " "
  3     1       3      SIUNIT
  4     1       4      " ("
  5     1       5      ATOX
  6     1       6      ", "
  7     1       7      GRADE
  8     1       8      ")"
  9     2       1      SIFI,
 10     2       2      " "
 11     2       3      SIUNIT
 12     2       4      " (",
 13     2       5      ATOX
 14     2       6      ", "
 15     2       7      GRADE
 16     2       8      ")"&lt;/PRE&gt;
&lt;P&gt;Your second string doesn't seem to be following the same rules.&amp;nbsp; Did you want to treat comma as another possible delimiter? If so then include it in the SCAN() and COUNTW() function calls.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;term=scan(have,index,' ,','q');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to remove those quotes in the even numbered values you can use the DEQUOTE() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;term=dequote(scan(have,index,' ,','q'));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Dec 2019 06:06:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-string-variable-name-and-sub-string-into-separate/m-p/614193#M179479</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-28T06:06:17Z</dc:date>
    </item>
  </channel>
</rss>

