<?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 counting certain characters in a variable in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/counting-certain-characters-in-a-variable/m-p/388166#M65907</link>
    <description>&lt;P&gt;I have a variable that looks kind of like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;variable1
FLZZZZZ
DFZZZZZ
DGLZZZZ
FMNZZZZ
DZZZZZZ
FGZZZZZ
FGLZZZZ&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I need to count the occurrences of F&amp;nbsp;&lt;/P&gt;&lt;P&gt;the occurrences of L&lt;/P&gt;&lt;P&gt;the occurrences of L and F&lt;/P&gt;&lt;P&gt;and the occurrences of L or F&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so that it looks kind of like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just_F &amp;nbsp;| &amp;nbsp;Just_L &amp;nbsp;| L_and_F | L_or_F&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been using proc sql and trying COUNT&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count(variable1,'FL')&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;sum(case when variable1 = 'F' then 1 else 0 end) as Just_F,
sum(case when variable1 = 'L' then 1 else 0 end) as Just_L,
sum(case when variable1 = 'F' or variable1 = 'L' then 1 else 0 end) as L_or_F,
sum(case when variable1 = 'F' and variable1 = 'L' then 1 else 0 end) as L_and_F&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;what I get from proc sql sum cases is all 0 I think because it's not taking into account the other characters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please Help! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 15 Aug 2017 14:44:04 GMT</pubDate>
    <dc:creator>serrld113</dc:creator>
    <dc:date>2017-08-15T14:44:04Z</dc:date>
    <item>
      <title>counting certain characters in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/counting-certain-characters-in-a-variable/m-p/388166#M65907</link>
      <description>&lt;P&gt;I have a variable that looks kind of like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;variable1
FLZZZZZ
DFZZZZZ
DGLZZZZ
FMNZZZZ
DZZZZZZ
FGZZZZZ
FGLZZZZ&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I need to count the occurrences of F&amp;nbsp;&lt;/P&gt;&lt;P&gt;the occurrences of L&lt;/P&gt;&lt;P&gt;the occurrences of L and F&lt;/P&gt;&lt;P&gt;and the occurrences of L or F&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so that it looks kind of like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just_F &amp;nbsp;| &amp;nbsp;Just_L &amp;nbsp;| L_and_F | L_or_F&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been using proc sql and trying COUNT&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count(variable1,'FL')&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;sum(case when variable1 = 'F' then 1 else 0 end) as Just_F,
sum(case when variable1 = 'L' then 1 else 0 end) as Just_L,
sum(case when variable1 = 'F' or variable1 = 'L' then 1 else 0 end) as L_or_F,
sum(case when variable1 = 'F' and variable1 = 'L' then 1 else 0 end) as L_and_F&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;what I get from proc sql sum cases is all 0 I think because it's not taking into account the other characters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please Help! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2017 14:44:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/counting-certain-characters-in-a-variable/m-p/388166#M65907</guid>
      <dc:creator>serrld113</dc:creator>
      <dc:date>2017-08-15T14:44:04Z</dc:date>
    </item>
    <item>
      <title>Re: counting certain characters in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/counting-certain-characters-in-a-variable/m-p/388176#M65908</link>
      <description>&lt;P&gt;You're checking if the variable is equal to the F/L rather than counting them in that code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use COUNT or COUNTW.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2017 14:52:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/counting-certain-characters-in-a-variable/m-p/388176#M65908</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-15T14:52:09Z</dc:date>
    </item>
    <item>
      <title>Re: counting certain characters in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/counting-certain-characters-in-a-variable/m-p/388178#M65909</link>
      <description>&lt;P&gt;I'm not sure you can do it with proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using datastep you can try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input var1 $;
cards;
FLZZZZZ
DFZZZZZ
DGLZZZZ
FMNZZZZ
DZZZZZZ
FGZZZZZ
FGLZZZZ
; run;

data _null_;
 set have end=eof;
   retain Just_f Just_L L_and_F L_or_F ;
   array count  Just_f Just_L L_and_F L_or_F ;
   count_F=0; count_L=0;

   do i=1 to length(var1);
        char = substr(var1, i, 1);
        if char = 'F' then do; count_F=1; Just_F +1; end; else
        if char = 'L' then do; count_L=1; Just_L +1; end;
  end;
  if count_F and count_l then L_and_F +1;
  if Count_F or count_L then L_or_F +1;

  if eof then put Just_f= Just_L= L_and_F= L_or_F= ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Aug 2017 14:55:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/counting-certain-characters-in-a-variable/m-p/388178#M65909</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-08-15T14:55:07Z</dc:date>
    </item>
    <item>
      <title>Re: counting certain characters in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/counting-certain-characters-in-a-variable/m-p/388184#M65910</link>
      <description>&lt;P&gt;You may prefer use function COUNTC (thanks to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input var1 $;
cards;
FLZZZZZ
DFZZZZZ
DGLZZZZ
FMNZZZZ
DZZZZZZ
FGZZZZZ
FGLZZZZ
; run;

data _null_;
 set have end=eof;
   retain Just_f Just_L L_and_F L_or_F ;
   array count  Just_f Just_L L_and_F L_or_F ;

   count_F = countc(var1,'F');
   count_L = countc(var1,'L');
   Just_F + count_F;
   Just_L + count_L;
   L_or_F = L_or_F + countc(var1,'LF');
   if count_F and count_L then L_and_F +1;

  if eof then put Just_f= Just_L= L_and_F= L_or_F= ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Aug 2017 15:03:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/counting-certain-characters-in-a-variable/m-p/388184#M65910</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-08-15T15:03:38Z</dc:date>
    </item>
    <item>
      <title>Re: counting certain characters in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/counting-certain-characters-in-a-variable/m-p/388187#M65911</link>
      <description>&lt;P&gt;You can do this in SQL, but a data step seems much more straightforward to me, since its a single pass of the data as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
	create table want as select sum(FCount&amp;gt;0) as FTotal, sum(LCount&amp;gt;0) as LTotal, 
		sum(fcount&amp;gt;0 and lcount&amp;gt;0) as FLTotal, sum(fcount&amp;gt;0 or lcount&amp;gt;0) as 
		FLoptional from (select a.*, countc(var1, 'F') as FCount, countc(var1, 'L') 
		as LCount from have as a);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Aug 2017 15:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/counting-certain-characters-in-a-variable/m-p/388187#M65911</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-15T15:11:10Z</dc:date>
    </item>
  </channel>
</rss>

