<?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: Deleting rows from a dataset that have the same value in a particular column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Deleting-rows-from-a-dataset-that-have-the-same-value-in-a/m-p/768230#M243652</link>
    <description>&lt;P&gt;1. The code you provided does not run. Please always vet it by pasting back to SAS and running it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Here is step by step, easy to follow (if not at all efficient) method:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table MIN as 
  select PATIENT_ID, min(PRESCRIPTION_DATE) as MIN 
  from CLASS 
  group by 1 ;

  create table COUNT as 
  select CLASS.PATIENT_ID, count(distinct DRUG) as COUNT
  from CLASS, MIN  
  where CLASS.PATIENT_ID=MIN.PATIENT_ID and PRESCRIPTION_DATE=MIN
  group by 1 ;

  delete from CLASS where PATIENT_ID in (select PATIENT_ID from COUNT where COUNT&amp;gt;1) ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Sep 2021 06:57:47 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2021-09-17T06:57:47Z</dc:date>
    <item>
      <title>Deleting rows from a dataset that have the same value in a particular column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-rows-from-a-dataset-that-have-the-same-value-in-a/m-p/768212#M243646</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm having trouble figuring out how to delete complete rows from a dataset when the first prescription date for the patient for more than one drug is the same. Here is an example dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data WORK.CLASS(label='Prescription Data');
   infile datalines dsd truncover;
   input Patient_ID $2  Prescription_Date:MMDDYY8. Drug:$1 ;
 Datalines;
XX 01/01/21  A
XX 01/01/21 B
XX 03/15/21  A
XX 04/15/21  C
YY 01/01/21  A
YY 02/10/21 B
YY 03/15/21  A
YY 04/15/21  C
;;;;&lt;/PRE&gt;
&lt;P&gt;The table looks something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;Patient_ID&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;Prescription Date&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;Drug&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;
&lt;P&gt;XX&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;01/01/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;A&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;XX&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;01/01/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;XX&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;03/15/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;XX&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;04/15/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;YY&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;01/01/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;YY&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;02/10/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;YY&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;04/15/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;A&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;YY&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;04/15/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now for patient XX - they've been prescribed drugs A and B on the exact same date the first time they've ever been prescribed a drug and hence I want all 4 rows for patient XX deleted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, for patient YY, there is only one 'first prescription date' which is 01/01/21. They do have a subsequent prescription for drug A and B on 04/15/21. I don't want any rows for patient YY deleted because on their 'first prescription date', they only had one drug prescribed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone help?&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 02:27:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-rows-from-a-dataset-that-have-the-same-value-in-a/m-p/768212#M243646</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2021-09-17T02:27:24Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting rows from a dataset that have the same value in a particular column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-rows-from-a-dataset-that-have-the-same-value-in-a/m-p/768217#M243648</link>
      <description>&lt;P&gt;Probably a much easier way but it's late, my suggestion would be a double DoW loop but they can be complicated to code or understand. You may also want to just use multiple steps instead if that's easier.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Find the number unique of drugs per day&lt;/P&gt;
&lt;P&gt;2. Identify the first day and if it has more than 2 drugs write that ID to a table&lt;/P&gt;
&lt;P&gt;3. Filter based on ID's in table2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table drugsPerDay as
select patient_id, prescription_date, count(distinct drug) as num_drugs
from have
group by patient_id, prescription_date;
quit;

data singleDrugs;
set drugsPerDay;
by patient_ID prescription_date;
if first.patient_id and num_drugs = 1;
run;

data want_data;
merge have (in=t1) singleDrugs (in=t2);
by patient_id;
if t2;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or squishing it all together in SQL (SQL is the easiest method to get distinct counts)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

create table want as
select * from have where patient_id NOT in (
select patient_id from(
select patient_id, prescription_date, count(distinct drug) as num_drugs
from have
group by patient_id, prescription_date)
where num_drugs &amp;gt; 1
group by patient_id

having Prescription_Date=min(Prescription_Date));

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/341454"&gt;@alaxman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm having trouble figuring out how to delete complete rows from a dataset when the first prescription date for the patient for more than one drug is the same. Here is an example dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data WORK.CLASS(label='Prescription Data');
   infile datalines dsd truncover;
   input Patient_ID $2  Prescription_Date:MMDDYY8. Drug:$1 ;
 Datalines;
XX 01/01/21  A
XX 01/01/21 B
XX 03/15/21  A
XX 04/15/21  C
YY 01/01/21  A
YY 02/10/21 B
YY 03/15/21  A
YY 04/15/21  C
;;;;&lt;/PRE&gt;
&lt;P&gt;The table looks something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;Patient_ID&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;Prescription Date&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;Drug&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;
&lt;P&gt;XX&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;01/01/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;A&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;XX&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;01/01/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;XX&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;03/15/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;XX&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;04/15/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;YY&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;01/01/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;YY&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;02/10/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;YY&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;04/15/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;A&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;YY&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;04/15/21&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="29px" class="lia-align-center"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now for patient XX - they've been prescribed drugs A and B on the exact same date the first time they've ever been prescribed a drug and hence I want all 4 rows for patient XX deleted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, for patient YY, there is only one 'first prescription date' which is 01/01/21. They do have a subsequent prescription for drug A and B on 04/15/21. I don't want any rows for patient YY deleted because on their 'first prescription date', they only had one drug prescribed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone help?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 03:17:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-rows-from-a-dataset-that-have-the-same-value-in-a/m-p/768217#M243648</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-09-17T03:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting rows from a dataset that have the same value in a particular column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-rows-from-a-dataset-that-have-the-same-value-in-a/m-p/768230#M243652</link>
      <description>&lt;P&gt;1. The code you provided does not run. Please always vet it by pasting back to SAS and running it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Here is step by step, easy to follow (if not at all efficient) method:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table MIN as 
  select PATIENT_ID, min(PRESCRIPTION_DATE) as MIN 
  from CLASS 
  group by 1 ;

  create table COUNT as 
  select CLASS.PATIENT_ID, count(distinct DRUG) as COUNT
  from CLASS, MIN  
  where CLASS.PATIENT_ID=MIN.PATIENT_ID and PRESCRIPTION_DATE=MIN
  group by 1 ;

  delete from CLASS where PATIENT_ID in (select PATIENT_ID from COUNT where COUNT&amp;gt;1) ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 06:57:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-rows-from-a-dataset-that-have-the-same-value-in-a/m-p/768230#M243652</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-09-17T06:57:47Z</dc:date>
    </item>
  </channel>
</rss>

