<?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 values one column at a time in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-values-one-column-at-a-time/m-p/789967#M252879</link>
    <description>&lt;P&gt;Transpose, then sum in SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=long;
by obs;
var rank:;
run;

proc sql;
create table want as
  select
    col1 as position,
    sum(_name_ = 'Rank_1') as n_rank1,
    sum(_name_ = 'Rank_2') as n_rank2,
    sum(_name_ = 'Rank_3') as n_rank3
  from long
  group by position
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 13 Jan 2022 12:30:53 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-01-13T12:30:53Z</dc:date>
    <item>
      <title>Count values one column at a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-values-one-column-at-a-time/m-p/789817#M252785</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm working on a data set with a variable that is ranked according to some criteria, and i'm changing this criteria and comparing the new ranking. So I got a table with the variable being sorted, and its position according to three different rankings. Something like:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
infile datalines;
input OBS$ Rank_1$ Rank_2$ Rank_3$;
datalines;
A	 1	 1	 1
B	 1	 2	 1
C	 2	 2	 3
D	 4	 5	 2
E	 6	 2	 8
F	 2	 7	 4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So, A is first position in all three rankings, B is first in Rankings 1 and 3, but second in Ranking 2, etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want is to count how many items are at each positions per ranking. It would look something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
infile datalines;
input Position$ n_Rank_1$ n_Rank_2$ n_Rank_3$;
datalines;
1	 2	 1	 2
2	 2	 3	 1
3	 0	 0	 1
4	 1	 0	 1
5	 0	 1	 0
6	 1	 0	 0
7	 0	 1	 0
8	 0	 0	 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So Ranking 1 has 2 items at first and second positions, 1 item at fourth and sixth positions. Ranking 2 has 1 at first, 3 at second and etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I sure managed to do that separately, one ranking at a time, using SQL "select rank, count(rank)". But I would like to do that just once and couldn't find a solution on my own.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And also, using SQL, it only displays the positions that occurs in the table. Meaning that the first one shows only 1st, 2nd, 4th and 6th positions, but I want every position, with the zeros.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any thoughts?&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jan 2022 21:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-values-one-column-at-a-time/m-p/789817#M252785</guid>
      <dc:creator>Andre_AA1</dc:creator>
      <dc:date>2022-01-12T21:05:52Z</dc:date>
    </item>
    <item>
      <title>Re: Count values one column at a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-values-one-column-at-a-time/m-p/789937#M252864</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/412492"&gt;@Andre_AA1&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can create the WANT dataset with a DATA step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(keep=Position n_:);
set have end=last;
length Position 8;
array   rank_[3];
array n_rank_[3];
array p[8,3] _temporary_ (8*(3*0)); /* Dimensions could also be derived from the data. */
do _j=1 to dim2(p);
  p[input(rank_[_j],16.),_j]+1; /* Numeric rank variables would be more appropriate. */
end;
if last;
do Position=1 to dim1(p);
  do _j=1 to dim2(p);
    n_rank_[_j]=p[position,_j];
  end;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I used numeric variables &lt;FONT face="courier new,courier"&gt;Position&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;n_rank_1-n_rank_3&lt;/FONT&gt;. Not sure why you suggested character variables (also for the ranks in dataset HAVE).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Are your ranks always integers? If not, we can switch from a temporary array &lt;FONT face="courier new,courier"&gt;p[*]&lt;/FONT&gt; to a hash object.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jan 2022 10:55:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-values-one-column-at-a-time/m-p/789937#M252864</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-01-13T10:55:07Z</dc:date>
    </item>
    <item>
      <title>Re: Count values one column at a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-values-one-column-at-a-time/m-p/789952#M252873</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines;
input OBS$ Rank_1$ Rank_2$ Rank_3$;
datalines;
A  1  1  1
B  1  2  1
C  2  2  3
D  4  5  2
E  6  2  8
F  2  7  4
;
run;

proc transpose data=have out=temp;
by obs;
var rank: ;
run;


proc freq data=temp noprint;
table col1*_NAME_/out=temp2 list;
run;

proc transpose data=temp2 out=temp3(drop=_name_ _label_ rename=(col1=position));
by col1;
id _NAME_;
var count;
run;

proc stdize data=temp3 out=want missing=0 reponly;
var _numeric_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jan 2022 11:45:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-values-one-column-at-a-time/m-p/789952#M252873</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-01-13T11:45:57Z</dc:date>
    </item>
    <item>
      <title>Re: Count values one column at a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-values-one-column-at-a-time/m-p/789967#M252879</link>
      <description>&lt;P&gt;Transpose, then sum in SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=long;
by obs;
var rank:;
run;

proc sql;
create table want as
  select
    col1 as position,
    sum(_name_ = 'Rank_1') as n_rank1,
    sum(_name_ = 'Rank_2') as n_rank2,
    sum(_name_ = 'Rank_3') as n_rank3
  from long
  group by position
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jan 2022 12:30:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-values-one-column-at-a-time/m-p/789967#M252879</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-13T12:30:53Z</dc:date>
    </item>
  </channel>
</rss>

