<?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 number of observations with certain values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-observations-with-certain-values/m-p/242469#M45012</link>
    <description>proc sql;&lt;BR /&gt;	select count(*) as N_obs &lt;BR /&gt;	from mydata&lt;BR /&gt;	where age &amp;gt;100;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;this works</description>
    <pubDate>Fri, 08 Jan 2016 21:23:17 GMT</pubDate>
    <dc:creator>fengyuwuzu</dc:creator>
    <dc:date>2016-01-08T21:23:17Z</dc:date>
    <item>
      <title>count number of observations with certain values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-observations-with-certain-values/m-p/242468#M45011</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=mydata;
by age;
run;

data count;
set mydata;
by age;
zero=0;
hundred=0;
if age &amp;lt;0 then zero+1;
else if age &amp;gt;100 then hundred+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with above code, I can get zero and hundred as two new variables added in the data set. I actually only want to have a simple number of how many cases with age &amp;lt;0 and how many with age&amp;gt;100. How to do that?&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2016 21:21:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-number-of-observations-with-certain-values/m-p/242468#M45011</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-01-08T21:21:04Z</dc:date>
    </item>
    <item>
      <title>Re: count number of observations with certain values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-observations-with-certain-values/m-p/242469#M45012</link>
      <description>proc sql;&lt;BR /&gt;	select count(*) as N_obs &lt;BR /&gt;	from mydata&lt;BR /&gt;	where age &amp;gt;100;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;this works</description>
      <pubDate>Fri, 08 Jan 2016 21:23:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-number-of-observations-with-certain-values/m-p/242469#M45012</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-01-08T21:23:17Z</dc:date>
    </item>
    <item>
      <title>Re: count number of observations with certain values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-observations-with-certain-values/m-p/242549#M45032</link>
      <description>&lt;P&gt;Great that you found a good solution using PROC SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note that your data step approach could be simplified considerably:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You don't actually perform BY group processing. Hence, the BY statement in the data step and, most importantly, the (possibly resource-intensive) PROC SORT step could be omitted.&lt;/LI&gt;
&lt;LI&gt;The initialization of ZERO and HUNDRED to 0 is redundant. It is implicitly done if you use the "&lt;EM&gt;variable&lt;/EM&gt;+&lt;EM&gt;increment&lt;/EM&gt;" syntax (sum statement).&lt;/LI&gt;
&lt;LI&gt;If you like your code very short, you could make use of the fact that logical expressions evaluate to 0 (if they are false) or 1 (if they are true).&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;With these suggestions your initial code could have looked like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data count;
set mydata;
zero+(age&amp;lt;0);
hundred+(age&amp;gt;100);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You should be aware that not only negative values, but also missing values of AGE would satisfy the condition AGE&amp;lt;0. If you want to restrict your count to negative values, the third line should read &lt;FONT face="courier new,courier"&gt;zero+(.&amp;lt;age&amp;lt;0)&lt;/FONT&gt; or, to exclude special missing values as well,&amp;nbsp;&lt;SPAN&gt;&lt;FONT face="courier new,courier"&gt;zero+(.z&amp;lt;age&amp;lt;0)&lt;/FONT&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, you don't need to create a new (possibly large) dataset COUNT to count those observations. You could avoid this with a data step as the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set mydata end=last;
zero+(age&amp;lt;0);
hundred+(age&amp;gt;100);
if last then put zero= hundred=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By default, the PUT statement would write the desired numbers to the log (not to the output window like PROC SQL).&lt;/P&gt;</description>
      <pubDate>Sat, 09 Jan 2016 15:07:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-number-of-observations-with-certain-values/m-p/242549#M45032</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-09T15:07:17Z</dc:date>
    </item>
  </channel>
</rss>

