<?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 to count number of observations by ID with identical dates but different locations? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898975#M40026</link>
    <description>&lt;P&gt;The real key to this solution is in the MERGE statement, which does a self-merge of TEST, simultaneously taking data from both observation i&amp;nbsp; and observation i+1 (where TEST has the FIRSTOBS=2 parameter).&amp;nbsp; This allows you to look-ahead and compare data in the current obs to the next obs.&amp;nbsp; &amp;nbsp;Of course, that means you have to rename variables because both obs start out with the same variable names in a self-merge.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SET statement is there merely to use with the BY statement, thereby allowing use of FIRST.ID and LAST.ID in the IF statement.&amp;nbsp; &amp;nbsp;You could actually drop the SET and BY statements, through use of a slightly "busier" if statement, as in&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2 (drop=nxt_:);
  merge test
        test (firstobs=2 keep=id admitdate rename=(id=nxt_id admitdate=nxt_admdate));
  if (id=nxt_id and nxt_admdate=dischargedate) or (id=lag(id) and admitdate=lag(dischargedate)) then dummy=1;
  else dummy=0;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course, this latter code version doesn't protect you against data that might not be sorted BY ID/ADMITDATE/DISCHARGEDATE.&lt;/P&gt;</description>
    <pubDate>Tue, 17 Oct 2023 16:38:07 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-10-17T16:38:07Z</dc:date>
    <item>
      <title>How to count number of observations by ID with identical dates but different locations?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898836#M40015</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm a novice SAS user (SAS 9.4) and I'm working with a healthcare dataset where I need to count the number of observations that were discharged and admitted on the same day at different places of service. Example, if ID 1 was seen at the ED on 01/01/2020 and was discharged on 01/02/2020 and Inpatient admission on 01/02/2020 then the new variable would count that as 1 service but if ID 1 was seen at the ED on 01/10/2020 and discharged on 01/10/2020 and Inpatient was admitted on 01/31/2020 then the new variable wouldn't count that. How would I create a variable that would count that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data SC.test;
input id $ admitdate:mmddyy10. dischargedate:mmddyy10. place $2. ;
format ADMITdate MMDDYY10. DISCHARGEdate MMDDYY10.;
datalines;
1 09/10/2020 09/10/2020 ED
1 09/10/2020 09/11/2020 IN
2 10/10/2020 10/12/2020 IN
3 04/03/2020 04/03/2020 ED
3 05/03/2020 05/03/2020 ED
3 05/29/2020 06/03/2020 IN
4 10/09/2020 10/10/2020 ED
4 10/10/2020 10/12/2020 IN
4 11/03/2020 11/03/2020 ED
4 12/29/2020 12/31/2020 IN
;
run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My expected output would be something like this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;ADMITDATE&lt;/TD&gt;&lt;TD&gt;DISCHARGEDATE&lt;/TD&gt;&lt;TD&gt;PLACE&lt;/TD&gt;&lt;TD&gt;NEWVAR&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;9/10/2020&lt;/TD&gt;&lt;TD&gt;9/10/2020&lt;/TD&gt;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gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;11/3/2020&lt;/TD&gt;&lt;TD&gt;11/3/2020&lt;/TD&gt;&lt;TD&gt;ED&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;12/29/2020&lt;/TD&gt;&lt;TD&gt;12/31/2020&lt;/TD&gt;&lt;TD&gt;IN&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would really appreciate any insight. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Amanda&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2023 18:11:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898836#M40015</guid>
      <dc:creator>aokolo</dc:creator>
      <dc:date>2023-10-16T18:11:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of observations by ID with identical dates but different locations?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898847#M40016</link>
      <description>&lt;P&gt;When you say "count" you have to be pretty explicit about why None of these get a count:&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;10/10/2020&lt;/TD&gt;
&lt;TD&gt;10/12/2020&lt;/TD&gt;
&lt;TD&gt;IN&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;4/3/2020&lt;/TD&gt;
&lt;TD&gt;4/3/2020&lt;/TD&gt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;I am not seeing any Rule that says id 2 and 3 or part of id=4 should not have anything counted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My observation of just the Want example would be that the DATES really are not playing an actual role in the result at all.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2023 20:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898847#M40016</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-10-16T20:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of observations by ID with identical dates but different locations?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898857#M40017</link>
      <description>&lt;P&gt;You want to set a dummy variable to one for all records for which a discharge from one place occured on the same date as a admission to another place.&amp;nbsp; You want to set the dummy to 1 for both records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id $ admitdate:mmddyy10. dischargedate:mmddyy10. place $2. ;
format ADMITdate MMDDYY10. DISCHARGEdate MMDDYY10.;
datalines;
1 09/10/2020 09/10/2020 ED
1 09/10/2020 09/11/2020 IN
2 10/10/2020 10/12/2020 IN
3 04/03/2020 04/03/2020 ED
3 05/03/2020 05/03/2020 ED
3 05/29/2020 06/03/2020 IN
4 10/09/2020 10/10/2020 ED
4 10/10/2020 10/12/2020 IN
4 11/03/2020 11/03/2020 ED
4 12/29/2020 12/31/2020 IN
run;

data want (drop=nxt_:);
  set test (keep=id admitdate dischargedate);
  by id admitdate dischargedate;
  merge test
        test (firstobs=2 keep=admitdate rename=(admitdate=nxt_admdate));
  if (last.id=0 and nxt_admdate=dischargedate) or (first.id=0 and admitdate=lag(dischargedate)) then dummy=1;
  else dummy=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code assumes the data are sorted by &lt;STRIKE&gt;ID/ADMITDATE&lt;/STRIKE&gt; ID/ADMITDATE/DISCHARGEDATE&amp;nbsp; &amp;nbsp;The code logic really only needs a BY statement of&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;BY ID;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I also included &lt;STRIKE&gt;ADMITDATE&lt;/STRIKE&gt;&amp;nbsp; ADMITDATE and DISCHARGEDATE, so if the data set is not properly sorted, the program will stop with an error message.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;ERROR: BY variables are not properly sorted on data set WORK.TEST.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and you'll know you need to sort the data.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Oct 2023 16:28:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898857#M40017</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-10-17T16:28:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of observations by ID with identical dates but different locations?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898942#M40024</link>
      <description>Thanks for responding! The reason id=2 and id=3 and part of id=4 wouldn't be counted is because their discharge and admit dates aren't on the same day. I am looking to count only encounters that have the same discharge and admit dates, as I'm looking for ED visits that resulted in an inpatient admission and looking at the dates is the only way I can observe this in my dataset. My apologies for not making that clear.</description>
      <pubDate>Tue, 17 Oct 2023 14:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898942#M40024</guid>
      <dc:creator>aokolo</dc:creator>
      <dc:date>2023-10-17T14:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of observations by ID with identical dates but different locations?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898946#M40025</link>
      <description>Thank you! This was successful! Now I'm studying this to learn more about why and how this worked.</description>
      <pubDate>Tue, 17 Oct 2023 14:50:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898946#M40025</guid>
      <dc:creator>aokolo</dc:creator>
      <dc:date>2023-10-17T14:50:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of observations by ID with identical dates but different locations?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898975#M40026</link>
      <description>&lt;P&gt;The real key to this solution is in the MERGE statement, which does a self-merge of TEST, simultaneously taking data from both observation i&amp;nbsp; and observation i+1 (where TEST has the FIRSTOBS=2 parameter).&amp;nbsp; This allows you to look-ahead and compare data in the current obs to the next obs.&amp;nbsp; &amp;nbsp;Of course, that means you have to rename variables because both obs start out with the same variable names in a self-merge.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SET statement is there merely to use with the BY statement, thereby allowing use of FIRST.ID and LAST.ID in the IF statement.&amp;nbsp; &amp;nbsp;You could actually drop the SET and BY statements, through use of a slightly "busier" if statement, as in&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2 (drop=nxt_:);
  merge test
        test (firstobs=2 keep=id admitdate rename=(id=nxt_id admitdate=nxt_admdate));
  if (id=nxt_id and nxt_admdate=dischargedate) or (id=lag(id) and admitdate=lag(dischargedate)) then dummy=1;
  else dummy=0;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course, this latter code version doesn't protect you against data that might not be sorted BY ID/ADMITDATE/DISCHARGEDATE.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Oct 2023 16:38:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898975#M40026</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-10-17T16:38:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of observations by ID with identical dates but different locations?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898983#M40028</link>
      <description>Thank you so much for providing this explanation! It helps so much to understand how the code works.</description>
      <pubDate>Tue, 17 Oct 2023 17:15:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-number-of-observations-by-ID-with-identical-dates/m-p/898983#M40028</guid>
      <dc:creator>aokolo</dc:creator>
      <dc:date>2023-10-17T17:15:32Z</dc:date>
    </item>
  </channel>
</rss>

