<?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 Divide the text into variables with max 200 length, put attention to not cut word from middle. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Divide-the-text-into-variables-with-max-200-length-put-attention/m-p/937424#M368347</link>
    <description>&lt;P&gt;I tried this code, but it has some issues, such as the &lt;CODE&gt;find&lt;/CODE&gt; function not always working.&lt;/P&gt;&lt;PRE&gt;data ex_5;
	length parts $200;
	set text;

	do i=1 to length(text)-200;
		parts=substr(text, i, 200);
		last_word=scan(parts, countw(parts));
		k=lengthn(last_word);

		if find(text, last_word) then
			do;
				temp=200;
			end;
		else
			do;
				temp=200 - length(last_word);
			end;
		i+(temp-1);
		output;
		n+1;
	end;
run;

data final;
	set ex_5;
	len=length(parts) - length(last_word);

	if find(text, last_word) then
		parts1=parts;
	else
		parts1=substr(parts, 1, len);
	drop parts i len temp;
run;

proc transpose data=final out=final_final;
	id last_word;
	var parts1;
run;&lt;/PRE&gt;</description>
    <pubDate>Mon, 29 Jul 2024 11:12:44 GMT</pubDate>
    <dc:creator>aaaaaaaaaaaaa10</dc:creator>
    <dc:date>2024-07-29T11:12:44Z</dc:date>
    <item>
      <title>Divide the text into variables with max 200 length, put attention to not cut word from middle.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-the-text-into-variables-with-max-200-length-put-attention/m-p/937422#M368341</link>
      <description>&lt;P&gt;data ex_5;&lt;/P&gt;&lt;P&gt;length parts $1000;&lt;/P&gt;&lt;P&gt;set text;&lt;/P&gt;&lt;P&gt;do i=1 to length(text)-200; parts=substr(text, i, 200);&lt;/P&gt;&lt;P&gt;last_word=scan(parts, countw(parts));&lt;/P&gt;&lt;P&gt;k=lengthn(last_word);&lt;/P&gt;&lt;P&gt;if find(text, last_word) then do;&lt;/P&gt;&lt;P&gt;temp=200;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;else do;&lt;/P&gt;&lt;P&gt;temp=200 - length(last_word);&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;i+(temp-1);&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;n+1;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;data final;&lt;/P&gt;&lt;P&gt;set ex_5;&lt;/P&gt;&lt;P&gt;len=length(parts) - length(last_word);&lt;/P&gt;&lt;P&gt;if find(text, last_word) then parts1=parts;&lt;/P&gt;&lt;P&gt;else parts1=substr(parts, 1, len);&lt;/P&gt;&lt;P&gt;drop parts i len temp;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;proc transpose data=final out=final_final;&lt;/P&gt;&lt;P&gt;id last_word;&lt;/P&gt;&lt;P&gt;var parts1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;I tried this code, but it has some issues, such as the &lt;CODE&gt;find&lt;/CODE&gt; function not always working.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2024 11:02:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-the-text-into-variables-with-max-200-length-put-attention/m-p/937422#M368341</guid>
      <dc:creator>aaaaaaaaaaaaa10</dc:creator>
      <dc:date>2024-07-29T11:02:08Z</dc:date>
    </item>
    <item>
      <title>Divide the text into variables with max 200 length, put attention to not cut word from middle.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-the-text-into-variables-with-max-200-length-put-attention/m-p/937424#M368347</link>
      <description>&lt;P&gt;I tried this code, but it has some issues, such as the &lt;CODE&gt;find&lt;/CODE&gt; function not always working.&lt;/P&gt;&lt;PRE&gt;data ex_5;
	length parts $200;
	set text;

	do i=1 to length(text)-200;
		parts=substr(text, i, 200);
		last_word=scan(parts, countw(parts));
		k=lengthn(last_word);

		if find(text, last_word) then
			do;
				temp=200;
			end;
		else
			do;
				temp=200 - length(last_word);
			end;
		i+(temp-1);
		output;
		n+1;
	end;
run;

data final;
	set ex_5;
	len=length(parts) - length(last_word);

	if find(text, last_word) then
		parts1=parts;
	else
		parts1=substr(parts, 1, len);
	drop parts i len temp;
run;

proc transpose data=final out=final_final;
	id last_word;
	var parts1;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jul 2024 11:12:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-the-text-into-variables-with-max-200-length-put-attention/m-p/937424#M368347</guid>
      <dc:creator>aaaaaaaaaaaaa10</dc:creator>
      <dc:date>2024-07-29T11:12:44Z</dc:date>
    </item>
    <item>
      <title>Re: Divide the text into variables with max 200 length, put attention to not cut word from middle.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-the-text-into-variables-with-max-200-length-put-attention/m-p/937429#M368349</link>
      <description>&lt;P&gt;Please do not double-post.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here a quick example for splitting along word boundaries:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
text = "Very long text with a lot of words, which should be split along word boundaries into chunks of a given size";
run;

%let chunk = 50; /* length of individual pieces */

data want;
set have;
length part $&amp;amp;chunk.;
do while (lengthn(text) &amp;gt; 0);
  do until (
    length(part) + length(scan(text,1," ")) + 1 &amp;gt; &amp;amp;chunk.
    or lengthn(text) = 0
  );
    part = catx(" ",part,scan(text,1," "));
    text = substr(text,indexc(text," ") + 1);
  end;
  output;
  part = "";
end;
drop text;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jul 2024 11:42:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-the-text-into-variables-with-max-200-length-put-attention/m-p/937429#M368349</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-07-29T11:42:47Z</dc:date>
    </item>
    <item>
      <title>Re: Divide the text into variables with max 200 length, put attention to not cut word from middle.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-the-text-into-variables-with-max-200-length-put-attention/m-p/937553#M368388</link>
      <description>&lt;P&gt;I would make a copy of the original string, then starting at character 201, search backwards for a blank.&amp;nbsp; Copy the identified length from the copy to the subtext variable (length $200), shift the copy leftwards by the identified number of characters, and repeat, ... until the copy is blank:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want (drop=_: i);
  set have;
  length subtext $200;

  _copy=original;
  do part=1 by 1 while (_copy^=' ');
    do i=201 by -1 while (char(_copy,i)^=' ');
    end;
    subtext=substr(_copy,1,i-1);
    _copy=substr(_copy,i+1);
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note this program assumes there is always blank somewhere from character 2 through character 201, through each modification of variable _COPY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2024 04:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-the-text-into-variables-with-max-200-length-put-attention/m-p/937553#M368388</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-07-30T04:06:02Z</dc:date>
    </item>
    <item>
      <title>Re: Divide the text into variables with max 200 length, put attention to not cut word from middle.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-the-text-into-variables-with-max-200-length-put-attention/m-p/937559#M368392</link>
      <description>&lt;P&gt;Using a regular expression:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    text = "Very long text with a lot of words, which should be split along word boundaries into chunks of a given size";
run;

%let chunk = 50; /* length of individual pieces */

data want;
    set have;
    
    length part $ &amp;amp;chunk.;
    retain rx;
    drop rx;
    
    if _n_ = 1 then do;
        /* Don't add a space after the comma in the following statement! */
        rx = prxparse("/(.{0,&amp;amp;chunk.}\b)/");
    end;
    
    begin = 1;
    end = -1;
    pos = 0;
    len = 0;
    
    call prxnext(rx, begin, end, trim(text), pos, len);
    
    do while (pos &amp;gt; 0 and len&amp;gt; 0);
        part = substr(text, pos, len);
        output;
        call prxnext(rx, begin, end, trim(text), pos, len);
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jul 2024 06:26:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-the-text-into-variables-with-max-200-length-put-attention/m-p/937559#M368392</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2024-07-30T06:26:19Z</dc:date>
    </item>
  </channel>
</rss>

