<?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: How do I count (and sum) occurrences of a variable that satisfies certain requirements in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-and-sum-occurrences-of-a-variable-that-satisfies/m-p/411668#M279723</link>
    <description>&lt;P&gt;Hi, that actually makes it count correctly! I think that the "Type" not being written twice was the issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;V27 are integers.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just a follow up question: how could I make it list 0 if there is not an observation that fits the criteria for that state?&lt;/P&gt;</description>
    <pubDate>Wed, 08 Nov 2017 20:06:25 GMT</pubDate>
    <dc:creator>Revere2323</dc:creator>
    <dc:date>2017-11-08T20:06:25Z</dc:date>
    <item>
      <title>How do I count (and sum) occurrences of a variable that satisfies certain requirements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-and-sum-occurrences-of-a-variable-that-satisfies/m-p/411656#M279721</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to count the number of times an occurrence happened in a state, and thus get an output that lists&amp;nbsp;the number of times an event satisfies for that state.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So far I have used:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc freq;&lt;BR /&gt;where (Type='UN' or 'ST') AND (mass&amp;gt;=3) AND&lt;BR /&gt;(V27=15 or V27=14 or V27=13 or V27=12 or V27=11);&lt;BR /&gt;tables state / out=DATA.new(drop=percent);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want is a count of the number of times a state has an occurrence where the variable "type" is UN or ST, mass (count)&amp;gt;=3, and 11=&amp;lt;V27&amp;lt;=25. When I use the statement above it gives me incorrect answers so I have no idea what it is counting. It would be super nice if it placed a 0 in the table if the event is not counted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The final table I would like would look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;AL 1&lt;/P&gt;&lt;P&gt;AK 2&lt;/P&gt;&lt;P&gt;AZ 0&lt;/P&gt;&lt;P&gt;CA 2&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone give me a hand? I am on SAS 9.4&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2017 19:42:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-and-sum-occurrences-of-a-variable-that-satisfies/m-p/411656#M279721</guid>
      <dc:creator>Revere2323</dc:creator>
      <dc:date>2017-11-08T19:42:04Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count (and sum) occurrences of a variable that satisfies certain requirements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-and-sum-occurrences-of-a-variable-that-satisfies/m-p/411661#M279722</link>
      <description>&lt;P&gt;You would have to provide example data and the expected count to really know what is going "wrong".&lt;/P&gt;
&lt;P&gt;Most likely cause: (Type='UN' or 'ST')&amp;nbsp; where you meant: Type='UN' or Type='ST'&amp;nbsp; or better Type in ('UN' 'ST')&lt;/P&gt;
&lt;P&gt;Note the "Invalid numeric data, 'ST' notes in the log from running this code:&lt;/P&gt;
&lt;PRE&gt;data example;
   input type $;
   result = (Type='UN' or 'ST');
datalines;
UN
ST
BX
;
run;
&lt;/PRE&gt;
&lt;P&gt;The Result would be 1 where true and 0 where false.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One possibility is that your V27 variable has values that are not integers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW SAS will accept a condition such as&lt;/P&gt;
&lt;P&gt;(11 le V27 le 15) where the le is short for Less than or equal (assuming you meant 15 as that is the largest value you show in your where statement)&lt;/P&gt;
&lt;P&gt;or if you want to compare to a list of specific values:&lt;/P&gt;
&lt;P&gt;V27 in (11, 12, 13, 14, 15) which can also be V27 in (11 : 15)&amp;nbsp; &amp;lt;= ONLY for integers&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2017 19:56:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-and-sum-occurrences-of-a-variable-that-satisfies/m-p/411661#M279722</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-11-08T19:56:14Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count (and sum) occurrences of a variable that satisfies certain requirements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-and-sum-occurrences-of-a-variable-that-satisfies/m-p/411668#M279723</link>
      <description>&lt;P&gt;Hi, that actually makes it count correctly! I think that the "Type" not being written twice was the issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;V27 are integers.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just a follow up question: how could I make it list 0 if there is not an observation that fits the criteria for that state?&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2017 20:06:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-and-sum-occurrences-of-a-variable-that-satisfies/m-p/411668#M279723</guid>
      <dc:creator>Revere2323</dc:creator>
      <dc:date>2017-11-08T20:06:25Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count (and sum) occurrences of a variable that satisfies certain requirements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-and-sum-occurrences-of-a-variable-that-satisfies/m-p/411694#M279724</link>
      <description>&lt;P&gt;If a value of State is not in your data set the only way I know is to use a procedure that allows use of a format with all the expected values&lt;/P&gt;
&lt;P&gt;and the Preloadfmt option such as tabulate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
   input state $;
datalines;
AK
AK
AL
CA
CA
CA
;

proc format library=work;
value $expected (notsorted)
AL='AL'
AK='AK'
AZ='AZ'
CA='CA'
;


proc tabulate data=have out=want missing;
   class state /  preloadfmt order=data;
   format state $expected.;
   table state='',n
    /printmiss  box=state misstext='0';
run;&lt;/PRE&gt;
&lt;P&gt;However the output data set will have missing instead of 0 as there is actually nothing to count. You could use a data step to replace the missing with 0 or use a format to display missing as 0 depending on what you need the 0 for. Replace if it is to be used in calculations (of course don't use 0 as a denominator unless you want errors) or format to display.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2017 20:59:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-and-sum-occurrences-of-a-variable-that-satisfies/m-p/411694#M279724</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-11-08T20:59:59Z</dc:date>
    </item>
  </channel>
</rss>

