<?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 occurrances of a variable's value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/621785#M182844</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
	input client :$50.;
	datalines;
A
B
A,B
B,A
A,B,C
B
C
D
;
run;

data want;
 do until(z);
  set have end=z;
  array cl(999999) $10 _temporary_;
  array co(999999) _temporary_;
  length temp $10;
  do _n_=1 to countw(client,',');
   temp=scan(client,_n_,',');
   k=whichc(temp, of cl(*));
   if k then co(k)=sum(co(k),1);
   else do;
    _iorc_+1;
    cl(_iorc_)=temp;
	co(_iorc_)=1;
   end;
  end;
 end;
 do _n_=1 to dim(cl)-cmiss(of cl(*));
  client=cl(_n_);
  Count=co(_n_);
  output;
 end;
 keep client count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 02 Feb 2020 16:54:47 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-02-02T16:54:47Z</dc:date>
    <item>
      <title>Count occurrances of a variable's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/621731#M182821</link>
      <description>&lt;P&gt;hi all; may you please assist. I have a dataset where the client column can have multiple clients. I need count the occurrences of the clients showing in the data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is the example of dataset: Count is the data column needed here, ie, Client A appeared 4 times in the data while B showed 5 times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="128" style="width: 96pt; border-collapse: collapse;" border="0" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL width="64" style="width: 48pt;" /&gt; &lt;COL width="64" style="width: 48pt;" /&gt;&lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD width="64" height="20" style="border: 0px black; border-image: none; width: 48pt; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Client&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="64" style="border: 0px black; border-image: none; width: 48pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Count&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;A&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;B&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;A,B&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;B,A&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;A,B,C&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;B&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;C&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;D&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data wanted:&lt;/P&gt;
&lt;TABLE width="128" style="width: 96pt; border-collapse: collapse;" border="0" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL width="64" style="width: 48pt;" /&gt; &lt;COL width="64" style="width: 48pt;" /&gt;&lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD width="64" height="20" style="border: 0px black; border-image: none; width: 48pt; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Client&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="64" style="border: 0px black; border-image: none; width: 48pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Count&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;A&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;4&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;B&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;5&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;C&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;D&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;1&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks ahead of time for your assistance.&lt;/P&gt;</description>
      <pubDate>Sun, 02 Feb 2020 06:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/621731#M182821</guid>
      <dc:creator>Suzy_Cat</dc:creator>
      <dc:date>2020-02-02T06:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: Count occurrances of a variable's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/621732#M182822</link>
      <description>&lt;P&gt;Use an intermediate step to separate your client variable into 1 client per observation (using scan). Then use proc freq to count the instances of each client:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input client :$50.;
	datalines;
A
B
A,B
B,A
A,B,C
B
C
D
;
run;

data temp;
	set have;
	i=1;

	do until (scan(client, i, ',')='');
		client_separated=compress(scan(client, i, ','));
		output;
		i+1;
	end;
	drop i client;
run;

proc freq data=temp noprint;
	tables client_separated / out=want(drop=percent);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 02 Feb 2020 07:10:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/621732#M182822</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2020-02-02T07:10:02Z</dc:date>
    </item>
    <item>
      <title>Re: Count occurrances of a variable's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/621733#M182823</link>
      <description>hi unison, that is super. it is exactly what I need to achieve!</description>
      <pubDate>Sun, 02 Feb 2020 07:20:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/621733#M182823</guid>
      <dc:creator>Suzy_Cat</dc:creator>
      <dc:date>2020-02-02T07:20:59Z</dc:date>
    </item>
    <item>
      <title>Re: Count occurrances of a variable's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/621771#M182839</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
	input client :$50.;
	datalines;
A
B
A,B
B,A
A,B,C
B
C
D
;
run;

data _null_ ;
 if _n_=1 then do;
   dcl hash H (ordered: "A") ;
   h.definekey  ("client") ;
   h.definedata ("client","count") ;
   h.definedone () ;
 end;
 set have(rename=client=_client) end=z;
 length client $10;
 do _n_=1 to countw(_client,',');
  client=scan(_client,_n_,',');
  if h.find() = 0 then count=sum(count,1);
  else count=1;
  h.replace();
 end;
 if z;
 h.output(dataset:'want');
run;

proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 02 Feb 2020 14:41:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/621771#M182839</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-02T14:41:24Z</dc:date>
    </item>
    <item>
      <title>Re: Count occurrances of a variable's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/621785#M182844</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
	input client :$50.;
	datalines;
A
B
A,B
B,A
A,B,C
B
C
D
;
run;

data want;
 do until(z);
  set have end=z;
  array cl(999999) $10 _temporary_;
  array co(999999) _temporary_;
  length temp $10;
  do _n_=1 to countw(client,',');
   temp=scan(client,_n_,',');
   k=whichc(temp, of cl(*));
   if k then co(k)=sum(co(k),1);
   else do;
    _iorc_+1;
    cl(_iorc_)=temp;
	co(_iorc_)=1;
   end;
  end;
 end;
 do _n_=1 to dim(cl)-cmiss(of cl(*));
  client=cl(_n_);
  Count=co(_n_);
  output;
 end;
 keep client count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 02 Feb 2020 16:54:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/621785#M182844</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-02T16:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: Count occurrances of a variable's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/622578#M183147</link>
      <description>Amazing!! &lt;BR /&gt;&lt;BR /&gt;Thank you guys for the great help. The alternative methods helps me to learn. :):)</description>
      <pubDate>Wed, 05 Feb 2020 23:53:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/622578#M183147</guid>
      <dc:creator>Suzy_Cat</dc:creator>
      <dc:date>2020-02-05T23:53:12Z</dc:date>
    </item>
    <item>
      <title>Re: Count occurrances of a variable's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/622579#M183148</link>
      <description>all works!</description>
      <pubDate>Wed, 05 Feb 2020 23:53:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-occurrances-of-a-variable-s-value/m-p/622579#M183148</guid>
      <dc:creator>Suzy_Cat</dc:creator>
      <dc:date>2020-02-05T23:53:34Z</dc:date>
    </item>
  </channel>
</rss>

