<?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: By Group - dominant occurances in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/By-Group-dominant-occurances/m-p/340246#M63225</link>
    <description>&lt;P&gt;The appearance of your output suggests you want all the transactions for a given id, that come from the most frequent COUNTRY_FROM/COUNTRY_TO pair.&amp;nbsp; But it also appears, though not stated, that&amp;nbsp;country_from is constant for each id.&amp;nbsp; If so, then&amp;nbsp;you really&amp;nbsp;want a list of transactions that have the most frequent COUNTRY_TO.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And in the case of multiple country_to's with the same frequency choose the country with the latest transaction date.&amp;nbsp; You provide no rules for when the tied countries also have ties in latest transaction date, so we'll ignore that for now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From the look of your datasets, it seems that&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;HAVE is sorted by ID.&lt;/LI&gt;
&lt;LI&gt;Within ID, it is grouped (but not sorted) by country_to.&lt;/LI&gt;
&lt;LI&gt;Within country_to, your data is NOT sorted by date,&amp;nbsp;but you wish to preserve original order&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards;
informat trxn_date ddmmyy10.;
format trxn_date yymmddn8.;
input ID Country_from $ country_to $ trxn_date amount ;
cards;
1 US IND 01-03-2017 387
1 US IND 02-03-2017 388
1 US IND 03-03-2017 514
1 US UK 04-03-2017 889
1 US UK 05-03-2017 987
1 US UK 06-03-2017 690
1 US UK 07-03-2017 158
1 US AUS 08-03-2017 341
1 US AUS 09-03-2017 247
1 US CAN 10-03-2017 699
1 US CAN 11-03-2017 623
2 UK IND 04-03-2017 627
2 UK IND 05-03-2017 822
2 UK IND 06-03-2017 724
2 UK US 07-03-2017 231
2 UK US 08-03-2017 437
2 UK US 04-03-2017 193
2 UK AUS 05-03-2017 922
2 UK AUS 06-03-2017 750
2 UK AUS 07-03-2017 123
2 UK CAN 08-03-2017 920
2 UK CAN 08-03-2017 587
;;
run;


data want (drop=_:);

  do until (last.id);
    do _nt=1 by 1 until (last.country_to);
      set have;
      by id country_to notsorted;
	  if _nt=1 then _to_maxdate=trxn_date;
	  else _to_maxdate=max(_to_maxdate,trxn_date);
    end;

    if (_nt&amp;gt;_maxnt) or (_nt=_maxnt and _to_maxdate&amp;gt;_id_maxdate) then do;
      _maxnt=_nt;
      _max_to=country_to;
      _id_maxdate=_to_maxdate;
    end;
  end;

  do until (last.id);
    do until(last.country_to);
      set have;
      by id country_to notsorted;
      if country_to=_max_to then output;
    end;
  end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;This program has two DO .... until(last.id) loops.&lt;/LI&gt;
&lt;OL&gt;
&lt;LI&gt;EAch has a "set have; by id country_to notsorted;"&amp;nbsp;pair of statements telling SAS to expect the data to be grouped by id/country_to, but not&amp;nbsp;necessarily in ascending order.&amp;nbsp;&amp;nbsp; If that is not a secure assumption, then presort your data by id/country_to.&lt;/LI&gt;
&lt;LI&gt;The first loop reads all the records for and ID, one country_to at a time.&amp;nbsp; It identifies the country_to (variable _MAX_TO) that satisfies the criteria (highest _MAXNT, etc.)&lt;/LI&gt;
&lt;LI&gt;The second loop re-reads the very same records and output those records whose country_to matches the _max_to variable.&lt;/LI&gt;
&lt;/OL&gt;
&lt;/OL&gt;</description>
    <pubDate>Sun, 12 Mar 2017 18:05:27 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2017-03-12T18:05:27Z</dc:date>
    <item>
      <title>By Group - dominant occurances</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/By-Group-dominant-occurances/m-p/340223#M63220</link>
      <description>&lt;P&gt;Hi i have a situation where i need to select only the transactions from each ID where the no.of transactions made to a perticular country is more if ties then latest transaction date decides.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;infile cards;&lt;BR /&gt;informat trxn_date ddmmyy10.;&lt;BR /&gt;format trxn_date ddmmyy10.;&lt;BR /&gt;input ID Country_from $ country_to $ trxn_date amount ;&lt;BR /&gt;cards;&lt;BR /&gt;1 US IND 01-03-2017 387&lt;BR /&gt;1 US IND 02-03-2017 388&lt;BR /&gt;1 US IND 03-03-2017 514&lt;BR /&gt;1 US UK 04-03-2017 889&lt;BR /&gt;1 US UK 05-03-2017 987&lt;BR /&gt;1 US UK 06-03-2017 690&lt;BR /&gt;1 US UK 07-03-2017 158&lt;BR /&gt;1 US AUS 08-03-2017 341&lt;BR /&gt;1 US AUS 09-03-2017 247&lt;BR /&gt;1 US CAN 10-03-2017 699&lt;BR /&gt;1 US CAN 11-03-2017 623&lt;BR /&gt;2 UK IND 04-03-2017 627&lt;BR /&gt;2 UK IND 05-03-2017 822&lt;BR /&gt;2 UK IND 06-03-2017 724&lt;BR /&gt;2 UK US 07-03-2017 231&lt;BR /&gt;2 UK US 08-03-2017 437&lt;BR /&gt;2 UK US 04-03-2017 193&lt;BR /&gt;2 UK AUS 05-03-2017 922&lt;BR /&gt;2 UK AUS 06-03-2017 750&lt;BR /&gt;2 UK AUS 07-03-2017 123&lt;BR /&gt;2 UK CAN 08-03-2017 920&lt;BR /&gt;2 UK CAN 08-03-2017 587&lt;BR /&gt;;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expected Output :&lt;/P&gt;&lt;P&gt;1 US UK 04-03-2017 889&lt;BR /&gt;1 US UK 05-03-2017 987&lt;BR /&gt;1 US UK 06-03-2017 690&lt;BR /&gt;1 US UK 07-03-2017 158&lt;/P&gt;&lt;P&gt;2 UK US 07-03-2017 231&lt;BR /&gt;2 UK US 08-03-2017 437&lt;BR /&gt;2 UK US 04-03-2017 193&lt;/P&gt;</description>
      <pubDate>Sun, 12 Mar 2017 08:41:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/By-Group-dominant-occurances/m-p/340223#M63220</guid>
      <dc:creator>Reddi</dc:creator>
      <dc:date>2017-03-12T08:41:59Z</dc:date>
    </item>
    <item>
      <title>Re: By Group - dominant occurances</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/By-Group-dominant-occurances/m-p/340225#M63221</link>
      <description>&lt;PRE&gt;
Assuming I understood what you mean.




 
data have;
infile cards;
informat trxn_date ddmmyy10.;
format trxn_date ddmmyy10.;
input ID Country_from $ country_to $ trxn_date amount ;
cards;
1 US IND 01-03-2017 387
1 US IND 02-03-2017 388
1 US IND 03-03-2017 514
1 US UK 04-03-2017 889
1 US UK 05-03-2017 987
1 US UK 06-03-2017 690
1 US UK 07-03-2017 158
1 US AUS 08-03-2017 341
1 US AUS 09-03-2017 247
1 US CAN 10-03-2017 699
1 US CAN 11-03-2017 623
2 UK IND 04-03-2017 627
2 UK IND 05-03-2017 822
2 UK IND 06-03-2017 724
2 UK US 07-03-2017 231
2 UK US 08-03-2017 437
2 UK US 04-03-2017 193
2 UK AUS 05-03-2017 922
2 UK AUS 06-03-2017 750
2 UK AUS 07-03-2017 123
2 UK CAN 08-03-2017 920
2 UK CAN 08-03-2017 587
;;
run;
 data temp;
  set have;
  from=country_from;
  to=country_to;
  call sortc(from,to);
run;
proc sql;
select *
 from temp
  group by from,to
   having count(distinct id) ne 1;
quit;
&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Mar 2017 10:25:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/By-Group-dominant-occurances/m-p/340225#M63221</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-12T10:25:32Z</dc:date>
    </item>
    <item>
      <title>Re: By Group - dominant occurances</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/By-Group-dominant-occurances/m-p/340229#M63222</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the reply i think you got the list of countires where more than one ID's had the transactions. But my issue is i need to select a list of all the transactions made by each ID to a unique country and the unique country must be selected based on the no.of transactions(more) , if thereare more than one country having the maximum count then the latest transaction date would decide(country having the latest transaction).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope it make sense.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Mar 2017 11:02:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/By-Group-dominant-occurances/m-p/340229#M63222</guid>
      <dc:creator>Reddi</dc:creator>
      <dc:date>2017-03-12T11:02:21Z</dc:date>
    </item>
    <item>
      <title>Re: By Group - dominant occurances</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/By-Group-dominant-occurances/m-p/340241#M63224</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;you can try this&amp;nbsp; (I wish I could find something that is not as lengthy though)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have missing nway;
class ID Country_from  country_to;
var trxn_date ;
output out=temp (drop=_type_ rename=(_freq_=no_trans)) max(trxn_date)=max_date;
run;

proc sort data=temp ;
by id  descending no_trans descending  max_date;
run;

data temp (drop=max_date no_trans );
set temp ;
      by id  descending no_trans descending  max_date;
                  if first.id ;
run;

proc sort data=temp;
     by id Country_from  country_to  ;
run;
proc sort data=have;
    by id Country_from  country_to ;
run;

data have;
merge have (in=A) temp (in=b);
  by id Country_from  country_to ;
        if a and b;
run;

proc datasets lib=work nolist;
     delete temp;
quit;
proc print data=have noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Mar 2017 17:16:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/By-Group-dominant-occurances/m-p/340241#M63224</guid>
      <dc:creator>atzamis</dc:creator>
      <dc:date>2017-03-12T17:16:38Z</dc:date>
    </item>
    <item>
      <title>Re: By Group - dominant occurances</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/By-Group-dominant-occurances/m-p/340246#M63225</link>
      <description>&lt;P&gt;The appearance of your output suggests you want all the transactions for a given id, that come from the most frequent COUNTRY_FROM/COUNTRY_TO pair.&amp;nbsp; But it also appears, though not stated, that&amp;nbsp;country_from is constant for each id.&amp;nbsp; If so, then&amp;nbsp;you really&amp;nbsp;want a list of transactions that have the most frequent COUNTRY_TO.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And in the case of multiple country_to's with the same frequency choose the country with the latest transaction date.&amp;nbsp; You provide no rules for when the tied countries also have ties in latest transaction date, so we'll ignore that for now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From the look of your datasets, it seems that&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;HAVE is sorted by ID.&lt;/LI&gt;
&lt;LI&gt;Within ID, it is grouped (but not sorted) by country_to.&lt;/LI&gt;
&lt;LI&gt;Within country_to, your data is NOT sorted by date,&amp;nbsp;but you wish to preserve original order&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards;
informat trxn_date ddmmyy10.;
format trxn_date yymmddn8.;
input ID Country_from $ country_to $ trxn_date amount ;
cards;
1 US IND 01-03-2017 387
1 US IND 02-03-2017 388
1 US IND 03-03-2017 514
1 US UK 04-03-2017 889
1 US UK 05-03-2017 987
1 US UK 06-03-2017 690
1 US UK 07-03-2017 158
1 US AUS 08-03-2017 341
1 US AUS 09-03-2017 247
1 US CAN 10-03-2017 699
1 US CAN 11-03-2017 623
2 UK IND 04-03-2017 627
2 UK IND 05-03-2017 822
2 UK IND 06-03-2017 724
2 UK US 07-03-2017 231
2 UK US 08-03-2017 437
2 UK US 04-03-2017 193
2 UK AUS 05-03-2017 922
2 UK AUS 06-03-2017 750
2 UK AUS 07-03-2017 123
2 UK CAN 08-03-2017 920
2 UK CAN 08-03-2017 587
;;
run;


data want (drop=_:);

  do until (last.id);
    do _nt=1 by 1 until (last.country_to);
      set have;
      by id country_to notsorted;
	  if _nt=1 then _to_maxdate=trxn_date;
	  else _to_maxdate=max(_to_maxdate,trxn_date);
    end;

    if (_nt&amp;gt;_maxnt) or (_nt=_maxnt and _to_maxdate&amp;gt;_id_maxdate) then do;
      _maxnt=_nt;
      _max_to=country_to;
      _id_maxdate=_to_maxdate;
    end;
  end;

  do until (last.id);
    do until(last.country_to);
      set have;
      by id country_to notsorted;
      if country_to=_max_to then output;
    end;
  end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;This program has two DO .... until(last.id) loops.&lt;/LI&gt;
&lt;OL&gt;
&lt;LI&gt;EAch has a "set have; by id country_to notsorted;"&amp;nbsp;pair of statements telling SAS to expect the data to be grouped by id/country_to, but not&amp;nbsp;necessarily in ascending order.&amp;nbsp;&amp;nbsp; If that is not a secure assumption, then presort your data by id/country_to.&lt;/LI&gt;
&lt;LI&gt;The first loop reads all the records for and ID, one country_to at a time.&amp;nbsp; It identifies the country_to (variable _MAX_TO) that satisfies the criteria (highest _MAXNT, etc.)&lt;/LI&gt;
&lt;LI&gt;The second loop re-reads the very same records and output those records whose country_to matches the _max_to variable.&lt;/LI&gt;
&lt;/OL&gt;
&lt;/OL&gt;</description>
      <pubDate>Sun, 12 Mar 2017 18:05:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/By-Group-dominant-occurances/m-p/340246#M63225</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-03-12T18:05:27Z</dc:date>
    </item>
  </channel>
</rss>

