<?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 create an if then statement to drop dates for the same observation. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895513#M353821</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/77163"&gt;@Oligolas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;oh yes good point, it's also possible to simply compare to the max since earlier observations must be dropped anyway&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
   CREATE TABLE want AS
      SELECT *
      FROM have
      GROUP BY Patient_ID
      HAVING pending='YES' or Date&amp;gt;=max(Pending_Date) 
      ORDER BY obs
   ;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;"Comparing to the max" actually means reading all the data twice, once to get the max, and once to compare.&lt;/P&gt;</description>
    <pubDate>Fri, 22 Sep 2023 22:49:24 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-09-22T22:49:24Z</dc:date>
    <item>
      <title>How to create an if then statement to drop dates for the same observation.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895367#M353753</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have an example subset of 1 patient that where I created 2 separate dates. That is "Date" and "Pending_Date".&amp;nbsp; I also created a variable where each row is either Pending='YES' or Pending='NO'. Below is my table of what I have and what I want. How can I code if Date is less than Pending_Date then exclude='yes' then I would drop my exclusions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried it using an if then statement but it will only compare dates right next to each other (ex 3/29/2023 &amp;gt; blank date) but i want it to take into consideration the other dates as well. For example Obs 3 is a pending date and not greater Obs 1,2 4,5, and 8 so that wont get excluded. However, Obs 6 is a pending date and is greater than Obs 1,2, and 4 which is how I have my "Want" table.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Again I want my exclusion to be " Pending_Date &amp;gt; Date then Exclude='YES' " but I want all the "Dates" to be taken into consideration.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Whats the best way to approach this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Obs&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&lt;SPAN&gt;Patient_ID&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;Pending&lt;/TD&gt;&lt;TD&gt;Pending_Date&lt;/TD&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;AB1&lt;/TD&gt;&lt;TD&gt;NO&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;3/29/22&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;AB1&lt;/TD&gt;&lt;TD&gt;NO&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;3/29/22&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&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&gt;&lt;TD&gt;Pending&lt;/TD&gt;&lt;TD&gt;Pending_Date&lt;/TD&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;AB1&lt;/TD&gt;&lt;TD&gt;YES&lt;/TD&gt;&lt;TD&gt;3/29/22&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;AB1&lt;/TD&gt;&lt;TD&gt;NO&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;4/5/22&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;AB1&lt;/TD&gt;&lt;TD&gt;YES&lt;/TD&gt;&lt;TD&gt;4/5/22&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;AB1&lt;/TD&gt;&lt;TD&gt;YES&lt;/TD&gt;&lt;TD&gt;4/5/22&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;AB1&lt;/TD&gt;&lt;TD&gt;NO&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;4/5/22&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 22 Sep 2023 01:13:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895367#M353753</guid>
      <dc:creator>rebelde52</dc:creator>
      <dc:date>2023-09-22T01:13:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an if then statement to drop dates for the same observation.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895372#M353754</link>
      <description>&lt;P&gt;It's not clear what PENDING date you are using as a criterion.&amp;nbsp; Are all dates compared to the highest PENDING date found, or just to the closest upcoming pending date?&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2023 02:25:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895372#M353754</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-22T02:25:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an if then statement to drop dates for the same observation.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895374#M353755</link>
      <description>All dates are compared to the highest Pending date found.</description>
      <pubDate>Fri, 22 Sep 2023 02:37:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895374#M353755</guid>
      <dc:creator>rebelde52</dc:creator>
      <dc:date>2023-09-22T02:37:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an if then statement to drop dates for the same observation.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895383#M353760</link>
      <description>&lt;P&gt;I'd use SQL for this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dsd dlm=' ';
input Obs Patient_ID $ Pending  $ Pending_Date :mmddyy8. Date :mmddyy8.;
format Pending_Date Date mmddyy8.;
datalines;
1 AB1 NO  3/29/22
2 AB1 NO  3/29/22
3 AB1 YES 3/29/22
4 AB1 NO  3/29/22
5 AB1 NO  4/5/22
6 AB1 YES 4/5/22
7 AB1 YES 4/5/22
8 AB1 NO  4/5/22
;
run;

PROC SQL;
   CREATE TABLE excludes AS
      SELECT DISTINCT a.*
      FROM have a, have b
      WHERE a.Patient_ID eq b.Patient_ID 
      AND b.pending_date IS NOT NULL
      AND a.date IS NOT NULL
      AND b.pending_date &amp;gt; a.date
      ORDER BY a.obs
   ;
   CREATE TABLE want AS
      SELECT *
      FROM have
      EXCEPT
      SELECT *
      FROM excludes
   ;
   DROP TABLE excludes;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Sep 2023 06:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895383#M353760</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2023-09-22T06:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an if then statement to drop dates for the same observation.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895413#M353773</link>
      <description>&lt;P&gt;Pass through each ID twice.&amp;nbsp; In the first pass, read only the PENDING='YES' observations, and retain the last such observation with variable _MAX_PENDING_DATE.&amp;nbsp; On the second pass output only cases with PENDING='YES' or DATE not preceding _MAX_PENDING_DATE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input Obs Patient_ID $ Pending  $ Pending_Date :mmddyy8. Date :mmddyy8.;
format Pending_Date Date mmddyy8.;
datalines;
1 AB1 NO  .       3/29/22
2 AB1 NO  .       3/29/22
3 AB1 YES 3/29/22
4 AB1 NO  .       3/29/22
5 AB1 NO  .       4/5/22
6 AB1 YES 4/5/22
7 AB1 YES 4/5/22
8 AB1 NO  .       4/5/22
run;

data want (drop=_:);
  set have (in=firstpass where=(pending='YES')) 
      have (in=secondpass);
  by patient_id;

  retain _max_pending_date ;
  if firstpass=1  then _max_pending_date=pending_date;
  if secondpass=1 and (pending='YES' or date&amp;gt;=_max_pending_date);
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This program assumes the (1) data are sorted by patient_id, (2) that every patient_id has at least one pending_date observation, and (3) the last pending_date value within a patient_id is always the maximum pending_date.&amp;nbsp; It makes no assumptions about the order of the other DATE values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2023 22:38:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895413#M353773</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-22T22:38:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an if then statement to drop dates for the same observation.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895465#M353804</link>
      <description>&lt;P&gt;oh yes good point, it's also possible to simply compare to the max since earlier observations must be dropped anyway&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
   CREATE TABLE want AS
      SELECT *
      FROM have
      GROUP BY Patient_ID
      HAVING pending='YES' or Date&amp;gt;=max(Pending_Date) 
      ORDER BY obs
   ;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Sep 2023 16:06:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895465#M353804</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2023-09-22T16:06:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an if then statement to drop dates for the same observation.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895513#M353821</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/77163"&gt;@Oligolas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;oh yes good point, it's also possible to simply compare to the max since earlier observations must be dropped anyway&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
   CREATE TABLE want AS
      SELECT *
      FROM have
      GROUP BY Patient_ID
      HAVING pending='YES' or Date&amp;gt;=max(Pending_Date) 
      ORDER BY obs
   ;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;"Comparing to the max" actually means reading all the data twice, once to get the max, and once to compare.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2023 22:49:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-if-then-statement-to-drop-dates-for-the-same/m-p/895513#M353821</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-22T22:49:24Z</dc:date>
    </item>
  </channel>
</rss>

