<?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: count times of appearance for elements in a data step array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598256#M172521</link>
    <description>&lt;P&gt;You need to have some row id to generate the expected results of the counts per row, instead of overall counts.&lt;/P&gt;
&lt;P&gt;If there isn't one then add one during the transpose step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tall ;
  set have;
  array list var1-var4 ;
  row+1;
  do col=1 to dim(list);
      word = list[col];
      output;
  end;
run;

proc freq data=tall;
  tables row*word / out=counts;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 21 Oct 2019 20:20:38 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-10-21T20:20:38Z</dc:date>
    <item>
      <title>count times of appearance for elements in a data step array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598139#M172447</link>
      <description>&lt;P&gt;Hello experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suppose I have a dataset as follows:&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;data have; &lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;input var1$ 1-12 var2$ 13-24 var3$ 25-36 var4$ 37-48; &lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;datalines; &lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;ElpasoDiner Amazon&amp;nbsp; &amp;nbsp; &amp;nbsp; Amazon&amp;nbsp; &amp;nbsp; &amp;nbsp; WilliamsS&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;MickeyD&amp;nbsp; &amp;nbsp; &amp;nbsp;SecondCare&amp;nbsp; Bluecross&amp;nbsp; &amp;nbsp;MickeyD&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;ElpasoDiner ElpasoDiner Amazon&amp;nbsp; &amp;nbsp; &amp;nbsp; United&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am struggling to come up with a way to count the distinct elements in var1-var4 (using an array anyways). Is there a way to get count like this:&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;ElpasoDiner Amazon WilliamsS&amp;nbsp;MickeyD&amp;nbsp;SecondCare&amp;nbsp;Bluecross United&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;1 2 1 0 0 0 0&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;0 0 0 2 1 1 0&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;2 1 0 0 0 0 1&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The example I gave here is an ideal situation, but in my real dataset I often do not know the contents in each row (i.e. I do not know exactly what is in var1 - var4). And there are multiple possible text strings for var1 - var4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 14:40:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598139#M172447</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2019-10-21T14:40:32Z</dc:date>
    </item>
    <item>
      <title>Re: count distinct elements in a data step array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598142#M172450</link>
      <description>&lt;P&gt;are they single char values as your sample suggests?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming your sample is truly representative, it;s easy&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var1 $ var2 $ var3 $ var4 $;
datalines;
a a b c
a b c d
a a a b
;

data want;
 set have;
 array t a b c d;
 _k=cats(of var:);
 do over t;
  t=countc(_k,strip(vname(t)));
 end;
 drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Oct 2019 14:13:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598142#M172450</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-21T14:13:14Z</dc:date>
    </item>
    <item>
      <title>Re: count distinct elements in a data step array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598143#M172451</link>
      <description>Thanks novinosrin! They are actually longer names than just abcd in my datasets, sometimes as long as over 10 characters.</description>
      <pubDate>Mon, 21 Oct 2019 14:16:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598143#M172451</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2019-10-21T14:16:16Z</dc:date>
    </item>
    <item>
      <title>Re: count distinct elements in a data step array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598147#M172453</link>
      <description>&lt;P&gt;Can you post a "comprehensive" sample plz&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 14:17:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598147#M172453</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-21T14:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: count distinct elements in a data step array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598149#M172454</link>
      <description>Sorry the extra blanks are not being shown in my text.&lt;BR /&gt;</description>
      <pubDate>Mon, 21 Oct 2019 14:30:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598149#M172454</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2019-10-21T14:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: count distinct elements in a data step array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598150#M172455</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274317"&gt;@aaronh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose I have a dataset as follows:&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;data have; &lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;input var1$ 1-12 var2$ 13-24 var3$ 25-36 var4$ 37-48; &lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;datalines; &lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;ElpasoDiner Amazon&amp;nbsp; &amp;nbsp; &amp;nbsp; Amazon&amp;nbsp; &amp;nbsp; &amp;nbsp; WilliamsS&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;MickeyD&amp;nbsp; &amp;nbsp; &amp;nbsp;SecondCare&amp;nbsp; Bluecross&amp;nbsp; &amp;nbsp;MickeyD&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;ElpasoDiner ElpasoDiner Amazon&amp;nbsp; &amp;nbsp; &amp;nbsp; United&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am struggling to come up with a way to count the distinct elements in var1-var4 (using an array anyways).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The example I gave here is an ideal situation, but in my real dataset I often do not know the contents in each row (i.e. I do not know exactly what is in var1 - var4). And there are multiple possible text strings for var1 - var4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You really need to show what you expect the output to look like for a given example input.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 14:39:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598150#M172455</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-21T14:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: count distinct elements in a data step array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598151#M172456</link>
      <description>&lt;P&gt;Okay a generic one and linear simple safe method I think&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*1st pass to get distinct values in a macor var list*/
data _null_;
 do _n_=1 by 1 until(z);
  set have end=z;
  array v var:;
  array t(99)$32 _temporary_;/*Assign length of 32 which is variable name maximun length*/
  do over v;
  if v not in t then do;
  n+1;
  t(n)=v;
  end;
  end;
 end;
 call symputx('list',catx(' ',of t(*)));
run;

%put &amp;amp;=list;
/*2nd pass linear look up */
data want;
 set have;
 array v(*) var:;
 array t(*) &amp;amp;list;
 do i=1 to dim(t);
  t(i)=0;
  do j =1 to dim(v);
   if vname(t(i))=v(j) then t(i)=sum(t(i),1);
  end;
 end;
 drop i j;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Oct 2019 14:41:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598151#M172456</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-21T14:41:53Z</dc:date>
    </item>
    <item>
      <title>Re: count times of appearance for elements in a data step array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598161#M172460</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274317"&gt;@aaronh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another way is to read individual Vars into a Data Set. You can specify the length of the Var as a macro.&lt;/P&gt;
&lt;P&gt;Then use Proc Freq.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Vlen=12;

data have;
infile datalines;
input ;
/* put _infile_; */
length Var $&amp;amp;Vlen;
do i = 1 to countw(_infile_, ' ');
   Var = scan(_infile_, i);
   output;
end;
drop i;
datalines;
ElpasoDiner Amazon      Amazon      WilliamsS
MickeyD     SecondCare  Bluecross   MickeyD
ElpasoDiner ElpasoDiner Amazon      United
;
run;


proc freq data = have;
   tables Var / out = FreqCount nopercent nocum;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Oct 2019 15:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598161#M172460</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-10-21T15:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: count times of appearance for elements in a data step array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598256#M172521</link>
      <description>&lt;P&gt;You need to have some row id to generate the expected results of the counts per row, instead of overall counts.&lt;/P&gt;
&lt;P&gt;If there isn't one then add one during the transpose step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tall ;
  set have;
  array list var1-var4 ;
  row+1;
  do col=1 to dim(list);
      word = list[col];
      output;
  end;
run;

proc freq data=tall;
  tables row*word / out=counts;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Oct 2019 20:20:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598256#M172521</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-21T20:20:38Z</dc:date>
    </item>
    <item>
      <title>Re: count times of appearance for elements in a data step array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598471#M172589</link>
      <description>&lt;P&gt;Thanks to everyone for your input!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;AH&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2019 17:59:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-times-of-appearance-for-elements-in-a-data-step-array/m-p/598471#M172589</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2019-10-22T17:59:29Z</dc:date>
    </item>
  </channel>
</rss>

