<?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: Checking whether there is a new obs within a group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675301#M203438</link>
    <description>&lt;P&gt;An important question is how do you know which are the "old" observations?&lt;/P&gt;
&lt;P&gt;Do you have a data set with the old groups in it somewhere?&lt;/P&gt;</description>
    <pubDate>Fri, 07 Aug 2020 21:11:01 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-08-07T21:11:01Z</dc:date>
    <item>
      <title>Checking whether there is a new obs within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675290#M203434</link>
      <description>&lt;P&gt;Is there a way to check whether there is a new person in my grouped data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;HospitalID PersonID Year&lt;/P&gt;
&lt;P&gt;1 1 2005&lt;/P&gt;
&lt;P&gt;1 2 2005&lt;/P&gt;
&lt;P&gt;1 3 2005&lt;/P&gt;
&lt;P&gt;1 1 2006&lt;/P&gt;
&lt;P&gt;1 2 2006&lt;/P&gt;
&lt;P&gt;1 3 2006&lt;/P&gt;
&lt;P&gt;1 4 2006&lt;/P&gt;
&lt;P&gt;2 5 2005&lt;/P&gt;
&lt;P&gt;2 6 2005&lt;/P&gt;
&lt;P&gt;2 7 2005&lt;/P&gt;
&lt;P&gt;2 5 2006&lt;/P&gt;
&lt;P&gt;2 6 2006&lt;/P&gt;
&lt;P&gt;2 8 2006&lt;/P&gt;
&lt;P&gt;3 9 2005&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3 10 2005&lt;/P&gt;
&lt;P&gt;3 9 2006&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3 10 2006&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;As shown above, there were 1, 2, and 3 in hospital A in year 2005. There is a new person, 4, in year 2006. In this case, I would like to give a value of 1 to observations of hospital 1 and year 2006 (i.e., 1 for the observations of groups where there is a new person).&lt;/P&gt;
&lt;P&gt;In hospital 2, there are 5, 6, and 7 in year 2005. But next year, 7 left while 8 joined. In this case, I would like to assign missing to the observations of this group (i.e., hospital 2 - year 2006).&lt;/P&gt;
&lt;P&gt;In hospital 3, there are 9 and 10 in years 2005 and 2006. For the observations of this group, I would like to assign 0 to the observations of this group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The resulting dataset would be either at hospital-year level or at hospital-person-yearl level, whichever is OK for me. Is there a simple way to make this dataset? Thanks in advance!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Aug 2020 20:17:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675290#M203434</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2020-08-07T20:17:10Z</dc:date>
    </item>
    <item>
      <title>Re: Checking whether there is a new obs within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675291#M203435</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279507"&gt;@braam&lt;/a&gt;&amp;nbsp; In addition to the sentences that explains the logic, can you please post the expected output for the input sample posted. Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 07 Aug 2020 20:30:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675291#M203435</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-08-07T20:30:59Z</dc:date>
    </item>
    <item>
      <title>Re: Checking whether there is a new obs within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675301#M203438</link>
      <description>&lt;P&gt;An important question is how do you know which are the "old" observations?&lt;/P&gt;
&lt;P&gt;Do you have a data set with the old groups in it somewhere?&lt;/P&gt;</description>
      <pubDate>Fri, 07 Aug 2020 21:11:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675301#M203438</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-07T21:11:01Z</dc:date>
    </item>
    <item>
      <title>Re: Checking whether there is a new obs within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675302#M203439</link>
      <description>&lt;P&gt;Thanks for your reply. I added some blank lines between different hospitals to read it easily.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;HospitalID PersonID Year&lt;/P&gt;
&lt;P&gt;1 1 2005&lt;/P&gt;
&lt;P&gt;1 2 2005&lt;/P&gt;
&lt;P&gt;1 3 2005&lt;/P&gt;
&lt;P&gt;1 1 2006&lt;/P&gt;
&lt;P&gt;1 2 2006&lt;/P&gt;
&lt;P&gt;1 3 2006&lt;/P&gt;
&lt;P&gt;1 4 2006&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2 5 2005&lt;/P&gt;
&lt;P&gt;2 6 2005&lt;/P&gt;
&lt;P&gt;2 7 2005&lt;/P&gt;
&lt;P&gt;2 5 2006&lt;/P&gt;
&lt;P&gt;2 6 2006&lt;/P&gt;
&lt;P&gt;2 8 2006&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3 9 2005&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3 10 2005&lt;/P&gt;
&lt;P&gt;3 9 2006&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3 10 2006&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My expected output is:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;HopsitalID Year NewPerson&lt;/P&gt;
&lt;P&gt;1 2005 .&lt;/P&gt;
&lt;P&gt;1 2006 1&lt;/P&gt;
&lt;P&gt;2 2005 .&lt;/P&gt;
&lt;P&gt;2 2006 .&lt;/P&gt;
&lt;P&gt;3 2005 .&lt;/P&gt;
&lt;P&gt;3 2006 0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the first hospital, there is a clearly new person in year 2006. So 1 would be assigned to NewPerson&amp;nbsp;in year 2006.&lt;/P&gt;
&lt;P&gt;For the second hospital, there is a new person coming in and another outgoing. So missing would be assigned to NewPerson in year 2006.&lt;/P&gt;
&lt;P&gt;For the last hospital, there is no change. So 0 would be assigned to NewPerson in 2006.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just so you know, I have a much bigger panel data. Thanks again.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Aug 2020 21:25:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675302#M203439</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2020-08-07T21:25:12Z</dc:date>
    </item>
    <item>
      <title>Re: Checking whether there is a new obs within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675303#M203440</link>
      <description>I meant, old observations are those in the previous year. &lt;BR /&gt;Let's say, observations in year 2005 serve as "old" observations for those in year 2006.</description>
      <pubDate>Fri, 07 Aug 2020 21:26:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675303#M203440</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2020-08-07T21:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: Checking whether there is a new obs within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675307#M203444</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279507"&gt;@braam&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I meant, old observations are those in the previous year. &lt;BR /&gt;Let's say, observations in year 2005 serve as "old" observations for those in year 2006.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Previous to what? What in the data tells what is a "new" obs?&lt;/P&gt;
&lt;P&gt;Listing an example does not provide a rule. You know your data, presumably, and so can tell. But we don't and to program a process we need to know the rule(s) involved that tell us what is new.&lt;/P&gt;
&lt;P&gt;Since all of the example years you have shown so far are more than 10 years old compared to the current date everything looks "old".&lt;/P&gt;
&lt;P&gt;So, are&amp;nbsp; you talking about the latest year in the data for a hospital is "new"? or Hospital PersonID the latest year is the "new"?&lt;/P&gt;
&lt;P&gt;Or is a specific year, one that you designate in the code for example, to be considered the "new" year?&lt;/P&gt;</description>
      <pubDate>Fri, 07 Aug 2020 21:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675307#M203444</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-07T21:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: Checking whether there is a new obs within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675344#M203455</link>
      <description>&lt;P&gt;So can&amp;nbsp;&lt;SPAN&gt;we assume the following the rules-&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;1. If there's a change i.e. an addition of 1 or more patients in subsequent years &lt;SPAN&gt;without any drop in existing patients&lt;/SPAN&gt;, then&amp;nbsp;&lt;SPAN&gt;NewPerson flag is 1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2. If there's a change&amp;nbsp;whereby the situation reflects drop in some existing patients and concurrently there are some additional new patients, then&amp;nbsp;NewPerson flag is missing&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;3. If there's NO change, then&amp;nbsp;NewPerson flag is 0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you please revie&lt;SPAN&gt;w and let us know all scenarios to account for?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Aug 2020 23:41:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675344#M203455</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-08-07T23:41:25Z</dc:date>
    </item>
    <item>
      <title>Re: Checking whether there is a new obs within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675354#M203463</link>
      <description>&lt;P&gt;Let's restate the objective, to:&amp;nbsp; For each hospital create 3 variables N_KEPT (number of persons in both 2005 and 2006), N_ADDED (Number in 2006 and not in 2005), and N_DROPPED (you can guess).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And it just so happens I have suggested code for my suggested redefinition of the task:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=hospitalid n_: );
  merge have (where=(year=2005) in=in2005)
        have (where=(year=2006) in=in2006);
  by hospitalid personid;

  if in2005=in2006 then n_kept+1;
  else if in2006=0 then n_dropped+1;
  else if in2005=0 then n_added+1;

  if last.hospitalid;
  output;
  call missing(of n_:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This program assumes you data are sorted by hospitalid/year/personid.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason there is a call missing is to clear out the accumulations of N_dropped, N_added, N_kept at the end of one hospital, before starting the next hospital.&amp;nbsp; And because there is an explicit call missing of these news variables, it has to be preceded by an explicit OUTPUT statement.&amp;nbsp; Otherwise the &lt;EM&gt;&lt;STRONG&gt;implicit&lt;/STRONG&gt;&lt;/EM&gt; output action just before the RUN statement would be outputting missing values instead of the calculated totals.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question: do you really just have the same two years for all hospitals?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Aug 2020 04:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675354#M203463</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-08T04:40:42Z</dc:date>
    </item>
    <item>
      <title>Re: Checking whether there is a new obs within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675370#M203476</link>
      <description>Thanks for your clarification. Your description is what I'm looking for.</description>
      <pubDate>Sat, 08 Aug 2020 06:50:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675370#M203476</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2020-08-08T06:50:47Z</dc:date>
    </item>
    <item>
      <title>Re: Checking whether there is a new obs within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675426#M203518</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279507"&gt;@braam&lt;/a&gt;&amp;nbsp; Your clarification helps. Thanks! Please try the below and see if it works-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input HospitalID PersonID Year;
cards;
1 1 2005

1 2 2005

1 3 2005

1 1 2006

1 2 2006

1 3 2006

1 4 2006

2 5 2005

2 6 2005

2 7 2005

2 5 2006

2 6 2006

2 8 2006

3 9 2005 

3 10 2005

3 9 2006 

3 10 2006
;

data want;
 if _n_=1 then do;
  dcl hash H () ;
  h.definekey  ("PersonID") ;
  h.definedone () ;
 end;
 do _n_=1 by 1 until(last.HospitalID);
  h_items=h.num_items;
  do until(last.year);
   set have;
   by HospitalID year;
   if _n_&amp;gt;1 then if  h.check()=0 then h_items=h_items-1;
   else NewPerson=1;
   h.replace();
  end;
  if _n_&amp;gt;1 then if  h_items then NewPerson=.;
  else if NewPerson=. then NewPerson=0; 
  output;
 end;
 h.clear();
 keep HospitalID year NewPerson;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Aug 2020 19:30:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-whether-there-is-a-new-obs-within-a-group/m-p/675426#M203518</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-08-08T19:30:23Z</dc:date>
    </item>
  </channel>
</rss>

