<?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 How to select unique records using datetime variable in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308318#M61165</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I have a dataset looking at orthopedic surgery procedures over time. Date of procedure is recorded as DATETIME20. I have some duplicate entries for same procedure on same date where some entries show datetime exactly (for eg. 10OCT2016 04:25:10) and some show just the date and 0 for time (for eg. 10OCT2016 00:00:00). FOr those with duplicate entries, I need to pick up the ones with exact date time (10OCT2016 04:25:10) and delete one with no time on it (10OCT2016 00:00:00).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I have some observations with no duplicate entries on same date, but with date and 0 for time which I need to retain (for eg. 11OCT2016 00:00:00).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would appreciate if I can get help in removing duplicate entries for same day with 0 time value but retaining unique observations with date and 0 time value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID variable: Reg_No&lt;/P&gt;
&lt;P&gt;Date of Surgery: PROCEDURE_DATE (datetime 20.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the sample data from my study. &lt;FONT color="#FF6600"&gt;I just want to remove the one I marked DELETE.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Reg_No&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PROCEDURE_DATE&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 11OCT2016 00:00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12OCT2016 00:00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#FF6600"&gt;--Delete&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12OCT2016 01:20:01&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 13OCT2016 00:00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14OCT2016 02:30:10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1002&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 13OCT2016 00:00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1002&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14OCT2016 22:01:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1002&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; 15OCT2016 00:00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1003&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 15OCT2016 00:00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&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;Thank you!&lt;/P&gt;
&lt;P&gt;Sat&lt;/P&gt;</description>
    <pubDate>Mon, 31 Oct 2016 16:53:50 GMT</pubDate>
    <dc:creator>smunigala</dc:creator>
    <dc:date>2016-10-31T16:53:50Z</dc:date>
    <item>
      <title>How to select unique records using datetime variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308318#M61165</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I have a dataset looking at orthopedic surgery procedures over time. Date of procedure is recorded as DATETIME20. I have some duplicate entries for same procedure on same date where some entries show datetime exactly (for eg. 10OCT2016 04:25:10) and some show just the date and 0 for time (for eg. 10OCT2016 00:00:00). FOr those with duplicate entries, I need to pick up the ones with exact date time (10OCT2016 04:25:10) and delete one with no time on it (10OCT2016 00:00:00).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I have some observations with no duplicate entries on same date, but with date and 0 for time which I need to retain (for eg. 11OCT2016 00:00:00).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would appreciate if I can get help in removing duplicate entries for same day with 0 time value but retaining unique observations with date and 0 time value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID variable: Reg_No&lt;/P&gt;
&lt;P&gt;Date of Surgery: PROCEDURE_DATE (datetime 20.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the sample data from my study. &lt;FONT color="#FF6600"&gt;I just want to remove the one I marked DELETE.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Reg_No&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PROCEDURE_DATE&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 11OCT2016 00:00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12OCT2016 00:00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#FF6600"&gt;--Delete&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12OCT2016 01:20:01&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 13OCT2016 00:00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14OCT2016 02:30:10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1002&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 13OCT2016 00:00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1002&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14OCT2016 22:01:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1002&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; 15OCT2016 00:00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&lt;/P&gt;
&lt;P&gt;1003&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 15OCT2016 00:00:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --Retain&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;Thank you!&lt;/P&gt;
&lt;P&gt;Sat&lt;/P&gt;</description>
      <pubDate>Mon, 31 Oct 2016 16:53:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308318#M61165</guid>
      <dc:creator>smunigala</dc:creator>
      <dc:date>2016-10-31T16:53:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to select unique records using datetime variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308332#M61166</link>
      <description>&lt;P&gt;If the data is sorted by&amp;nbsp;Reg_no and procedure_date then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data temp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dateonly=datepart (procedure_date);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set temp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by reg_no dateonly;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if last.dateonly;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;seems to do what you request.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Oct 2016 17:21:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308332#M61166</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-10-31T17:21:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to select unique records using datetime variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308335#M61167</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Thanks for the quick reply. However, I am looking to remove duplicates only for the same day and not for different days. Can you check the sample data I specified? Each Reg_No has unique procedure dates (with and without time) which I want to retain. Only if a Reg_No has duplicate entries on same day, I need to pick up the procedure dates qith specified time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Satish&lt;/P&gt;</description>
      <pubDate>Mon, 31 Oct 2016 17:27:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308335#M61167</guid>
      <dc:creator>smunigala</dc:creator>
      <dc:date>2016-10-31T17:27:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to select unique records using datetime variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308336#M61168</link>
      <description>&lt;P&gt;The below solution supports the situation where you have 2+ records for the same Reg_no and same day.&lt;/P&gt;
&lt;P&gt;It will throw away the first occurrence if and only if the time = 00:00:00 while keeping all other records for that&amp;nbsp;&lt;SPAN&gt;same Reg_no and same day.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.smunigala;
input Reg_No PROCEDURE_DATE_Char $ 15-32;
PROCEDURE_DATE=input(PROCEDURE_DATE_Char,datetime20.);
procedure_date_dp=datepart(PROCEDURE_DATE);
procedure_date_tp=timepart(PROCEDURE_DATE);
format PROCEDURE_DATE datetime20. procedure_date_dp date9. procedure_date_tp time8.;
datalines;
1001          11OCT2016 00:00:00
1001          12OCT2016 00:00:00
1001          12OCT2016 01:20:01
1001          13OCT2016 00:00:00
1001          14OCT2016 02:30:10
1002          13OCT2016 00:00:00
1002          14OCT2016 22:01:00
1002          15OCT2016 00:00:00
1003          15OCT2016 00:00:00
;
run;

proc sort data=work.smunigala NODUPKEY;
 by Reg_No procedure_date_dp procedure_date_tp;
run;

data work.smunigala1;
 set work.smunigala;
 by Reg_No procedure_date_dp procedure_date_tp;
 if first.procedure_date_dp then do;
  if     first.procedure_date_dp NE last.procedure_date_dp
     AND procedure_date_tp = '00:00:00't
   then delete;
  end;
run;
/* end of program */
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Oct 2016 17:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308336#M61168</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2016-10-31T17:37:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to select unique records using datetime variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308338#M61169</link>
      <description>Quick addendum: if there is only one record for a Reg_no &amp;amp; day combination, it is kept in the output in any case (even if the time was 00:00:00).&lt;BR /&gt;Koen</description>
      <pubDate>Mon, 31 Oct 2016 17:43:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308338#M61169</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2016-10-31T17:43:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to select unique records using datetime variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308341#M61170</link>
      <description>&lt;PRE class=" language-sas"&gt;proc sql;&lt;BR /&gt; create table NEWDATASETNAME(drop=count_flag time_flag) as &lt;BR /&gt; select *&lt;BR /&gt; , case when count(reg_no)&amp;gt;1 then 1 else 0 end as count_flag&lt;BR /&gt; , case when timepart(procedure_date)='00:00:00't then 1 else 0 end as time_flag&lt;BR /&gt; from CURRENTDATASETNAME&lt;BR /&gt; group by reg_no,datepart(procedure_date)&lt;BR /&gt; having calculated count_flag + calculated time_flag &amp;lt; 2;&lt;BR /&gt;quit; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Oct 2016 18:04:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308341#M61170</guid>
      <dc:creator>jhlaramore</dc:creator>
      <dc:date>2016-10-31T18:04:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to select unique records using datetime variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308356#M61171</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/55310"&gt;@smunigala&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Thanks for the quick reply. However, I am looking to remove duplicates only for the same day and not for different days. Can you check the sample data I specified? Each Reg_No has unique procedure dates (with and without time) which I want to retain. Only if a Reg_No has duplicate entries on same day, I need to pick up the procedure dates qith specified time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Satish&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Did you try the code?&lt;/P&gt;
&lt;P&gt;Do you have records with more than 2 of the same date?&lt;/P&gt;</description>
      <pubDate>Mon, 31 Oct 2016 19:19:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/308356#M61171</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-10-31T19:19:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to select unique records using datetime variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/310712#M61300</link>
      <description>&lt;P&gt;Thank you all for helping me with this!&lt;/P&gt;
&lt;P&gt;Sat&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 16:34:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-select-unique-records-using-datetime-variable/m-p/310712#M61300</guid>
      <dc:creator>smunigala</dc:creator>
      <dc:date>2016-11-10T16:34:54Z</dc:date>
    </item>
  </channel>
</rss>

