<?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 Create a Variable based on various character lengths in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667319#M199792</link>
    <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create a new field with a value determined by a formula applied to the character length of another field.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The field, called targets, contains a series of three characters, separated by a space, except for the end (e.g., KOR TUR THA ITA VNM USA CHN IND FRA POL MEX MYS IDN).&amp;nbsp; In Excel the formula is easy: =(LEN(E2)+1)/4; where E2 is the cell referenced.&amp;nbsp; In the Excel formula, you simply add 1 to make up for the lack of a trailing blank and then divid by four to get the number of 3-character targets.&amp;nbsp; For that example, the value is 13.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are hundreds of thousands of rows in the dataset with varying 3-character counts in each.&lt;/P&gt;
&lt;P&gt;Is there a way to do that in SAS?&amp;nbsp; Thanks&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jul 2020 00:26:04 GMT</pubDate>
    <dc:creator>texasmfp</dc:creator>
    <dc:date>2020-07-07T00:26:04Z</dc:date>
    <item>
      <title>Create a Variable based on various character lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667319#M199792</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create a new field with a value determined by a formula applied to the character length of another field.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The field, called targets, contains a series of three characters, separated by a space, except for the end (e.g., KOR TUR THA ITA VNM USA CHN IND FRA POL MEX MYS IDN).&amp;nbsp; In Excel the formula is easy: =(LEN(E2)+1)/4; where E2 is the cell referenced.&amp;nbsp; In the Excel formula, you simply add 1 to make up for the lack of a trailing blank and then divid by four to get the number of 3-character targets.&amp;nbsp; For that example, the value is 13.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are hundreds of thousands of rows in the dataset with varying 3-character counts in each.&lt;/P&gt;
&lt;P&gt;Is there a way to do that in SAS?&amp;nbsp; Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jul 2020 00:26:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667319#M199792</guid>
      <dc:creator>texasmfp</dc:creator>
      <dc:date>2020-07-07T00:26:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Variable based on various character lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667320#M199793</link>
      <description>&lt;P&gt;Are you eventually looking for the countw() function documented &lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p18xi2516ihygyn1qg1b1nby326k.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;here&lt;/A&gt;?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
  string='KOR TUR THA ITA VNM USA CHN IND FRA POL MEX MYS IDN';
  n_terms=countw(string);
run;
proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1594082822342.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46955iA7E8D8BF81547AA0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1594082822342.png" alt="Patrick_0-1594082822342.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jul 2020 00:47:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667320#M199793</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-07-07T00:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Variable based on various character lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667323#M199795</link>
      <description>&lt;P&gt;You should be able to do the same:&lt;/P&gt;&lt;PRE&gt;value=(length(targets) + 1)/4;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Jul 2020 00:49:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667323#M199795</guid>
      <dc:creator>ketpt42</dc:creator>
      <dc:date>2020-07-07T00:49:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Variable based on various character lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667325#M199797</link>
      <description>Function countw counts words</description>
      <pubDate>Tue, 07 Jul 2020 00:55:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667325#M199797</guid>
      <dc:creator>sustagens</dc:creator>
      <dc:date>2020-07-07T00:55:11Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Variable based on various character lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667328#M199799</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/278672"&gt;@ketpt42&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your formula won't work for an empty string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
  string='KOR TUR THA ITA VNM USA CHN IND FRA POL MEX MYS IDN';
  n_terms=countw(string);
  n_terms2=(length(string) + 1)/4;
  n_terms3=lengthn(compress(string))/3;
  output;
  string=' ';
  n_terms=countw(string);
  n_terms2=(length(string) + 1)/4;
  n_terms3=lengthn(compress(string))/3;
  output;
  stop;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1594083425606.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46956iA9100366B386D74F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1594083425606.png" alt="Patrick_0-1594083425606.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jul 2020 00:57:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667328#M199799</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-07-07T00:57:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Variable based on various character lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667329#M199800</link>
      <description>It wasn't clear from the information given that the value would ever be missing. Your solution was more elegant anyway.</description>
      <pubDate>Tue, 07 Jul 2020 01:01:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-Variable-based-on-various-character-lengths/m-p/667329#M199800</guid>
      <dc:creator>ketpt42</dc:creator>
      <dc:date>2020-07-07T01:01:39Z</dc:date>
    </item>
  </channel>
</rss>

