<?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 Count specific character for each variable using array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-specific-character-for-each-variable-using-array/m-p/786168#M250974</link>
    <description>&lt;P&gt;Hello, I have a dataset as it is given below. I need to create another table (single row) to have counts of 'f' for each variable. I am able to using proc sql but i am wondering how i can do that using arrays or proc iml which is new procedure in my sas knowledge.&lt;/P&gt;
&lt;P&gt;Thank you.&lt;BR /&gt;data one;&lt;BR /&gt;input a $ b $ c $ d e;&lt;BR /&gt;cards;&lt;BR /&gt;a f a 1 3&lt;BR /&gt;f b f 2 4&lt;BR /&gt;a a a f 5&lt;BR /&gt;f f b 3 5&lt;BR /&gt;a a a f 6&lt;BR /&gt;a a a f 7&lt;BR /&gt;a a a 2 8&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Wed, 15 Dec 2021 13:54:43 GMT</pubDate>
    <dc:creator>sascode</dc:creator>
    <dc:date>2021-12-15T13:54:43Z</dc:date>
    <item>
      <title>Count specific character for each variable using array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-specific-character-for-each-variable-using-array/m-p/786168#M250974</link>
      <description>&lt;P&gt;Hello, I have a dataset as it is given below. I need to create another table (single row) to have counts of 'f' for each variable. I am able to using proc sql but i am wondering how i can do that using arrays or proc iml which is new procedure in my sas knowledge.&lt;/P&gt;
&lt;P&gt;Thank you.&lt;BR /&gt;data one;&lt;BR /&gt;input a $ b $ c $ d e;&lt;BR /&gt;cards;&lt;BR /&gt;a f a 1 3&lt;BR /&gt;f b f 2 4&lt;BR /&gt;a a a f 5&lt;BR /&gt;f f b 3 5&lt;BR /&gt;a a a f 6&lt;BR /&gt;a a a f 7&lt;BR /&gt;a a a 2 8&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Dec 2021 13:54:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-specific-character-for-each-variable-using-array/m-p/786168#M250974</guid>
      <dc:creator>sascode</dc:creator>
      <dc:date>2021-12-15T13:54:43Z</dc:date>
    </item>
    <item>
      <title>Re: Count specific character for each variable using array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-specific-character-for-each-variable-using-array/m-p/786172#M250976</link>
      <description>&lt;P&gt;One way (note change of variable D to character as written the "f" on some rows would be missing as your code has D as numeric.&lt;/P&gt;
&lt;PRE&gt;data one;
input a $ b $ c $ d $ e;
cards;
a f a 1 3
f b f 2 4
a a a f 5
f f b 3 5
a a a f 6
a a a f 7
a a a 2 8
;

data want;
   set one;
   array val(*) a b c d ;
   count = countc(cats(of val(*)),'f');
run;&lt;/PRE&gt;
&lt;P&gt;Probably not the way you expected to see an array used though.&lt;/P&gt;
&lt;P&gt;Perhaps you have an actual more complex problem in mind than counting a single character. If so you should be a bit more explicit. The Countc function counts a specific single character in this case, or use 'fF' to count upper and lower case f. The array is not actually needed though is a handy shortcut using the " of arrayname(*) " to process all the variables defined in the array especially with longer variable names. Countc (cats(a,b,c,d), 'f') would work without the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215702"&gt;@sascode&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello, I have a dataset as it is given below. I need to create another table (single row) to have counts of 'f' for each variable. I am able to using proc sql but i am wondering how i can do that using arrays or proc iml which is new procedure in my sas knowledge.&lt;/P&gt;
&lt;P&gt;Thank you.&lt;BR /&gt;data one;&lt;BR /&gt;input a $ b $ c $ d e;&lt;BR /&gt;cards;&lt;BR /&gt;a f a 1 3&lt;BR /&gt;f b f 2 4&lt;BR /&gt;a a a f 5&lt;BR /&gt;f f b 3 5&lt;BR /&gt;a a a f 6&lt;BR /&gt;a a a f 7&lt;BR /&gt;a a a 2 8&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Dec 2021 15:40:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-specific-character-for-each-variable-using-array/m-p/786172#M250976</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-12-15T15:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: Count specific character for each variable using array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-specific-character-for-each-variable-using-array/m-p/786175#M250977</link>
      <description>Thank you, this works fine,&lt;BR /&gt;i was expecting to see count column transposed but it solved anyway.</description>
      <pubDate>Wed, 15 Dec 2021 14:16:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-specific-character-for-each-variable-using-array/m-p/786175#M250977</guid>
      <dc:creator>sascode</dc:creator>
      <dc:date>2021-12-15T14:16:20Z</dc:date>
    </item>
    <item>
      <title>Re: Count specific character for each variable using array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-specific-character-for-each-variable-using-array/m-p/786196#M250982</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215702"&gt;@sascode&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you, this works fine,&lt;BR /&gt;i was expecting to see count column transposed but it solved anyway.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;As I said, one way.&lt;/P&gt;
&lt;P&gt;Another would be to examine each variable in the array and iterate a counter:&lt;/P&gt;
&lt;PRE&gt;data want;
   set one;
   array val(*) a b c d ;
   do i=1 to dim(val);
      count = sum(count,countc(val[i],'f'));
   end;
   drop i;
run;&lt;/PRE&gt;
&lt;P&gt;Depending on how you were thinking of transpose, transposed data might be one way to not use an array or variable list.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Dec 2021 15:46:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-specific-character-for-each-variable-using-array/m-p/786196#M250982</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-12-15T15:46:11Z</dc:date>
    </item>
    <item>
      <title>Re: Count specific character for each variable using array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-specific-character-for-each-variable-using-array/m-p/786286#M251021</link>
      <description>&lt;P&gt;Here is IML code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input a $ b $ c $ d $ e;
cards;
a f a 1 3
f b f 2 4
a a a f 5
f f b 3 5
a a a f 6
a a a f 7
a a a 2 8
;



proc iml;
use one;
read all var _char_ into x;
close; 
count=(x='f')[,+]; 
create  count var {count};
append;
close;
quit;

data want2;
merge one count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Dec 2021 12:22:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-specific-character-for-each-variable-using-array/m-p/786286#M251021</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-12-16T12:22:36Z</dc:date>
    </item>
  </channel>
</rss>

