<?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 the number of rows whose value is greater or equal than the value of current row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922265#M363178</link>
    <description>&lt;P&gt;The power of correlated sub-queries! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 29 Mar 2024 13:32:56 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2024-03-29T13:32:56Z</dc:date>
    <item>
      <title>Count the number of rows whose value is greater or equal than the value of current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922084#M363116</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the below dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input amount;
datalines;
10
11
11
33
456
54
66
66
89
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to add a column showing how many rows in the dataset have greater or equal value than the current row:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input amount No_times_gr_eq;
datalines;
10 9
11 8
11 8
33 6
456 1
54 5
66 4
66 4
89 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any help would be much appreciated.&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 10:34:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922084#M363116</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2024-03-28T10:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of rows whose value is greater or equal than the value of current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922085#M363117</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11100"&gt;@Zatere&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/p0le3p5ngj1zlbn1mh3tistq9t76.htm" target="_blank" rel="noopener"&gt;PROC RANK&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc rank data=have out=want descending ties=high;
var amount;
ranks No_times_gr_eq;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Mar 2024 10:56:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922085#M363117</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-03-28T10:56:25Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of rows whose value is greater or equal than the value of current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922095#M363122</link>
      <description>&lt;P&gt;Two approaches with Hash Tables;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) order like in the original dataset (two data reads):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  declare hash H(ordered:"A");
  H.defineKey("amount");
  H.defineData("amount","K");
  H.defineDone();
  declare hiter I("H");
  do until(eof);
    set have end=eof;
    if H.find() then K=1;
                else K+1;
    H.replace();
  end;

  do while(0=I.prev());
    N+k;
    k=N;
    H.replace();
  end;

  do until(eof2);
    set have end=eof2;
    H.find();
    output;
  end;
  drop n;
run;

proc print data=test1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2) data in descending order (single data read):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
  declare hash H(ordered:"A");
  H.defineKey("amount");
  H.defineData("amount","K");
  H.defineDone();
  declare hiter I("H");
  do until(eof);
    set have end=eof nobs=nobs;
    if H.find() then K=1;
                else K+1;
    H.replace();
  end;

  do while(0=I.prev());
    N+k;
    do k=1 to k;
      output;
    end;
  end;
run;

proc print data=test2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 12:41:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922095#M363122</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-03-28T12:41:30Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of rows whose value is greater or equal than the value of current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922096#M363123</link>
      <description>Hi and thanks for this reply. If I have many variables for which I want to make this calculation, is there any way to do so using hash tables? For example, if I had another column "volume".</description>
      <pubDate>Thu, 28 Mar 2024 12:50:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922096#M363123</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2024-03-28T12:50:24Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of rows whose value is greater or equal than the value of current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922110#M363128</link>
      <description>&lt;P&gt;Hash table of hash tables will do the job here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input amount;
  volume = amount*123;
  reversed = 1000-amount;
datalines;
10
11
11
33
456
54
66
66
89
;
run;
proc print;
run;

%macro scoring(
 have=have
,variables=amount volume reversed
,result=result
);

data &amp;amp;result.;
  array v &amp;amp;variables.;

  declare hash H;
  declare hiter I;

  length vn $ 32;
  declare hash HoH();
  HoH.defineKey("vn");
  HoH.defineData("H","I");
  HoH.defineDone();
  declare hiter IHoH("HoH");

  do until(eof);
    set &amp;amp;have. end=eof;
    do over v;
      vn=vname(v);
      if HoH.CHECK() then 
          do;
              H = _NEW_ hash(ordered: "A");
                  H.DefineKey(vn);
                  H.DefineData(vn,"K");
                  H.DefineDone();
              I = _NEW_ hiter ("H");
              HoH.replace();
          end;
      HoH.find();
      if H.find() then K=1;
                  else K+1;
      H.replace();
    end;
  end;

  do over v;
    vn=vname(v);
    HoH.find();
    N=0;
    do while(0=I.prev());
      N+k;
      k=N;
      H.replace();
    end;
    H.output(dataset:cats("test3_B_",vn));
  end;

  do until(eof2);
    set &amp;amp;have. end=eof2;
    array scores[%sysfunc(countw(&amp;amp;variables.,%str( )))];
    do over v;
      vn=vname(v);
      HoH.find();
      H.find();
      scores[_I_]=K;
    end;
    output;
  end;

  keep &amp;amp;variables. scores:; 
run;
%mend scoring;

%scoring(
have=have
,variables=amount volume reversed notExistingVariable
,result=test3
)


proc print data=test3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 13:39:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922110#M363128</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-03-28T13:39:48Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of rows whose value is greater or equal than the value of current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922229#M363161</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input amount;
datalines;
10
11
11
33
456
54
66
66
89
;
run;

proc sql;
create table want as
select amount,(select count(*) from have where amount&amp;gt;=a.amount) as want
 from have as a;
 quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Mar 2024 03:01:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922229#M363161</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-03-29T03:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of rows whose value is greater or equal than the value of current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922265#M363178</link>
      <description>&lt;P&gt;The power of correlated sub-queries! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 13:32:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-rows-whose-value-is-greater-or-equal-than/m-p/922265#M363178</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-03-29T13:32:56Z</dc:date>
    </item>
  </channel>
</rss>

