<?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: If a blank occurs in any observation within a group, flag each proceeding value. in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/If-a-blank-occurs-in-any-observation-within-a-group-flag-each/m-p/770632#M30906</link>
    <description>&lt;P&gt;IMO, your last observation of patient 2 should be flagged "Return".&lt;/P&gt;
&lt;P&gt;This code does it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm="09"x dsd truncover;
input
  date_admit :mmddyy10.
  date_discharge :mmddyy10.
  id_patient $
  n
;
format
  date_admit
  date_discharge yymmdd10.
;
datalines;
9/27/2019	1/10/2020	1	1
1/28/2021	4/27/2021	1	2
7/1/2021	.	1	3	
7/13/2021	.	1	4	.
7/2/2020	11/10/2020	2	1
7/13/2020	4/6/2021	2	2	
10/22/2020	.	2	3	
11/10/2020	3/31/2021	2	4
5/26/2021	.	2	5	
;

data want;
set have;
by id_patient;
retain l_disc;
if first.id_patient
then do;
  want = "First time";
  l_disc = .;
end;
else do;
  if l_disc ne . and date_admit &amp;gt; l_disc then want = "Return";
end;
if date_discharge ne . then l_disc = max(l_disc,date_discharge);
if coalesce(date_discharge,lag(date_discharge)) = . then want = "";
drop l_disc;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note how example data is presented in readily usable form as a data step with datalines; please do so yourself in the future.&lt;/P&gt;</description>
    <pubDate>Mon, 27 Sep 2021 13:29:56 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-09-27T13:29:56Z</dc:date>
    <item>
      <title>If a blank occurs in any observation within a group, flag each proceeding value.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-a-blank-occurs-in-any-observation-within-a-group-flag-each/m-p/770456#M30874</link>
      <description>&lt;P&gt;Hello - I have a dataset with date admit and date discharge.&amp;nbsp; There are cases where the patient may have been admitted again without ever discharging.&amp;nbsp; In order to figure out the number of patients returning, I need to flag those that never actually discharged.&amp;nbsp; So for example, since this patient never discharged and was re-admitted on 1/23, I want observation 2 and 3 to be flagged as a patient who never discharged.&amp;nbsp; The issue is each group (Patient ID) has a different number of observations.&amp;nbsp; I couldn't figure out how to use lag to look back n times.&amp;nbsp; It seems to need a specific number.&amp;nbsp; I also can't just use first.ID or last.ID because the missing date may occur in the middle of the group.&amp;nbsp; Hope this makes sense!&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Patient&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Date___Admit&lt;/TD&gt;&lt;TD&gt;Date___Discharge&lt;/TD&gt;&lt;TD&gt;n&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3/26/2019&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/23/2020&lt;/TD&gt;&lt;TD&gt;6/19/2020&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;8/21/2020&lt;/TD&gt;&lt;TD&gt;7/19/2021&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Sep 2021 04:06:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-a-blank-occurs-in-any-observation-within-a-group-flag-each/m-p/770456#M30874</guid>
      <dc:creator>yma0424</dc:creator>
      <dc:date>2021-09-26T04:06:05Z</dc:date>
    </item>
    <item>
      <title>Re: If a blank occurs in any observation within a group, flag each proceeding value.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-a-blank-occurs-in-any-observation-within-a-group-flag-each/m-p/770465#M30877</link>
      <description>&lt;P&gt;Join with itself, with a filter on the second dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge
  have
  have (
    in=t2
    keep=patient date_discharge
    rename=(date_discharge=disc)
    where=(disc = .)
  )
;
if t2 then non_disc = 1;
drop disc;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 26 Sep 2021 06:59:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-a-blank-occurs-in-any-observation-within-a-group-flag-each/m-p/770465#M30877</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-26T06:59:43Z</dc:date>
    </item>
    <item>
      <title>Re: If a blank occurs in any observation within a group, flag each proceeding value.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-a-blank-occurs-in-any-observation-within-a-group-flag-each/m-p/770563#M30894</link>
      <description>&lt;P&gt;Thank you!&amp;nbsp; This worked great!&amp;nbsp; Only thing is, I left something out.&amp;nbsp; I have to define: new patients and returning patients.&amp;nbsp; Those that you helped me with are neither new or returning.&amp;nbsp; They have an admission with no discharge, therefore they're still receiving services in some program.&amp;nbsp; The problem I'm running into is, if I use the merge technique, it overrides some of my defined returning patients.&amp;nbsp; So for example, Patient 1 is a return patient when they're admitted on 7/1/2021, but it's flagged as a neither.&amp;nbsp; Your technique helped me with Patient 2, as using lag does not work for that one (note: the patient was admitted on 7/10/20 - prior to their discharge date on 11/10/20 - that's why this patient is considered neither a new or retuning patient).&amp;nbsp; Do you know of a way to fix this issue?&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Date___Admit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lt;/TD&gt;&lt;TD&gt;4/6/2021&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10/22/2020&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;11/10/2020&lt;/TD&gt;&lt;TD&gt;3/31/2021&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5/26/2021&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 27 Sep 2021 04:15:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-a-blank-occurs-in-any-observation-within-a-group-flag-each/m-p/770563#M30894</guid>
      <dc:creator>yma0424</dc:creator>
      <dc:date>2021-09-27T04:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: If a blank occurs in any observation within a group, flag each proceeding value.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-a-blank-occurs-in-any-observation-within-a-group-flag-each/m-p/770632#M30906</link>
      <description>&lt;P&gt;IMO, your last observation of patient 2 should be flagged "Return".&lt;/P&gt;
&lt;P&gt;This code does it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm="09"x dsd truncover;
input
  date_admit :mmddyy10.
  date_discharge :mmddyy10.
  id_patient $
  n
;
format
  date_admit
  date_discharge yymmdd10.
;
datalines;
9/27/2019	1/10/2020	1	1
1/28/2021	4/27/2021	1	2
7/1/2021	.	1	3	
7/13/2021	.	1	4	.
7/2/2020	11/10/2020	2	1
7/13/2020	4/6/2021	2	2	
10/22/2020	.	2	3	
11/10/2020	3/31/2021	2	4
5/26/2021	.	2	5	
;

data want;
set have;
by id_patient;
retain l_disc;
if first.id_patient
then do;
  want = "First time";
  l_disc = .;
end;
else do;
  if l_disc ne . and date_admit &amp;gt; l_disc then want = "Return";
end;
if date_discharge ne . then l_disc = max(l_disc,date_discharge);
if coalesce(date_discharge,lag(date_discharge)) = . then want = "";
drop l_disc;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note how example data is presented in readily usable form as a data step with datalines; please do so yourself in the future.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Sep 2021 13:29:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-a-blank-occurs-in-any-observation-within-a-group-flag-each/m-p/770632#M30906</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-27T13:29:56Z</dc:date>
    </item>
  </channel>
</rss>

