<?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: take the character variable length without chopping a word in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/take-the-character-variable-length-without-chopping-a-word/m-p/842481#M333132</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let len=20;

data test ;
  input long $char40.;
cards;
This is longer than 20 characters. 
This one, is exactly 20 characters.
But this short.
;
data want;
 set test;
 n+1;
 length want $ 400  ;
 do i=1 to countw(long,' ');
   _want=want;
   temp=scan(long,i,' ');
   want=catx(' ',want,temp);
   if length(want)&amp;gt;&amp;amp;len. then do;want=_want;output;want=temp; end;
 end;
 output;
 drop i temp _want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 04 Nov 2022 12:05:14 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2022-11-04T12:05:14Z</dc:date>
    <item>
      <title>take the character variable length without chopping a word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/take-the-character-variable-length-without-chopping-a-word/m-p/842425#M333112</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a variable which has values more than 200 in its length. So I am trying to take that variable until length 200 without chopping a word. Can you please help me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Adithya&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2022 23:45:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/take-the-character-variable-length-without-chopping-a-word/m-p/842425#M333112</guid>
      <dc:creator>chinna0369</dc:creator>
      <dc:date>2022-11-03T23:45:09Z</dc:date>
    </item>
    <item>
      <title>Re: take the character variable length without chopping a word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/take-the-character-variable-length-without-chopping-a-word/m-p/842427#M333113</link>
      <description>How do you know that the variable has more than 200 characters?  &lt;BR /&gt;&lt;BR /&gt;Are you able to define a variable that is 201 characters long, so you can inspect the 201st character?</description>
      <pubDate>Thu, 03 Nov 2022 23:56:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/take-the-character-variable-length-without-chopping-a-word/m-p/842427#M333113</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-11-03T23:56:02Z</dc:date>
    </item>
    <item>
      <title>Re: take the character variable length without chopping a word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/take-the-character-variable-length-without-chopping-a-word/m-p/842448#M333119</link>
      <description>&lt;P&gt;You could just rebuild the string word by word and stop when it gets too long.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  length short $200 ;
  short = scan(long,1,' ');
  do index=2 to 100 while (min(length(long),200)&amp;gt;=length(catx(' ',short,scan(long,index,' '))));
    short = catx(' ',short,scan(long,index,' '));
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Or ask FINDC() to find the place to break.&amp;nbsp; Remember it is ok if the space is at position 201.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test ;
  input long $char40.;
  length short $20 ;
  short=long;
  location=findc(long,' ',-21);
  if location then short=substr(short,1,location);
cards;
----+---10----+---20----5---30----5---40
This is longer than 20 characters. 
This one, is exactly 20 characters.
But this short.
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    long                                               short            location

 1     ----+---10----+---20----5---30----5---40    ----+---10----+---20        0
 2     This is longer than 20 characters.          This is longer than        20
 3     This one, is exactly 20 characters.         This one, is exactly       21
 4     But this short.                             But this short.            21

&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Nov 2022 03:59:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/take-the-character-variable-length-without-chopping-a-word/m-p/842448#M333119</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-11-04T03:59:19Z</dc:date>
    </item>
    <item>
      <title>Re: take the character variable length without chopping a word</title>
      <link>https://communities.sas.com/t5/SAS-Programming/take-the-character-variable-length-without-chopping-a-word/m-p/842481#M333132</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let len=20;

data test ;
  input long $char40.;
cards;
This is longer than 20 characters. 
This one, is exactly 20 characters.
But this short.
;
data want;
 set test;
 n+1;
 length want $ 400  ;
 do i=1 to countw(long,' ');
   _want=want;
   temp=scan(long,i,' ');
   want=catx(' ',want,temp);
   if length(want)&amp;gt;&amp;amp;len. then do;want=_want;output;want=temp; end;
 end;
 output;
 drop i temp _want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Nov 2022 12:05:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/take-the-character-variable-length-without-chopping-a-word/m-p/842481#M333132</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-11-04T12:05:14Z</dc:date>
    </item>
  </channel>
</rss>

