<?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: Help with conditional count in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50799#M10624</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; Thanks a lot Ksharp and Mike. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 21 Nov 2011 12:01:11 GMT</pubDate>
    <dc:creator>vioravis</dc:creator>
    <dc:date>2011-11-21T12:01:11Z</dc:date>
    <item>
      <title>Help with conditional count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50791#M10616</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have two data tables Table A and Table B as follows: &lt;/P&gt;&lt;P&gt;data tableA; &lt;/P&gt;&lt;P&gt;input Days;&lt;/P&gt;&lt;P&gt;cards; &lt;/P&gt;&lt;P&gt;1 &lt;/P&gt;&lt;P&gt;2 &lt;/P&gt;&lt;P&gt;3 &lt;/P&gt;&lt;P&gt;4 ; &lt;/P&gt;&lt;P&gt;run; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data tableB; &lt;/P&gt;&lt;P&gt;input DayCount; &lt;/P&gt;&lt;P&gt;cards; &lt;/P&gt;&lt;P&gt;2 &lt;/P&gt;&lt;P&gt;1 &lt;/P&gt;&lt;P&gt;2 &lt;/P&gt;&lt;P&gt;3 &lt;/P&gt;&lt;P&gt;4 ; &lt;/P&gt;&lt;P&gt;run; &lt;/P&gt;&lt;P&gt;I want to create a resulting data table (Table C) such a way that data table contains the number of times the value in Table B exceeds each row in Table A. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data tableC; &lt;/P&gt;&lt;P&gt;input Days Count; &lt;/P&gt;&lt;P&gt;cards; &lt;/P&gt;&lt;P&gt;1&amp;nbsp; 5 &lt;/P&gt;&lt;P&gt;2&amp;nbsp; 4 &lt;/P&gt;&lt;P&gt;3&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;4&amp;nbsp; 1 ;&lt;/P&gt;&lt;P&gt;run; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The first column of Table C is from Table A. The second column contains how many in Table B exceeds the column Day. For example, 5 days have value greater than 1 (in Table B). 4 days have value greater than 2 in Table B and so on. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can someone help with an appropriate DATA STEP or PROC SQL to obtain Table C? Thank you.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Nov 2011 10:24:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50791#M10616</guid>
      <dc:creator>vioravis</dc:creator>
      <dc:date>2011-11-19T10:24:34Z</dc:date>
    </item>
    <item>
      <title>Help with conditional count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50792#M10617</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;How about.&lt;/P&gt;&lt;P&gt;But if you care about speed, then Hash Table is a better choice, That will need some more code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data tableA;
input Days;
cards;
1
2
3
4 
;
run;

 

data tableB;
input DayCount;
cards;
2
1
2
3
4 
;
run;

data tableC(drop=daycount);
 set tablea;
 count=0;
 do i=1 to _nobs;
&amp;nbsp; set tableb nobs=_nobs point=i;
&amp;nbsp; if daycount ge days then count+1;
 end;
 run;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Nov 2011 12:13:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50792#M10617</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-11-19T12:13:34Z</dc:date>
    </item>
    <item>
      <title>Help with conditional count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50793#M10618</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;And version of SQL. IF you like SQL more.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data tableA;
input Days;
cards;
1
2
3
4 
;
run;

 

data tableB;
input DayCount;
cards;
2
1
2
3
4 
;
run;
proc sql;
create table s as
select days,(select count(*) from tableb as b where b.daycount ge a.days) as count
 from tablea as a;quit;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Nov 2011 12:20:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50793#M10618</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-11-19T12:20:44Z</dc:date>
    </item>
    <item>
      <title>Help with conditional count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50794#M10619</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;KSharp has already provided a number of solutions for accomplishing the results shown in your example, but I did want to point out that your description doesn't match your example's desired results.&amp;nbsp; I.e., you say that you want the number of records from table b whose value exceeds (i.e., gt) those of table a, but then show that you want the number of values that are greater than or equal to the value (i.e., ge).&amp;nbsp; Using the wrong comparison could easily end up causing wrong decisions to be made when the results are applied in the real world.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Nov 2011 16:23:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50794#M10619</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2011-11-19T16:23:42Z</dc:date>
    </item>
    <item>
      <title>Help with conditional count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50795#M10620</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; Thanks a lot KSharp. It worked well. I went with the DATA step for the time being. I will look into the hash table version of it since speed is a very important factor. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Nov 2011 20:20:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50795#M10620</guid>
      <dc:creator>vioravis</dc:creator>
      <dc:date>2011-11-19T20:20:35Z</dc:date>
    </item>
    <item>
      <title>Help with conditional count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50796#M10621</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;art297, you are right. It is a typo on my part. Thanks for pointing out.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Nov 2011 20:21:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50796#M10621</guid>
      <dc:creator>vioravis</dc:creator>
      <dc:date>2011-11-19T20:21:37Z</dc:date>
    </item>
    <item>
      <title>Help with conditional count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50797#M10622</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It is Hash Table version which will be more fast.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data tableA;
input Days;
cards;
1
2
3
4 
;
run;

 

data tableB;
input DayCount;
cards;
2
1
2
3
4 
;
run;


data TableC(drop=daycount);
 if _n_ eq 1 then do;
&amp;nbsp; if 0 then set tableb;
&amp;nbsp; declare hash ha(hashexp:20,dataset:'tableb',multidata:'Y');
&amp;nbsp; declare hiter hi('ha');
&amp;nbsp;&amp;nbsp; ha.definekey('daycount');
&amp;nbsp;&amp;nbsp; ha.definedata('daycount');
&amp;nbsp;&amp;nbsp; ha.definedone();
 end;
set tablea;
count=0;
 do while(hi.next() eq 0);
&amp;nbsp; if daycount ge days then count+1;
 end;
run;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 21 Nov 2011 01:56:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50797#M10622</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-11-21T01:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: Help with conditional count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50798#M10623</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi ... here's another idea&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;it requires a sort and relies on values in tablea matching values in tableb, but the final data step is really simple ... &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;proc sort data=tableb;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;by daycount;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;data fmt / view=fmt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;retain fmtname "howmany";&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;if last then call symputx('obs',_n_ - 1);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;set tableb (rename=(daycount=start)) end=last;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;by start;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;label = cat(_n_);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;if first.start;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;proc format cntlin=fmt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;data tablec;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;set tablea;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;howmany = &amp;amp;obs - input(put(days,howmany.),10.) + 1;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 21 Nov 2011 03:21:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50798#M10623</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2011-11-21T03:21:31Z</dc:date>
    </item>
    <item>
      <title>Re: Help with conditional count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50799#M10624</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; Thanks a lot Ksharp and Mike. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 21 Nov 2011 12:01:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50799#M10624</guid>
      <dc:creator>vioravis</dc:creator>
      <dc:date>2011-11-21T12:01:11Z</dc:date>
    </item>
    <item>
      <title>Re: Help with conditional count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50800#M10625</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think Mike has a better idea. i.e.&lt;/P&gt;&lt;P&gt;Firstly use proc sort to sort dataset descending. then make a count+1 variable ,keep the unique value of tableB,&lt;/P&gt;&lt;P&gt;and Use HashTable to query the count you want.&lt;/P&gt;&lt;P&gt;It will be faster than my code, when TableB is a large table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data tableA;
input Days;
cards;
1
2
3
4
9 
;
run;

 

data tableB;
input DayCount;
cards;
2
1
2
3
4 
;
run;

proc sort data=tableb;by descending daycount;run;
data tableb;
 set tableb;
 by daycount notsorted;
 count+1;
 if last.daycount;
run;

data TableC(drop=daycount rc);
 if _n_ eq 1 then do;
&amp;nbsp; if 0 then set tableb;
&amp;nbsp; declare hash ha(hashexp:20,dataset:'tableb');
&amp;nbsp;&amp;nbsp; ha.definekey('daycount');
&amp;nbsp;&amp;nbsp; ha.definedata('count');
&amp;nbsp;&amp;nbsp; ha.definedone();
 end;
set tablea;
daycount=days; call missing(count);
rc=ha.find();
run;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 Nov 2011 06:38:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-conditional-count/m-p/50800#M10625</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-11-22T06:38:41Z</dc:date>
    </item>
  </channel>
</rss>

