<?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 drop observations by group with a condition? in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-drop-observations-by-group-with-a-condition/m-p/388361#M11738</link>
    <description>&lt;P&gt;If you're interested in DATA step solutions, you could use a double-DoW loop.&amp;nbsp; It requires two passes of the data, and the data must be sorted.&amp;nbsp; You process the data in groups, by Security_ID, first to identify whether Ticker changes within the group, and then a second time to output the records for Security_ID groups where Ticker is constant.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.Security_ID);
    set have;
    by Security_ID Ticker;

    *If ticker changes within security_ID, flag the security_ID;
    if first.Ticker=1 and first.Security_ID=0 then _deleteme=1;
  end;

  do until(last.Security_ID);
    set have;
    by Security_ID;

    if NOT (_deleteme) then output;
  end;

  drop _: ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;w&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Aug 2017 02:58:30 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2017-08-16T02:58:30Z</dc:date>
    <item>
      <title>How to drop observations by group with a condition?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-drop-observations-by-group-with-a-condition/m-p/388280#M11733</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a very large dataset with several variables, including stock ticker and security ID. &amp;nbsp;I am trying to include only observations where the stock ticker was constant over the time period I have in my dataset. Here are samples of 2 stocks:&lt;/P&gt;&lt;DIV class="branch"&gt;&lt;DIV&gt;&lt;DIV align="center"&gt;TICKER SECURITY_ID &lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ABSIE&lt;/TD&gt;&lt;TD&gt;5001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ABSIE&lt;/TD&gt;&lt;TD&gt;5001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ABSIE&lt;/TD&gt;&lt;TD&gt;5001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ABSIE&lt;/TD&gt;&lt;TD&gt;5001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ABSIE&lt;/TD&gt;&lt;TD&gt;5001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ZZZZ&lt;/TD&gt;&lt;TD&gt;5001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ZZZZ&lt;/TD&gt;&lt;TD&gt;5001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ZZZZ&lt;/TD&gt;&lt;TD&gt;5001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ZZZZ&lt;/TD&gt;&lt;TD&gt;5001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ZZZZ&lt;/TD&gt;&lt;TD&gt;5001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ZZZZ&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;5001&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class="branch"&gt;&lt;DIV align="center"&gt;&lt;DIV class="branch"&gt;&lt;DIV align="center"&gt;&lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;101594&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;101594&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;101594&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;101594&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;101594&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;101594&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;101594&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;101594&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;101594&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;101594&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;101594&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;101594&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAPL&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;101594&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;I would want to keep all observations with security ID 101594 and delete&amp;nbsp;all observations with security ID 5001. So, if there is a "ZZZZ" present in any single observation with a&amp;nbsp;security ID, I need to delete all observations with the security ID. In other words, every observation with the same security ID should have the same ticker.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks so much, I really appreciate it!&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="branch"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2017 19:48:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-drop-observations-by-group-with-a-condition/m-p/388280#M11733</guid>
      <dc:creator>ag8711</dc:creator>
      <dc:date>2017-08-15T19:48:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to drop observations by group with a condition?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-drop-observations-by-group-with-a-condition/m-p/388286#M11734</link>
      <description>&lt;P&gt;How large is large?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one approach but I make no claim for efficiency:&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as
   select have.* 
   from (
         select security_id,count(ticker) as tcount
         from (select distinct ticker, security_id from have )
         group by security_id
         having calculated tcount = 1
        ) as a
        left join have 
        on a.security_id=have.security_id
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;the innermost part gets a set of single ticker and security_id, then count to see if there are more than one ticker associated with the security_id. Then use the security_id with a count of 1 to match to your whole data set to select the desired tickets (and any other data in the set).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2017 20:04:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-drop-observations-by-group-with-a-condition/m-p/388286#M11734</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-15T20:04:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to drop observations by group with a condition?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-drop-observations-by-group-with-a-condition/m-p/388287#M11735</link>
      <description>&lt;P&gt;untested:&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
group by security_id
having max(ticker)=min(ticker);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Aug 2017 20:10:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-drop-observations-by-group-with-a-condition/m-p/388287#M11735</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-15T20:10:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to drop observations by group with a condition?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-drop-observations-by-group-with-a-condition/m-p/388359#M11737</link>
      <description>&lt;P&gt;This might work:&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
group by security_id
having count(distinct ticker) = 1;
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Aug 2017 02:18:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-drop-observations-by-group-with-a-condition/m-p/388359#M11737</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-08-16T02:18:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to drop observations by group with a condition?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-drop-observations-by-group-with-a-condition/m-p/388361#M11738</link>
      <description>&lt;P&gt;If you're interested in DATA step solutions, you could use a double-DoW loop.&amp;nbsp; It requires two passes of the data, and the data must be sorted.&amp;nbsp; You process the data in groups, by Security_ID, first to identify whether Ticker changes within the group, and then a second time to output the records for Security_ID groups where Ticker is constant.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.Security_ID);
    set have;
    by Security_ID Ticker;

    *If ticker changes within security_ID, flag the security_ID;
    if first.Ticker=1 and first.Security_ID=0 then _deleteme=1;
  end;

  do until(last.Security_ID);
    set have;
    by Security_ID;

    if NOT (_deleteme) then output;
  end;

  drop _: ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;w&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Aug 2017 02:58:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-drop-observations-by-group-with-a-condition/m-p/388361#M11738</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-08-16T02:58:30Z</dc:date>
    </item>
  </channel>
</rss>

