<?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: multiple by variables - how does order of by variables matter? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661836#M197787</link>
    <description>&lt;P&gt;Thank you all for your quick and informative responses to my question! And thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; for pointing out that the by group variables form a hierarchy.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran the following to visualize of how the by variables behave differently under different hierarchy.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data visualize1;
set sample_srt;
by member hospital admission;
if first.admission then frst_adm = 1;
else frst_adm = 0;
if last.admission then last_adm = 1;
else last_adm = 0;

if first.hospital then frst_pos = 1;
else frst_pos = 0;
if last.hospital then last_pos = 1;
else last_pos = 0;
run;


data visualize2;
set sample_srt;
by member admission hospital;
if first.admission then frst_adm = 1;
else frst_adm = 0;
if last.admission then last_adm = 1;
else last_adm = 0;

if first.hospital then frst_pos = 1;
else frst_pos = 0;
if last.hospital then last_pos = 1;
else last_pos = 0;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 17 Jun 2020 16:29:47 GMT</pubDate>
    <dc:creator>aaronh</dc:creator>
    <dc:date>2020-06-17T16:29:47Z</dc:date>
    <item>
      <title>multiple by variables - how does order of by variables matter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661803#M197769</link>
      <description>&lt;P&gt;So I am trying to summarize data into distinct hospital stay.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
input member $ admission $ discharge $ hospital $;
datalines;
Jane 20150101 20150305 A
Jane 20150101 20150103 A
John 20160101 20160105 B
John 20160101 20160101 A
Jane 20190101 20190507 C
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For my research purpose, my definition of a hospital stay is based on member, admission date, and hospital. So I am not considering internal transfer as a separate hospital stay. For example, in the sample, Jane was admitted to hospital A on 20150101, but internally transferred on 20150103 to another department/care team, and discharged from A on 20150305. I consider this a single hospital stay.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the following and achieved what I wanted:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sample out=sample_srt;
by member admission discharge hospital;
run;

data want;
set sample_srt;
by member hospital admission;
if last.admission;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I don't understand is that if I try the following instead, I get something different.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
set sample_srt;
by member admission hospital;
if last.admission;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I thought that the order of by group variables doesn't matter, but clearly I was wrong. I'd really appreciate if someone can explain why the order of by variables matters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-AH&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 15:20:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661803#M197769</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2020-06-17T15:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: multiple by variables - how does order of by variables matter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661810#M197771</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274317"&gt;@aaronh&lt;/a&gt;&amp;nbsp; The stay in&lt;U&gt; &lt;STRONG&gt;a&lt;/STRONG&gt; &lt;/U&gt;hospital would require a &lt;EM&gt;sort &lt;FONT color="#339966"&gt;by member hospital admission discharge;&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Whether a patient &lt;STRONG&gt;ever&lt;/STRONG&gt; admitted to a hospital regardless of how many times or the tenure at a particular hospital would require a sort by&amp;nbsp;&lt;EM&gt;&lt;FONT color="#339966"&gt;member admission discharge;&lt;/FONT&gt;&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 15:29:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661810#M197771</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-06-17T15:29:59Z</dc:date>
    </item>
    <item>
      <title>Re: multiple by variables - how does order of by variables matter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661813#M197773</link>
      <description>&lt;P&gt;The Order of BY groups DOES matter.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's hierarchical, so your groups that are member/admission/hospital will be different than member/admission/hospital.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a quick example of an explanation. Hospitals each have their own ID systems so that IDs that are 4 in Hospital 1 are not the same as the ID=4 in Hospital 2. To account for this you would need to group your variables by Hospital ID Admission.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In theory your example below should result in the same answers as you shouldn't have the same admission date to different hospitals but it clearly happens and I wonder if it's because my example above (ID different at different hospitals) isn't correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an illustrated example from the documentation that may help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p0xu93fy5eemkyn1p6mj5elses7j.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p0xu93fy5eemkyn1p6mj5elses7j.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274317"&gt;@aaronh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;So I am trying to summarize data into distinct hospital stay.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
input member $ admission $ discharge $ hospital $;
datalines;
Jane 20150101 20150305 A
Jane 20150101 20150103 A
John 20160101 20160105 B
John 20160101 20160101 A
Jane 20190101 20190507 C
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For my research purpose, my definition of a hospital stay is based on member, admission date, and hospital. So I am not considering internal transfer as a separate hospital stay. For example, in the sample, Jane was admitted to hospital A on 20150101, but internally transferred on 20150103 to another department/care team, and discharged from A on 20150305. I consider this a single hospital stay.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried the following and achieved what I wanted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sample out=sample_srt;
by member admission discharge hospital;
run;

data want;
set sample_srt;
by member hospital admission;
if last.admission;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What I don't understand is that if I try the following instead, I get something different.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
set sample_srt;
by member admission hospital;
if last.admission;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I thought that the order of by group variables doesn't matter, but clearly I was wrong. I'd really appreciate if someone can explain why the order of by variables matters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-AH&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 15:30:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661813#M197773</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-06-17T15:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: multiple by variables - how does order of by variables matter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661814#M197774</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
input member $ admission $ discharge $ hospital $;
admission_dt=input(admission, yymmdd8.);
discharge_dt=input(discharge, yymmdd8.);
format admission_dt discharge_dt yymmdd10.;
datalines;
Jane 20150101 20150305 A
Jane 20150101 20150103 A
John 20160101 20160105 B
John 20160101 20160101 A
Jane 20190101 20190507 C
;
proc summary data=sample nway missing noprint;
class Member hospital;
var admission_dt discharge_dt;
output min(admission_dt) = max(discharge_dt)= out= want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jun 2020 15:30:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661814#M197774</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-17T15:30:44Z</dc:date>
    </item>
    <item>
      <title>Re: multiple by variables - how does order of by variables matter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661815#M197775</link>
      <description>&lt;P&gt;It is due to the way the data is sorted and location of variables , like in the below example&amp;nbsp; sorting by and variables order of&lt;/P&gt;
&lt;P&gt;member hospital admission will give 4 records due to hospital values distinguish the date 20160106 whereas in second example sorting by and variable order member admission hospital distinguish by admission date and we get 3 records where hospital is ignored as we are considering the admission date. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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="image.png" style="width: 794px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/45297i345D7285B81CE623/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 15:31:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661815#M197775</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-06-17T15:31:07Z</dc:date>
    </item>
    <item>
      <title>Re: multiple by variables - how does order of by variables matter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661816#M197776</link>
      <description>&lt;P&gt;The order of variables in the BY statement creates a hierarchy. So BY A B C will create a different order than BY C B A.&lt;/P&gt;
&lt;P&gt;In particular, keeping existing orders within a by group is NOT guaranteed if that order is not enforced by adding additional columns to the BY as needed.&lt;/P&gt;
&lt;P&gt;In your case, I would sort&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by member hospital admission discharge;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then, in the following data step, use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by member hospital admission;
if last.admission;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;so that you get the last discharge date for stays that share the same admission date.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 15:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661816#M197776</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-17T15:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: multiple by variables - how does order of by variables matter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661818#M197778</link>
      <description>&lt;P&gt;The problem could be the same person is admitted to two different hospitals on the same day.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 15:35:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661818#M197778</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-17T15:35:01Z</dc:date>
    </item>
    <item>
      <title>Re: multiple by variables - how does order of by variables matter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661824#M197783</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The problem could be the same person is admitted to two different hospitals on the same day.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;To handle this situation, you would need datetimes instead of dates, so you could determine the order of admissions within a day.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 15:47:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661824#M197783</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-17T15:47:28Z</dc:date>
    </item>
    <item>
      <title>Re: multiple by variables - how does order of by variables matter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661835#M197786</link>
      <description>&lt;P&gt;thanks Smantha for pointing that out. Yes, it happens because a patient could be admitted to hospital A, but it is determined that hospital A does not have the care level needed by the patient, so the patient is transferred immediately to a hospital with higher level of care.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 16:22:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661835#M197786</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2020-06-17T16:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: multiple by variables - how does order of by variables matter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661836#M197787</link>
      <description>&lt;P&gt;Thank you all for your quick and informative responses to my question! And thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; for pointing out that the by group variables form a hierarchy.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran the following to visualize of how the by variables behave differently under different hierarchy.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data visualize1;
set sample_srt;
by member hospital admission;
if first.admission then frst_adm = 1;
else frst_adm = 0;
if last.admission then last_adm = 1;
else last_adm = 0;

if first.hospital then frst_pos = 1;
else frst_pos = 0;
if last.hospital then last_pos = 1;
else last_pos = 0;
run;


data visualize2;
set sample_srt;
by member admission hospital;
if first.admission then frst_adm = 1;
else frst_adm = 0;
if last.admission then last_adm = 1;
else last_adm = 0;

if first.hospital then frst_pos = 1;
else frst_pos = 0;
if last.hospital then last_pos = 1;
else last_pos = 0;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 16:29:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiple-by-variables-how-does-order-of-by-variables-matter/m-p/661836#M197787</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2020-06-17T16:29:47Z</dc:date>
    </item>
  </channel>
</rss>

