<?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: Averaging count by two variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Averaging-count-by-two-variables/m-p/538143#M148118</link>
    <description>&lt;P&gt;You need to test for first.year&amp;nbsp;&lt;EM&gt;only&lt;/EM&gt;.&lt;/P&gt;</description>
    <pubDate>Sun, 24 Feb 2019 22:11:57 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-02-24T22:11:57Z</dc:date>
    <item>
      <title>Averaging count by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Averaging-count-by-two-variables/m-p/538142#M148117</link>
      <description>&lt;P&gt;I have a longitudinal dataset and I need a rate of tests performed per year per individual:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;test_date&amp;nbsp; &amp;nbsp; &amp;nbsp;test_value&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01/01/2014&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3.5&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03/24/2014&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12.4&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02/15/2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9.3&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03/01/2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.04&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;09/24/2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02/03/2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03/06/2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02/01/2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2.5&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10/12/2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I created a extracted the year from the date into a new year variable. I figured that if I can count the number of tests per year per ID, I can then average the count per ID to get the rate per ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code I am using but the output gives me a total count per ID instead of per year per ID:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;&amp;nbsp; retain new_var;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;set have;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; by ID year;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if first.ID then new_var= 1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else new_var= new_var+1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also tried adding "and first.year" in the if statement but I still get the same output.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there another way to get the rate with this dataset?&lt;/P&gt;</description>
      <pubDate>Sun, 24 Feb 2019 22:09:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Averaging-count-by-two-variables/m-p/538142#M148117</guid>
      <dc:creator>dikshar</dc:creator>
      <dc:date>2019-02-24T22:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: Averaging count by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Averaging-count-by-two-variables/m-p/538143#M148118</link>
      <description>&lt;P&gt;You need to test for first.year&amp;nbsp;&lt;EM&gt;only&lt;/EM&gt;.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Feb 2019 22:11:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Averaging-count-by-two-variables/m-p/538143#M148118</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-02-24T22:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: Averaging count by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Averaging-count-by-two-variables/m-p/538147#M148122</link>
      <description>&lt;P&gt;You did get most of the way there.&amp;nbsp; As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;pointed out, you are using first.ID when you should be using first.year instead ... except that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could do all this using your original data, without having to create a new variable to hold the year.&amp;nbsp; Consider:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=have;&lt;/P&gt;
&lt;P&gt;tables id * test_date / noprint out=n_tests;&lt;/P&gt;
&lt;P&gt;format test_date year4.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This gives you a data set N_TESTS with a COUNT for each patient, for each year.&amp;nbsp; Note that the TEST_DATE variable still refers to a particular day, the earliest day that was found for that patient, during that year.&amp;nbsp; But there is still just one COUNT for each patient/year.&amp;nbsp; So finish with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc means data=n_tests;&lt;/P&gt;
&lt;P&gt;var count;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Feb 2019 22:43:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Averaging-count-by-two-variables/m-p/538147#M148122</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-02-24T22:43:47Z</dc:date>
    </item>
    <item>
      <title>Re: Averaging count by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Averaging-count-by-two-variables/m-p/538149#M148124</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How about using sql?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select id,year(test_date) as year, avg(test_value) as avg_test_value format=8.2&lt;BR /&gt;from have&lt;BR /&gt;group by id,calculated year;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Feb 2019 22:50:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Averaging-count-by-two-variables/m-p/538149#M148124</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2019-02-24T22:50:24Z</dc:date>
    </item>
  </channel>
</rss>

