<?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: find subjects with more than 3 duplicate values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289440#M59781</link>
    <description>If the original sort order is that important you need a variable that holds that information. Like a datatime. If that's doesn't exist have a sequence no added in a preceeding step, then use that in an order by clause in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; SQL.</description>
    <pubDate>Thu, 04 Aug 2016 07:07:48 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2016-08-04T07:07:48Z</dc:date>
    <item>
      <title>find subjects with more than 3 duplicate values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289211#M59730</link>
      <description>&lt;P&gt;Have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;var1&lt;/TD&gt;&lt;TD&gt;var2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5000&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2015&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2015&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt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LE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think there should be an easy way to this in SAS. I need to select the id's where 3 or more observations of the same &amp;nbsp;ID have var2=1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2016 14:28:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289211#M59730</guid>
      <dc:creator>ari</dc:creator>
      <dc:date>2016-08-03T14:28:50Z</dc:date>
    </item>
    <item>
      <title>Re: find subjects with more than 3 duplicate values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289213#M59731</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select id from have
where var2 = 1
group by id
having count(*) &amp;gt;= 3
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Aug 2016 13:36:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289213#M59731</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-03T13:36:46Z</dc:date>
    </item>
    <item>
      <title>Re: find subjects with more than 3 duplicate values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289214#M59732</link>
      <description>&lt;P&gt;a&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2016 13:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289214#M59732</guid>
      <dc:creator>ari</dc:creator>
      <dc:date>2016-08-03T13:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: find subjects with more than 3 duplicate values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289218#M59733</link>
      <description>&lt;P&gt;Just add the other variables in the select list of the SQL statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id var1 var2;
cards;
1 5000 1
2 2015 1
2 2015 1
3 700 1
3 700 1
3 700 1
;
run;

proc sql;
create table want as
select id, var1, var2 from have
where var2 = 1
group by id
having count(*) &amp;gt;= 3
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can also see the best way to provide test data (data step with inline data in cards;)&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2016 13:49:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289218#M59733</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-03T13:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: find subjects with more than 3 duplicate values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289232#M59734</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp;this program does not preserve the order of the id's. I need to look at the subsequent observation of the same id's.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for instance,&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token keyword"&gt;cards&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token data string"&gt;1 5000 1
2 2015 1
2 2015 1
3 700 1
3 700 1
3 700 1&lt;BR /&gt;2 2015 1&lt;BR /&gt;3 700 1&lt;BR /&gt;3 700 1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to select those id's separately rather than combining them together.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2016 14:33:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289232#M59734</guid>
      <dc:creator>ari</dc:creator>
      <dc:date>2016-08-03T14:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: find subjects with more than 3 duplicate values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289254#M59736</link>
      <description>&lt;P&gt;ari,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kurt's program still gets you most of the way there.&amp;nbsp; If you want to extract all records for those IDs, use Kurt's results to extract:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create table want_list as&lt;/P&gt;
&lt;P&gt;select id from have&lt;/P&gt;
&lt;P&gt;where var2=1&lt;/P&gt;
&lt;P&gt;group by id&lt;/P&gt;
&lt;P&gt;having count(*) &amp;gt;= 3;&lt;/P&gt;
&lt;P&gt;create table want as&lt;/P&gt;
&lt;P&gt;select * from have where id in (select id from want_list);&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may need to correct the syntax on the final SELECT ... I'm much more at home with a DATA step than SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even in this sample code, SQL does not promise to maintain the order of the incoming records.&amp;nbsp; It's just a characteristic of SQL.&amp;nbsp; If you want to guarantee that order, you will need to use a DATA step instead.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2016 14:59:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289254#M59736</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-08-03T14:59:24Z</dc:date>
    </item>
    <item>
      <title>Re: find subjects with more than 3 duplicate values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289430#M59775</link>
      <description>&lt;P&gt;Do you only need to preserve the original order, or do you need to select ID's that have three &lt;U&gt;successive&lt;/U&gt; 1's in the original dataset?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Stating your complete requirements shortens the process of finding a working solution considerably &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2016 05:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289430#M59775</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-04T05:21:18Z</dc:date>
    </item>
    <item>
      <title>Re: find subjects with more than 3 duplicate values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289440#M59781</link>
      <description>If the original sort order is that important you need a variable that holds that information. Like a datatime. If that's doesn't exist have a sequence no added in a preceeding step, then use that in an order by clause in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; SQL.</description>
      <pubDate>Thu, 04 Aug 2016 07:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289440#M59781</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-08-04T07:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: find subjects with more than 3 duplicate values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289472#M59790</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, I need to preserve the original order and at the same time I need to select ID's with 3 successive 1's.&amp;nbsp;I think sequence no as suggested by Linush works. I will try that option.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2016 09:37:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289472#M59790</guid>
      <dc:creator>ari</dc:creator>
      <dc:date>2016-08-04T09:37:17Z</dc:date>
    </item>
    <item>
      <title>Re: find subjects with more than 3 duplicate values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289473#M59791</link>
      <description>&lt;P&gt;Sounds good idea. I will check this. Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2016 09:37:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289473#M59791</guid>
      <dc:creator>ari</dc:creator>
      <dc:date>2016-08-04T09:37:41Z</dc:date>
    </item>
    <item>
      <title>Re: find subjects with more than 3 duplicate values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289483#M59792</link>
      <description>&lt;PRE&gt;

data have;
input id var1 var2;
cards;
1 5000 1
2 2015 1
2 2015 1
3 700 1
3 700 1
3 700 1
;
run;
data want;
 do i=1 by 1 until(last.id);
  set have;
  by id;
 end;
 do until(last.id);
  set have;
  by id;
  if i gt 2 then output;
 end;
 drop i;
run;

&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Aug 2016 10:38:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-subjects-with-more-than-3-duplicate-values/m-p/289483#M59792</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-04T10:38:48Z</dc:date>
    </item>
  </channel>
</rss>

