<?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: Split long character string using multiple delimiters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Split-long-character-string-using-multiple-delimiters/m-p/919393#M362138</link>
    <description>&lt;P&gt;Instead of building NYSTR word by word, why not examine a copy of the source string at character 200 and work backwards until a blank or semi-colon character is found.&amp;nbsp; Copy character 1 through the located character to NYSTR.&amp;nbsp; Then move the rest of the copy-of-source-string to start in column 1, and repeat:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tmp (drop=_:);
  set db.cm (keep=subject cmtrt_product recordid);
  _string=cmtrt_product;

  length nystr $200;
  do while (_string^=' ');
    do _col=200 by -1 until (char(_string,_col) in (' ',';'));
    end;
    nystr=substr(_string,1,_col);
    output;
    _string=substr(_string,_col+1);
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 08 Mar 2024 01:47:52 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2024-03-08T01:47:52Z</dc:date>
    <item>
      <title>Split long character string using multiple delimiters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-long-character-string-using-multiple-delimiters/m-p/919381#M362131</link>
      <description>&lt;DIV&gt;Hello,&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;I have this code and it works ok, but I have text&amp;nbsp; with words that are separated by space AND semicolons so that no column is greater than 200.&amp;nbsp; So, if either is space or semicolon&amp;nbsp; encountered approaching 200, I would like ok to split while keeping the integrity of the data (i.e.&amp;nbsp; keeping spaces and semicolons in the data in all columns)&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Here is an example of text string:&lt;/DIV&gt;
&lt;DIV&gt;BUPLEURUM SPP. ROOT;CINNAMOMUM CASSIA TWIG;FOSSILIA OSSIS MASTODI;OYSTER SHELL;PANAX GINSENG ROOT;PINELLIA TERNATA TUBER;PORIA COCOS SCLEROTIUM;RHEUM SPP. ROOT WITH RHIZOME;SCUTELLARIA BAICALENSIS&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;data tmp (drop= i word); set db.cm (keep=subject cmtrt_product recordid);&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; length nystr $200;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; cmtrt_product = compbl(cmtrt_product);&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; do i = 1 to countw(cmtrt_product,' ');&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; word = scan(cmtrt_product,i,' ');&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; if length(nystr) + length(word) + 1 &amp;gt; 200 then do;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; output;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; nystr = word;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; end;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; else nystr = catx(' ',nystr,scan(cmtrt_product,i,' '));&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; end;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; if nystr ne '' then output;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;proc sort data=tmp; by subject recordid;&lt;/DIV&gt;
&lt;DIV&gt;proc transpose data=tmp out=want (drop=_name_);&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; by subject recordid ;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; var nystr;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 23:53:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-long-character-string-using-multiple-delimiters/m-p/919381#M362131</guid>
      <dc:creator>jenim514</dc:creator>
      <dc:date>2024-03-07T23:53:54Z</dc:date>
    </item>
    <item>
      <title>Re: Split long character string using multiple delimiters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-long-character-string-using-multiple-delimiters/m-p/919387#M362134</link>
      <description>&lt;P&gt;You really should show us what you expect for the output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would actually expect the use of the explicit delimiters you talk about in the SCAN and COUNTW functions. You only show using space as a delimiter. SCAN and COUNTW can use multiple delimiters such as this to use space and semicolon&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;do i = 1 to countw(cmtrt_product,' ;');&lt;/PRE&gt;
&lt;P&gt;I might suspect that it would make more sense to just use the semicolon though so that the concepts are together in the output instead of split between multiple observations.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 23:54:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-long-character-string-using-multiple-delimiters/m-p/919387#M362134</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-03-07T23:54:15Z</dc:date>
    </item>
    <item>
      <title>Re: Split long character string using multiple delimiters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-long-character-string-using-multiple-delimiters/m-p/919393#M362138</link>
      <description>&lt;P&gt;Instead of building NYSTR word by word, why not examine a copy of the source string at character 200 and work backwards until a blank or semi-colon character is found.&amp;nbsp; Copy character 1 through the located character to NYSTR.&amp;nbsp; Then move the rest of the copy-of-source-string to start in column 1, and repeat:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tmp (drop=_:);
  set db.cm (keep=subject cmtrt_product recordid);
  _string=cmtrt_product;

  length nystr $200;
  do while (_string^=' ');
    do _col=200 by -1 until (char(_string,_col) in (' ',';'));
    end;
    nystr=substr(_string,1,_col);
    output;
    _string=substr(_string,_col+1);
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Mar 2024 01:47:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-long-character-string-using-multiple-delimiters/m-p/919393#M362138</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-03-08T01:47:52Z</dc:date>
    </item>
  </channel>
</rss>

