<?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: Keep lines where the values are the same in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810648#M319681</link>
    <description>Very nice. Didn't know that option existed until now.</description>
    <pubDate>Fri, 29 Apr 2022 13:59:04 GMT</pubDate>
    <dc:creator>average_joe</dc:creator>
    <dc:date>2022-04-29T13:59:04Z</dc:date>
    <item>
      <title>Keep lines where the values are the same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810615#M319668</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;I have a large dataset with patients and their consumed fruits. And I would like to create a new dataset with only the lines where a patient has consumed any fruit twice or more.&lt;/P&gt;&lt;P&gt;This is my starting point:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data exercise;&lt;BR /&gt;input patient fruit$;&lt;BR /&gt;cards;&lt;BR /&gt;1 Apple&lt;BR /&gt;1 Pear&lt;BR /&gt;1 Apple&lt;BR /&gt;1 Berry&lt;BR /&gt;1 Berry&lt;BR /&gt;2 Apple&lt;BR /&gt;2 Pear&lt;BR /&gt;3 Pear&lt;BR /&gt;3 Pear&lt;BR /&gt;3 Berry&lt;BR /&gt;4 Pear&lt;BR /&gt;4 Berry&lt;BR /&gt;4 Berry&lt;BR /&gt;4 Berry&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;
&lt;/PRE&gt;&lt;P&gt;And this is how it should look after:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 Apple&lt;BR /&gt;1 Apple&lt;BR /&gt;1 Berry&lt;BR /&gt;1 Berry&lt;BR /&gt;3 Pear&lt;BR /&gt;3 Pear&lt;BR /&gt;4 Berry&lt;BR /&gt;4 Berry&lt;BR /&gt;4 Berry&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;</description>
      <pubDate>Fri, 29 Apr 2022 12:57:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810615#M319668</guid>
      <dc:creator>Nina4</dc:creator>
      <dc:date>2022-04-29T12:57:07Z</dc:date>
    </item>
    <item>
      <title>Re: Keep lines where the values are the same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810623#M319672</link>
      <description>&lt;P&gt;Take a look at First. Last. and &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0yeyftk8ftuckn1o5qzy53284gz.htm#n1gd3sjlellqfxn1q67on4bzgsfs" target="_self"&gt;BY group processing&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data exercise;
input patient fruit$;
cards;
1 Apple
1 Pear
1 Apple
1 Berry
1 Berry
2 Apple
2 Pear
3 Pear
3 Pear
3 Berry
4 Pear
4 Berry
4 Berry
4 Berry
;
run;

proc sort data=exercise out=srtd_exercise ;
	by patient fruit ;
run ;

data want ;
	set srtd_exercise ;
	by patient fruit ;
	put _all_ ;
	if first.fruit=1 and last.fruit=1 then
		delete ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Apr 2022 13:20:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810623#M319672</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2022-04-29T13:20:41Z</dc:date>
    </item>
    <item>
      <title>Re: Keep lines where the values are the same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810627#M319673</link>
      <description>&lt;P&gt;Try this.&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
    a.*
from
    exercise  a
    join
    (select patient, fruit, count(*) as cnt
     from exercise
     group by 1,2
     having count(*) &amp;gt; 1
    )  b
        on a.patient = b.patient
           and a.fruit = b.fruit
;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Apr 2022 13:20:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810627#M319673</guid>
      <dc:creator>average_joe</dc:creator>
      <dc:date>2022-04-29T13:20:58Z</dc:date>
    </item>
    <item>
      <title>Re: Keep lines where the values are the same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810628#M319674</link>
      <description>&lt;P&gt;Thank you both!&amp;nbsp;&lt;BR /&gt;Both of your solution work great&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Apr 2022 13:27:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810628#M319674</guid>
      <dc:creator>Nina4</dc:creator>
      <dc:date>2022-04-29T13:27:27Z</dc:date>
    </item>
    <item>
      <title>Re: Keep lines where the values are the same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810646#M319680</link>
      <description>&lt;PRE&gt;data exercise;
input patient fruit$;
cards;
1 Apple
1 Pear
1 Apple
1 Berry
1 Berry
2 Apple
2 Pear
3 Pear
3 Pear
3 Berry
4 Pear
4 Berry
4 Berry
4 Berry
;
run;

proc sort data=exercise out=want nouniquekey;
by patient fruit;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Apr 2022 13:55:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810646#M319680</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-04-29T13:55:37Z</dc:date>
    </item>
    <item>
      <title>Re: Keep lines where the values are the same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810648#M319681</link>
      <description>Very nice. Didn't know that option existed until now.</description>
      <pubDate>Fri, 29 Apr 2022 13:59:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810648#M319681</guid>
      <dc:creator>average_joe</dc:creator>
      <dc:date>2022-04-29T13:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: Keep lines where the values are the same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810659#M319688</link>
      <description>&lt;P&gt;If you have a limited number of values&amp;nbsp;for the variable FRUIT, then consider using a SET statement with one argument per fruit (see the "WHERE=" parameters).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is, the SET statement will read a collection of subsets (one per fruit).&amp;nbsp; When used with a BY statement, each subset will be processed as a consecutive sequence of obsrvatinos (i.e. all the "Apple", then all the "Berr", etc.) which provides an easy way to detect the presence of more than one obs for a given PATIENT/FRUIT combination:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data exercise;
input patient fruit$;
cards;
1 Apple
1 Pear
1 Apple
1 Berry
1 Berry
2 Apple
2 Pear
3 Pear
3 Pear
3 Berry
4 Pear
4 Berry
4 Berry
4 Berry
run;


data want;
  set exercise (where=(fruit='Apple'))
      exercise (where=(fruit='Pear'))
      exercise (where=(fruit='Berry')) ;
  by patient fruit;
  if not (first.fruit=1 and last.fruit=1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If first.fruit=1 and last.fruit=1 simultaneously then the record-in-hand is the only one for that patient/fruit, and is not wanted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you have too many distinct FRUIT values, then another approach is needed - one where you can dynamically get the frequency of each fruit.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Apr 2022 14:21:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810659#M319688</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-29T14:21:55Z</dc:date>
    </item>
    <item>
      <title>Re: Keep lines where the values are the same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810662#M319690</link>
      <description>This approach is not feasible for me, as I have thousend of distinct fruit variables.&lt;BR /&gt;Still thank you for your option!</description>
      <pubDate>Fri, 29 Apr 2022 14:25:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810662#M319690</guid>
      <dc:creator>Nina4</dc:creator>
      <dc:date>2022-04-29T14:25:52Z</dc:date>
    </item>
    <item>
      <title>Re: Keep lines where the values are the same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810691#M319709</link>
      <description>&lt;P&gt;For what it's worth-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data exercise ;
	input patient fruit$ ;
	cards;
1 Apple
1 Pear
1 Apple
1 Berry
1 Berry
2 Apple
2 Pear
3 Pear
3 Pear
3 Berry
4 Pear
4 Berry
4 Berry
4 Berry
;
run ;

proc sql ;
	create table want as
		select a.*
			from exercise a 
				inner join 
					(select patient, fruit, count(*) as c from exercise group by patient, fruit) b
						on a.patient=b.patient and a.fruit=b.fruit and c &amp;gt; 1 ;
quit ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Apr 2022 16:03:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810691#M319709</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2022-04-29T16:03:46Z</dc:date>
    </item>
    <item>
      <title>Re: Keep lines where the values are the same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810727#M319730</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data exercise ;
	input patient fruit$ ;
	cards;
1 Apple
1 Pear
1 Apple
1 Berry
1 Berry
2 Apple
2 Pear
3 Pear
3 Pear
3 Berry
4 Pear
4 Berry
4 Berry
4 Berry
;
run ;


data want ;
  if _n_ = 1 then do ;
    dcl hash h ( ) ;
	h.definekey ('fruit') ;
    h.definedata ('_iorc_') ;
	h.definedone ( ) ;
  end ;
  do _n_ = 1 by 1 until (last.patient) ;
    set exercise ;
	by patient ;
	if h.find( ) = 0 then _iorc_ = sum (_iorc_ , 1) ;
	else _iorc_ = 1 ;
    h.replace( ) ;
  end ;
  do _n_ = 1 to _n_ ;
    set exercise ;
	h.find( ) ;
	if _iorc_ &amp;gt; 1 then output ;
  end ;
  h.clear( ) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Apr 2022 18:57:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-lines-where-the-values-are-the-same/m-p/810727#M319730</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2022-04-29T18:57:32Z</dc:date>
    </item>
  </channel>
</rss>

