<?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 do I choose one of the two duplicates by ranking? I need an automated code. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738546#M230401</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/363831"&gt;@Pili1100&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To perform selections like this you can apply BY-group processing in a DATA step (using &lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;first&lt;/STRONG&gt;.&lt;EM&gt;variable&lt;/EM&gt;&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;last&lt;/STRONG&gt;.&lt;EM&gt;variable&lt;/EM&gt;&lt;/FONT&gt;) to an appropriately sorted version of the input dataset. The ORDER BY clause of PROC SQL is particularly suitable for implementing hierarchical sort criteria. The "sorted version" of the input dataset can be a &lt;EM&gt;view&lt;/EM&gt; (to save disk space). Here's an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create view _tmp as
select * from have1
where .z&amp;lt;answer_value&amp;lt;7 &amp;amp; criteria_met='Yes'
order by id, answer_month, missing(answer_string), answer_date desc;
quit;

data want;
set _tmp;
by id answer_month;
if first.answer_month;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that I interpreted "1. When Answer_value &amp;lt; 7 : Criteria_met = 'Yes'" as "&lt;FONT face="courier new,courier"&gt;answer_value&lt;/FONT&gt; has a &lt;EM&gt;non-missing&lt;/EM&gt; value &amp;lt; 7 &lt;EM&gt;and&lt;/EM&gt;&amp;nbsp;&lt;FONT face="courier new,courier"&gt;criteria_met = 'Yes'&lt;/FONT&gt;" (in view of obs. 14 of HAVE1, which has &lt;FONT face="courier new,courier"&gt;answer_value=6&lt;/FONT&gt;, yet &lt;FONT face="courier new,courier"&gt;criteria_met = 'No'&lt;/FONT&gt;). Feel free to modify the WHERE clause if this is not correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 03 May 2021 11:13:33 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2021-05-03T11:13:33Z</dc:date>
    <item>
      <title>How do I choose one of the two duplicates by ranking? I need an automated code.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738516#M230384</link>
      <description>&lt;P&gt;My customer dataset contains duplicates: Answer_date differs, but Answer_month is the same.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;I need to choose one customer rows according to the following&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp;When Answer_value &amp;lt; 7 : &lt;EM&gt;Criteria_met&lt;/EM&gt; = 'Yes'&lt;BR /&gt;2. If both rows have &lt;EM&gt;Criteria_met&lt;/EM&gt; = 'Yes' &lt;BR /&gt;Then choose then one with &lt;EM&gt;Answer_string&lt;/EM&gt; NE ' '&lt;/P&gt;
&lt;P&gt;3. Both rows have&lt;EM&gt; Criteria_met&lt;/EM&gt; = 'Yes' and &lt;EM&gt;Answer_string&lt;/EM&gt; NE ' ' &lt;BR /&gt;Then choose then one with max date.&lt;/P&gt;
&lt;P&gt;I need an&amp;nbsp;automated code&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dataset have1 contains all customer rows: contains duplicates.&lt;/P&gt;
&lt;P&gt;Dateset have2 contains all customer rows with&amp;nbsp;&lt;EM&gt;Criteria_met&lt;/EM&gt; = 'Yes':&amp;nbsp;contains duplicates&lt;/P&gt;
&lt;P&gt;Dataset want contains the rows I want according to my ranking. However I need an&amp;nbsp;automated code&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.Have1;

RETAIN
ID Name Answer_Date Answer_month answer_value Answer_string criteria_met
;
	LENGTH Name Answer_string $50;
	FORMAT Answer_Date YYMMDD10.;
INFILE DATALINES DELIMITER =','; 
INPUT ID $ Name $ Answer_Date  YYMMDD10. Answer_month $ answer_value Answer_string $ criteria_met $ ;
CARDS; 
1,Jessica Beatrice Fletcher,2021-04-15,2021M04,10,Very good,No
1,Jessica Beatrice Fletcher,2021-04-22,2021M04,6,OK,Yes
2,Seth Hazlett,2021-04-15,2021M04,5,Bad,Yes
2,Seth Hazlett,2021-04-22,2021M04,4, ,Yes
3,Mort Metzger,2021-04-15,2021M04,7,OK,No
3,Mort Metzger,2021-04-22,2021M04,7,I'm happy,No
4,Eve Simpson,2021-04-15,2021M04,4,No good,Yes
4,Eve Simpson,2021-04-22,2021M04,2,Not working,Yes
5,Lauretta Speigel,2021-04-15,2021M04,8, ,No
5,Lauretta Speigel,2021-04-22,2021M04,8, ,No
6,Amos Tupper,2021-04-15,2021M04,10,Happy,No
6,Amos Tupper,2021-04-22,2021M04,10,Very Good,No
7,Dennis Stanton,2021-04-15,2021M04,3,Needs improvement,Yes
7,Dennis Stanton,2021-04-22,2021M04,6,Still needs improvement ,No
8,Michael Hagarty,2021-04-15,2021M04,2, ,Yes
8,Michael Hagarty,2021-04-22,2021M04,9, ,No
;

RUN;
DATA have2;
SET have1;
	WHERE criteria_met = 'Yes';
RUN;
DATA want;
SET have2;
IF ID IN ('1', '7', '8') 		  		 THEN OUTPUT;
IF ID = 2 AND Answer_string NE '' 		 THEN OUTPUT;
IF ID = 4 AND Answer_date = '22APR2021'd THEN OUTPUT;
RUN;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 08:41:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738516#M230384</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2021-05-03T08:41:41Z</dc:date>
    </item>
    <item>
      <title>Re: How do I choose one of the two duplicates by ranking? I need an automated code.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738535#M230392</link>
      <description>&lt;P&gt;Is the number of obs for each id always two?&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 10:45:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738535#M230392</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-05-03T10:45:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I choose one of the two duplicates by ranking? I need an automated code.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738537#M230393</link>
      <description>&lt;P&gt;It can probably vary but it's often two rows per customer and month&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 10:47:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738537#M230393</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2021-05-03T10:47:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do I choose one of the two duplicates by ranking? I need an automated code.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738538#M230394</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/363831"&gt;@Pili1100&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;It can probably vary but it's often two rows per customer and month&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then you should modify have1 to contain such cases.&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 10:49:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738538#M230394</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-05-03T10:49:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do I choose one of the two duplicates by ranking? I need an automated code.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738545#M230400</link>
      <description>&lt;LI-SPOILER&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/363831"&gt;@Pili1100&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;My customer dataset contains duplicates: Answer_date differs, but Answer_month is the same.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;I need to choose one customer rows according to the following&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp;When Answer_value &amp;lt; 7 : &lt;EM&gt;Criteria_met&lt;/EM&gt; = 'Yes'&lt;BR /&gt;2. If both rows have &lt;EM&gt;Criteria_met&lt;/EM&gt; = 'Yes' &lt;BR /&gt;Then choose then one with &lt;EM&gt;Answer_string&lt;/EM&gt; NE ' '&lt;/P&gt;
&lt;P&gt;3. Both rows have&lt;EM&gt; Criteria_met&lt;/EM&gt; = 'Yes' and &lt;EM&gt;Answer_string&lt;/EM&gt; NE ' ' &lt;BR /&gt;Then choose then one with max date.&lt;/P&gt;
&lt;P&gt;I need an&amp;nbsp;automated code&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dataset have1 contains all customer rows: contains duplicates.&lt;/P&gt;
&lt;P&gt;Dateset have2 contains all customer rows with&amp;nbsp;&lt;EM&gt;Criteria_met&lt;/EM&gt; = 'Yes':&amp;nbsp;contains duplicates&lt;/P&gt;
&lt;P&gt;Dataset want contains the rows I want according to my ranking. However I need an&amp;nbsp;automated code&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.Have1;

RETAIN
ID Name Answer_Date Answer_month answer_value Answer_string criteria_met
;
	LENGTH Name Answer_string $50;
	FORMAT Answer_Date YYMMDD10.;
INFILE DATALINES DELIMITER =','; 
INPUT ID $ Name $ Answer_Date  YYMMDD10. Answer_month $ answer_value Answer_string $ criteria_met $ ;
CARDS; 
1,Jessica Beatrice Fletcher,2021-04-15,2021M04,10,Very good,No
1,Jessica Beatrice Fletcher,2021-04-22,2021M04,6,OK,Yes
2,Seth Hazlett,2021-04-15,2021M04,5,Bad,Yes
2,Seth Hazlett,2021-04-22,2021M04,4, ,Yes
3,Mort Metzger,2021-04-15,2021M04,7,OK,No
3,Mort Metzger,2021-04-22,2021M04,7,I'm happy,No
4,Eve Simpson,2021-04-15,2021M04,4,No good,Yes
4,Eve Simpson,2021-04-22,2021M04,2,Not working,Yes
5,Lauretta Speigel,2021-04-15,2021M04,8, ,No
5,Lauretta Speigel,2021-04-22,2021M04,8, ,No
6,Amos Tupper,2021-04-15,2021M04,10,Happy,No
6,Amos Tupper,2021-04-22,2021M04,10,Very Good,No
7,Dennis Stanton,2021-04-15,2021M04,3,Needs improvement,Yes
7,Dennis Stanton,2021-04-22,2021M04,6,Still needs improvement ,No
8,Michael Hagarty,2021-04-15,2021M04,2, ,Yes
8,Michael Hagarty,2021-04-22,2021M04,9, ,No
;

RUN;
DATA have2;
SET have1;
	WHERE criteria_met = 'Yes';
RUN;
DATA want;
SET have2;
IF ID IN ('1', '7', '8') 		  		 THEN OUTPUT;
IF ID = 2 AND Answer_string NE '' 		 THEN OUTPUT;
IF ID = 4 AND Answer_date = '22APR2021'd THEN OUTPUT;
RUN;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;This could be a solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have1 out=sorted;
   by id descending answer_date;
   where criteria_met = 'Yes';
run;

data want;
   set sorted;
   by id;
   
   retain id_kept;
   drop id_kept;
   
   if first.id then id_kept = 0;
      
   if first.id and last.id then do;
       /* keep observation if it is the only one for an id */
      output;
   end;
   else do;
      /* the first obs having answer_string ^= " " will be kept */
      if not missing(Answer_string) and id_kept = 0 then do;
         id_kept = 1;
         output;
      end;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 May 2021 11:12:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738545#M230400</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-05-03T11:12:11Z</dc:date>
    </item>
    <item>
      <title>Re: How do I choose one of the two duplicates by ranking? I need an automated code.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738546#M230401</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/363831"&gt;@Pili1100&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To perform selections like this you can apply BY-group processing in a DATA step (using &lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;first&lt;/STRONG&gt;.&lt;EM&gt;variable&lt;/EM&gt;&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;last&lt;/STRONG&gt;.&lt;EM&gt;variable&lt;/EM&gt;&lt;/FONT&gt;) to an appropriately sorted version of the input dataset. The ORDER BY clause of PROC SQL is particularly suitable for implementing hierarchical sort criteria. The "sorted version" of the input dataset can be a &lt;EM&gt;view&lt;/EM&gt; (to save disk space). Here's an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create view _tmp as
select * from have1
where .z&amp;lt;answer_value&amp;lt;7 &amp;amp; criteria_met='Yes'
order by id, answer_month, missing(answer_string), answer_date desc;
quit;

data want;
set _tmp;
by id answer_month;
if first.answer_month;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that I interpreted "1. When Answer_value &amp;lt; 7 : Criteria_met = 'Yes'" as "&lt;FONT face="courier new,courier"&gt;answer_value&lt;/FONT&gt; has a &lt;EM&gt;non-missing&lt;/EM&gt; value &amp;lt; 7 &lt;EM&gt;and&lt;/EM&gt;&amp;nbsp;&lt;FONT face="courier new,courier"&gt;criteria_met = 'Yes'&lt;/FONT&gt;" (in view of obs. 14 of HAVE1, which has &lt;FONT face="courier new,courier"&gt;answer_value=6&lt;/FONT&gt;, yet &lt;FONT face="courier new,courier"&gt;criteria_met = 'No'&lt;/FONT&gt;). Feel free to modify the WHERE clause if this is not correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 11:13:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738546#M230401</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-05-03T11:13:33Z</dc:date>
    </item>
    <item>
      <title>Re: How do I choose one of the two duplicates by ranking? I need an automated code.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738603#M230430</link>
      <description>&lt;P&gt;Why is your desired output for ID=4&amp;nbsp; the second observation (4/22/2021), when the first observation (4/15/2021) has an answer_value&amp;lt;7?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does satisfying rule 1&amp;nbsp; (answer&amp;lt;7 and criteria_met='Yes') not alleviate the need to assess rules 2 and 3?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can see that rule 3 (the latest answer_string^=' ') subsumes rule 2 (answer_string^=' '),.but please clarify the status of rule 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 14:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-choose-one-of-the-two-duplicates-by-ranking-I-need-an/m-p/738603#M230430</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-05-03T14:29:37Z</dc:date>
    </item>
  </channel>
</rss>

