<?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: Select one observation per ID from multiple rows per ID based on criteria. in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Select-one-observation-per-ID-from-multiple-rows-per-ID-based-on/m-p/938151#M42149</link>
    <description>&lt;P&gt;It appears you have a sorted order to your data:&amp;nbsp; by ID month.&amp;nbsp; Let's pull the observations in a slightly different order:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have (where=(case=1) in=have_case)
have (where=(case=0) in=have_control);
by id;
retain any_case 'Y';
*condition 1;
if first.id then do;
   if have_case then any_case='Y';
   else any_case='N';
end;
if have_control and any_case='Y' then delete;
*condition 2;
if have_control and not first.id then delete; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code is untested, since you have the data to work with.&amp;nbsp; But it looks right (even if it's mildly cumbersome).&lt;/P&gt;
&lt;P&gt;If there are multiple case records for an ID, this selects them all.&amp;nbsp; I'm not sure if that is your intention or not.&amp;nbsp; I'm interpreting the title vs. the description of the conditions as saying opposite things about that question.&lt;/P&gt;</description>
    <pubDate>Sat, 03 Aug 2024 03:48:10 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2024-08-03T03:48:10Z</dc:date>
    <item>
      <title>Select one observation per ID from multiple rows per ID based on criteria.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Select-one-observation-per-ID-from-multiple-rows-per-ID-based-on/m-p/938146#M42146</link>
      <description>&lt;P&gt;I have a large dataset with multiple rows per ID. I am trying to create a dataset with only one row per id (no duplicates), but on specific conditions. Data looks like this:&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TB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lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;The conditions for retaining the observation in the dataset are:&lt;/P&gt;&lt;P&gt;1) Select all cases (case=1). If the ID also has observations with control=1, remove those observations&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;ID 2 month 9 would be retained&lt;/LI&gt;&lt;LI&gt;ID 4 month 11 would be retained and ID 4 month 7 would be removed&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;2) For IDs with multiple control observations, select the observation with the first/lowest month.&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;ID 5 month 3 would be retained&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I know I can achieve criteria #2 with something like:&lt;/P&gt;&lt;P&gt;Data want; Set have;&lt;/P&gt;&lt;P&gt;By ID Month;&lt;/P&gt;&lt;P&gt;If first.ID; run;&lt;/P&gt;&lt;P&gt;But how can I achieve #1?&lt;/P&gt;</description>
      <pubDate>Sat, 03 Aug 2024 01:33:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Select-one-observation-per-ID-from-multiple-rows-per-ID-based-on/m-p/938146#M42146</guid>
      <dc:creator>m5</dc:creator>
      <dc:date>2024-08-03T01:33:07Z</dc:date>
    </item>
    <item>
      <title>Re: Select one observation per ID from multiple rows per ID based on criteria.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Select-one-observation-per-ID-from-multiple-rows-per-ID-based-on/m-p/938148#M42147</link>
      <description>&lt;P&gt;Just clarifying ....&lt;/P&gt;
&lt;P&gt;Please confirm that ID 2 Month 8 should be removed.&lt;/P&gt;
&lt;P&gt;Please confirm that there is no YEAR in the data.&amp;nbsp; Just looking at MONTH is sufficient to determine which observation is earlier.&lt;/P&gt;</description>
      <pubDate>Sat, 03 Aug 2024 02:16:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Select-one-observation-per-ID-from-multiple-rows-per-ID-based-on/m-p/938148#M42147</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-08-03T02:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: Select one observation per ID from multiple rows per ID based on criteria.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Select-one-observation-per-ID-from-multiple-rows-per-ID-based-on/m-p/938149#M42148</link>
      <description>&lt;P&gt;Correct, ID 2 month 8 should be removed. There is no year in the data, month is sufficient. Thank you!&lt;/P&gt;</description>
      <pubDate>Sat, 03 Aug 2024 02:28:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Select-one-observation-per-ID-from-multiple-rows-per-ID-based-on/m-p/938149#M42148</guid>
      <dc:creator>m5</dc:creator>
      <dc:date>2024-08-03T02:28:43Z</dc:date>
    </item>
    <item>
      <title>Re: Select one observation per ID from multiple rows per ID based on criteria.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Select-one-observation-per-ID-from-multiple-rows-per-ID-based-on/m-p/938151#M42149</link>
      <description>&lt;P&gt;It appears you have a sorted order to your data:&amp;nbsp; by ID month.&amp;nbsp; Let's pull the observations in a slightly different order:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have (where=(case=1) in=have_case)
have (where=(case=0) in=have_control);
by id;
retain any_case 'Y';
*condition 1;
if first.id then do;
   if have_case then any_case='Y';
   else any_case='N';
end;
if have_control and any_case='Y' then delete;
*condition 2;
if have_control and not first.id then delete; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code is untested, since you have the data to work with.&amp;nbsp; But it looks right (even if it's mildly cumbersome).&lt;/P&gt;
&lt;P&gt;If there are multiple case records for an ID, this selects them all.&amp;nbsp; I'm not sure if that is your intention or not.&amp;nbsp; I'm interpreting the title vs. the description of the conditions as saying opposite things about that question.&lt;/P&gt;</description>
      <pubDate>Sat, 03 Aug 2024 03:48:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Select-one-observation-per-ID-from-multiple-rows-per-ID-based-on/m-p/938151#M42149</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-08-03T03:48:10Z</dc:date>
    </item>
    <item>
      <title>Re: Select one observation per ID from multiple rows per ID based on criteria.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Select-one-observation-per-ID-from-multiple-rows-per-ID-based-on/m-p/938173#M42150</link>
      <description>&lt;P&gt;Here's another, simpler possibility.&amp;nbsp; It's possible this is all you really want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have (where=(case=1))
have (where=(case=0));
by id;
if first.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It keeps first case record (deleting all else).&amp;nbsp; If there are no case records, it keeps the first control record.&lt;/P&gt;</description>
      <pubDate>Sat, 03 Aug 2024 19:54:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Select-one-observation-per-ID-from-multiple-rows-per-ID-based-on/m-p/938173#M42150</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-08-03T19:54:02Z</dc:date>
    </item>
  </channel>
</rss>

