<?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 by multiple variables in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843127#M82231</link>
    <description>&lt;P&gt;Can there be a contact date that is not in an episode (not between activation_date and deactivation_date)? For ID2, how can the deactivation_date be before the activation_date?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide data as working SAS data step code (&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;instructions&lt;/A&gt;) and not as Excel files or screen captures.&lt;/P&gt;</description>
    <pubDate>Tue, 08 Nov 2022 13:58:24 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-11-08T13:58:24Z</dc:date>
    <item>
      <title>Count by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843116#M82228</link>
      <description>&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;PatientID&lt;/TD&gt;&lt;TD&gt;Contact_date&lt;/TD&gt;&lt;TD&gt;Program_code&lt;/TD&gt;&lt;TD&gt;activation_date&lt;/TD&gt;&lt;TD&gt;Deactivation_date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID1&lt;/TD&gt;&lt;TD&gt;12/1/22&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;12/1/22&lt;/TD&gt;&lt;TD&gt;12/6/22&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID1&lt;/TD&gt;&lt;TD&gt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;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/2/22&lt;/TD&gt;&lt;TD&gt;2/3/22&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID2&lt;/TD&gt;&lt;TD&gt;1/2/22&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/2/22&lt;/TD&gt;&lt;TD&gt;2/3/22&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID3&lt;/TD&gt;&lt;TD&gt;3/3/21&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1/3/21&lt;/TD&gt;&lt;TD&gt;18/6/21&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID3&lt;/TD&gt;&lt;TD&gt;16/6/21&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1/3/21&lt;/TD&gt;&lt;TD&gt;18/6/21&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am interested in counting the average number of contacts per episode (also the number of episodes) based on the patientid and program_code by activation year. An episode is a period between the activation date and the deactivation date.&amp;amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Date format as DD/MM/YYYY&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 22:25:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843116#M82228</guid>
      <dc:creator>Sofia2022</dc:creator>
      <dc:date>2022-11-08T22:25:48Z</dc:date>
    </item>
    <item>
      <title>Re: Count by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843127#M82231</link>
      <description>&lt;P&gt;Can there be a contact date that is not in an episode (not between activation_date and deactivation_date)? For ID2, how can the deactivation_date be before the activation_date?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide data as working SAS data step code (&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;instructions&lt;/A&gt;) and not as Excel files or screen captures.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 13:58:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843127#M82231</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-08T13:58:24Z</dc:date>
    </item>
    <item>
      <title>Re: Count by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843129#M82232</link>
      <description>&lt;P&gt;Thank you for looking into this. I have corrected the episode date for patient ID2.&amp;nbsp; Apologies - I don't have SAS on my personal computer and so can't provide the&amp;nbsp;&lt;SPAN&gt;data as a working SAS data step code&lt;/SPAN&gt;&amp;nbsp;now.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 14:06:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843129#M82232</guid>
      <dc:creator>Sofia2022</dc:creator>
      <dc:date>2022-11-08T14:06:11Z</dc:date>
    </item>
    <item>
      <title>Re: Count by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843132#M82233</link>
      <description>&lt;P&gt;You didn't answer my first question: "&lt;SPAN&gt;Can there be a contact date that is not in an episode (not between activation_date and deactivation_date)?"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I will wait until you can provide the data in the requested format.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 14:15:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843132#M82233</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-08T14:15:35Z</dc:date>
    </item>
    <item>
      <title>Re: Count by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843133#M82234</link>
      <description>No it can’t. The contact date need to be between the activation date and deactivation date.</description>
      <pubDate>Tue, 08 Nov 2022 14:19:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843133#M82234</guid>
      <dc:creator>Sofia2022</dc:creator>
      <dc:date>2022-11-08T14:19:26Z</dc:date>
    </item>
    <item>
      <title>Re: Count by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843134#M82235</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/431142"&gt;@Sofia2022&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;No it can’t. The contact date need to be between the activation date and deactivation date.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then isn't ID2 in violation of that rule?&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 14:24:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843134#M82235</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-08T14:24:09Z</dc:date>
    </item>
    <item>
      <title>Re: Count by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843136#M82236</link>
      <description>Not now, as I have corrected the deactivation date. The contact dates for ID2 is now equal or between the activation and deactivation date.</description>
      <pubDate>Tue, 08 Nov 2022 14:39:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843136#M82236</guid>
      <dc:creator>Sofia2022</dc:creator>
      <dc:date>2022-11-08T14:39:29Z</dc:date>
    </item>
    <item>
      <title>Re: Count by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843141#M82237</link>
      <description>&lt;P&gt;As a side note ... we all get confused by dates of the form 02/03/2022, is that March 2 or February 3? Better is to use unambiguous formatting of dates such as 03FEB2022.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 14:46:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843141#M82237</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-08T14:46:03Z</dc:date>
    </item>
    <item>
      <title>Re: Count by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843250#M82238</link>
      <description>March 2. Date format as DD/MM/YYYY</description>
      <pubDate>Tue, 08 Nov 2022 21:59:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/843250#M82238</guid>
      <dc:creator>Sofia2022</dc:creator>
      <dc:date>2022-11-08T21:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: Count by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/853204#M82397</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;remarked, you should provide example data as a data step, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length PatientID $3 Contact_date Program_code activation_date Deactivation_date 8;
  informat Contact_date activation_date Deactivation_date ddmmyy.;
  format Contact_date activation_date Deactivation_date ddmmyy.;
  input PatientID Contact_date Program_code activation_date Deactivation_date;
cards;
ID1 12/1/22 1 12/1/22 12/6/22
ID1 12/2/22 1 12/1/22 12/6/22
ID1 12/3/22 1 12/1/22 12/6/22
ID1 12/6/22 1 12/1/22 12/6/22
ID1 12/1/22 2 12/1/22 12/6/22
ID1 12/2/22 2 12/1/22 12/6/22
ID1 12/3/22 2 12/1/22 12/6/22
ID1 12/6/22 2 12/1/22 12/6/22
ID2 12/2/22 1 1/2/22 2/3/22
ID2 1/2/22 1 1/2/22 2/3/22
ID3 3/3/21 3 1/3/21 18/6/21
ID3 16/6/21 3 1/3/21 18/6/21
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would start by getting the counts for each patient and program_code. SQL can be used here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table counts as select
    PatientID,Program_code,count(*) as count
  from have
  where Contact_date between activation_date and Deactivation_date
  group by PatientID,Program_code;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use PROC SUMMARY to get the average number of contacts:&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;proc summary data=counts;
  class PatientID;
  var count;
  output out=averages mean=avg_count;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will (as we did not use the NWAY option) create a table like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="s_lassen_1-1673423115570.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79264iCAB925A3952A3BE8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="s_lassen_1-1673423115570.png" alt="s_lassen_1-1673423115570.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The first row (with missing PatientID and _TYPE_=0) is the grand total, the average for all patients. The _FREQ_ variable shows the number of input rows used to calculate the average. The next rows are for each patient. If you need the averages for the Program_code and not the PatientID, use that variable in the CLASS statement instead, and if you just need the grand total, drop the CLASS statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, PROC SUMMARY could also have been used to create the first table as well. And PROC SQL can also calculate averages, but not on different levels at once (the _TYPE_).&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jan 2023 07:55:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-by-multiple-variables/m-p/853204#M82397</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-01-11T07:55:58Z</dc:date>
    </item>
  </channel>
</rss>

