<?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: How to count the specific values using array? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-the-specific-values-using-array/m-p/863073#M340939</link>
    <description>&lt;P&gt;When you say " if each observation has the specific values that I am looking for" does that mean any of the variables hold any of the values? Some combination of variables holds all of the values? Some combination of variable has some minimum subset of the values?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you expect the output to look like? I'm not sure what the result should look like.&lt;/P&gt;
&lt;P&gt;Also, are your values actually just 'a' 'c' 'e' or are you substituting short values in place of your actual ones. This is important for solutions because there is a function COUNTC that counts occurrences of single characters in a string which makes part of this pretty easy if the question is single character values but won't work if these are short hand.&lt;/P&gt;
&lt;P&gt;Untested as complete example data not provided&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array x(*) x1-x30;
   /*if you need separate counts of a c e*/
   num_a = countc(cats(of x(*)),'a');
   num_c = countc(cats(of x(*)),'c');
   num_e = countc(cats(of x(*)),'e');
   Num_ace= sum(num_a,num_c,num_e);
   /* if only need total count of a c e*/
   Num_ace2=  countc(cats(of x(*)),'ace');
   /* if using this then the DO loop isn't needed*/
   find= (num_ace &amp;gt; 0);
run;

&lt;/PRE&gt;
&lt;P&gt;The CATS function combines all of the present values of the X variables into one long string to search, the Countc finds the number of times the character appears in the combined string.&lt;/P&gt;
&lt;P&gt;The find= at the end uses SAS behavior of a comparison result returns 1 for true and 0 for false.&lt;/P&gt;</description>
    <pubDate>Wed, 08 Mar 2023 21:30:12 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-03-08T21:30:12Z</dc:date>
    <item>
      <title>How to count the specific values using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-the-specific-values-using-array/m-p/863069#M340935</link>
      <description>&lt;P&gt;Hi! I have question about counting specific values using array, I don't know how to do it, could anyone help me with it? Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have lots of variables like below, and I wanted to do two things, first is to identify if each observation has the specific values that I am looking for, I used below array codes by using 'find=0', and if SAS find ''a', 'c', or 'e' in the variables, then 'find=1'.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data one;
input X1 $ X2 $ .. ....... X30 $;
cards;
a b .......ef
c d&amp;nbsp; &amp;nbsp;..... dg
d c&amp;nbsp; . .... rg
;
find=0;
array X X1-X30;
do over X;
&amp;nbsp; &amp;nbsp; if X in ('a','c','e') then find=1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now&amp;nbsp; I have a question about the second thing that I want to do, I need to know how many specific values in each observation. Like above code, I want to know how many 'a', 'c', and 'e' in total in each row for all 30X variables. Is there any way to achieve that using array or something else? Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Mar 2023 20:30:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-the-specific-values-using-array/m-p/863069#M340935</guid>
      <dc:creator>SAS-questioner</dc:creator>
      <dc:date>2023-03-08T20:30:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the specific values using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-the-specific-values-using-array/m-p/863073#M340939</link>
      <description>&lt;P&gt;When you say " if each observation has the specific values that I am looking for" does that mean any of the variables hold any of the values? Some combination of variables holds all of the values? Some combination of variable has some minimum subset of the values?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you expect the output to look like? I'm not sure what the result should look like.&lt;/P&gt;
&lt;P&gt;Also, are your values actually just 'a' 'c' 'e' or are you substituting short values in place of your actual ones. This is important for solutions because there is a function COUNTC that counts occurrences of single characters in a string which makes part of this pretty easy if the question is single character values but won't work if these are short hand.&lt;/P&gt;
&lt;P&gt;Untested as complete example data not provided&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array x(*) x1-x30;
   /*if you need separate counts of a c e*/
   num_a = countc(cats(of x(*)),'a');
   num_c = countc(cats(of x(*)),'c');
   num_e = countc(cats(of x(*)),'e');
   Num_ace= sum(num_a,num_c,num_e);
   /* if only need total count of a c e*/
   Num_ace2=  countc(cats(of x(*)),'ace');
   /* if using this then the DO loop isn't needed*/
   find= (num_ace &amp;gt; 0);
run;

&lt;/PRE&gt;
&lt;P&gt;The CATS function combines all of the present values of the X variables into one long string to search, the Countc finds the number of times the character appears in the combined string.&lt;/P&gt;
&lt;P&gt;The find= at the end uses SAS behavior of a comparison result returns 1 for true and 0 for false.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Mar 2023 21:30:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-the-specific-values-using-array/m-p/863073#M340939</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-03-08T21:30:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the specific values using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-the-specific-values-using-array/m-p/863078#M340943</link>
      <description>Maybe worth a transpose + PROC FREQ depending on exactly what you require for the output. Are you looking for an output per row? or how many A's in the table entirely?</description>
      <pubDate>Wed, 08 Mar 2023 21:51:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-the-specific-values-using-array/m-p/863078#M340943</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-03-08T21:51:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the specific values using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-the-specific-values-using-array/m-p/863640#M341135</link>
      <description>&lt;P&gt;You can get the count of "a", "c" or "e" by using the compress function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data one;
input X1 $ X2 $ X3 $ X4 $ X5 $;
cards;
a b c d e f
c d e h t f
d c h i h e
a r c e t j
m h b v f p
f j e g a c
;
data two;
set one;
array X(*) X1-X5;
do i=1 to dim(x);
    if X(i) in ('a','c','e') then find=1;
end;
words_len = length(compress(cat(of x:),compress(cat(of x:),'^ace')));
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Mar 2023 07:48:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-the-specific-values-using-array/m-p/863640#M341135</guid>
      <dc:creator>VijayPratap</dc:creator>
      <dc:date>2023-03-12T07:48:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the specific values using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-the-specific-values-using-array/m-p/863642#M341136</link>
      <description>&lt;P&gt;Simply add a count:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;find = 0;
count = 0;
array X X1-X30;
do over X;
  if X in ('a','c','e')
  then do;
    find = 1;
    count + 1;
  end;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Mar 2023 08:41:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-the-specific-values-using-array/m-p/863642#M341136</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-12T08:41:25Z</dc:date>
    </item>
  </channel>
</rss>

