<?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 split dataset with condition in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288845#M19715</link>
    <description>Thank KurtBremser a lot, but I'd like to have the result which must be satisfy 2 conditions as below:&lt;BR /&gt;1/ The total client in each group is equally.&lt;BR /&gt;2/ The total loan amount of each group is equally.&lt;BR /&gt;As your advice, the result is total loan amount of each group will be less than or equal average loan amount.</description>
    <pubDate>Tue, 02 Aug 2016 07:27:50 GMT</pubDate>
    <dc:creator>Hue</dc:creator>
    <dc:date>2016-08-02T07:27:50Z</dc:date>
    <item>
      <title>How to split dataset with condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288831#M19707</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I have a dataset,&amp;nbsp;including 1000 clients corresponding 1000 loans differently. I'd like to split 1000 clients to 50 groups with condition is total loans amount and the number of clients&amp;nbsp;of each group equally?&lt;/P&gt;&lt;P&gt;Could you help me solve this calculation by SAS EG?&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Cust_ID&lt;/TD&gt;&lt;TD&gt;Loan_amt ($)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;D&lt;/TD&gt;&lt;TD&gt;58&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks alot,&lt;/P&gt;</description>
      <pubDate>Tue, 02 Aug 2016 06:44:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288831#M19707</guid>
      <dc:creator>Hue</dc:creator>
      <dc:date>2016-08-02T06:44:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to split dataset with condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288834#M19709</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let groups = 50;

proc sql noprint;
select sum(loan_amt) into :sum_loan from have;
run;

data want;
set have;
retain group group_amt;
if _n_ = 1
then do;
  group = 1;
  group_amount = 0;
end;
group_amount + loan_amt;
if group_amount &amp;gt; &amp;amp;sum_loan / &amp;amp;groups
then do;
  group + 1;
  group_amount = 0;
end;
drop group_amt;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Aug 2016 06:50:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288834#M19709</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-02T06:50:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to split dataset with condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288845#M19715</link>
      <description>Thank KurtBremser a lot, but I'd like to have the result which must be satisfy 2 conditions as below:&lt;BR /&gt;1/ The total client in each group is equally.&lt;BR /&gt;2/ The total loan amount of each group is equally.&lt;BR /&gt;As your advice, the result is total loan amount of each group will be less than or equal average loan amount.</description>
      <pubDate>Tue, 02 Aug 2016 07:27:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288845#M19715</guid>
      <dc:creator>Hue</dc:creator>
      <dc:date>2016-08-02T07:27:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to split dataset with condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288850#M19718</link>
      <description>&lt;PRE&gt;
No. You can't do that. You only can say :
1/ The total client in each group is equally.
2/ The total loan amount of each group is ALMOST equally.

But that will lead you to a SAS/OR problem. Suggest you post it at OR forum  , @Prat is there.



Here is my GA code. Maybe you want OR code if you have SAS/OR .

proc iml;
use sashelp.class;
read all var {age} ;
close ;

start function(x) global(age,group);
if ncol(x) ^= ncol(unique(x)) then obj=9999;
else do;
 y=age[x,];
 obj=range(shape(y,group,0,0)[,+]);
end;
 return (obj);
finish;

start switch_mut(s) global(nswitches,nrow);
if ncol(s) ^= ncol(unique(s)) then s=ranperm(nrow,1);
else do;
  n = ncol(s);
  do i = 1 to nswitches;
   k1 = int(uniform(1234)*n) + 1;
   k2 = int(uniform(1234)*n) + 1;
   if k1^=k2 then do;
    temp=s[k2];
    s[k2] = s[k1];
    s[k1] = temp;
   end;
  end;
end;
finish;

start uniform_cross(child1, child2, parent1, parent2) global(nrow);

child1 = parent1;
child2 = parent2;
do i = 1 to ncol(parent1);
  r = uniform(1234);
  if r&amp;lt;=0.5 then do;
   child1[i] = parent2[i];
   child2[i] = parent1[i];
  end;
end;

if ncol(child1) ^= ncol(unique(child1)) then child1=ranperm(nrow,1);
 else if ncol(child2) ^= ncol(unique(child2)) then child2=ranperm(nrow,1);

finish;



nrow=nrow(age);
group=4;
nswitches = 3;
encoding=j(2,nrow,1);
encoding[2,]=nrow;    

id=gasetup(2,nrow,1234);
call gasetobj(id,0,"function");
call gasetcro(id,0.95,0,"uniform_cross");
call gasetmut(id,0.95,0,"switch_mut");
call gasetsel(id,100,1,1);
call gainit(id,1000,encoding);


niter = 10000;
summary = j(niter,2);
mattrib summary [c = {"Min Value", "Avg Value"} l=""];
do i = 1 to niter;
 call garegen(id);
 call gagetval(value, id);
 summary[i,1] = value[1];
 summary[i,2] = value[:];
end;
call gagetmem(mem, value, id, 1);


Memebers=shape(age[mem,],group,0,0);
print "Members:" Memebers[l=""],
      "Group sum:" (Memebers[,+]),
      "Min Value: " value[l = ""] ;
call gaend(id);
quit;

&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Aug 2016 07:36:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288850#M19718</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-02T07:36:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to split dataset with condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288853#M19719</link>
      <description>&lt;P&gt;Well, I overlooked that.&lt;/P&gt;
&lt;P&gt;On second thought, the solution could even be easier:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by descending loan_amt;
run;

data want;
set have;
retain group 0 direction 1;
if direction
then do;
  group + 1;
  if group &amp;gt; &amp;amp;groups
  then do;
    group = &amp;amp;groups;
    direction = 0;
  end;
end;
else do;
  group + (-1);
  if group &amp;lt; 1
  then do;
    group = 1;
    direction = 1;
  end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This should give you an almost even distribution.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Aug 2016 07:52:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288853#M19719</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-02T07:52:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to split dataset with condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288865#M19720</link>
      <description>&lt;P&gt;Thank you for your supporting, KurtBremer.&lt;/P&gt;&lt;P&gt;With your way,&amp;nbsp;I&amp;nbsp;see that the number clients of each group will be equally,&lt;/P&gt;&lt;P&gt;However the total&amp;nbsp;loan amount of each group will&amp;nbsp;be not&amp;nbsp;almost equally. You know that, the gap (loan amount) of each loans is perhap a bit&amp;nbsp;or so much, so if we sort by loan amount and use directory, it'll be not optimal.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Aug 2016 08:19:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288865#M19720</guid>
      <dc:creator>Hue</dc:creator>
      <dc:date>2016-08-02T08:19:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to split dataset with condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288867#M19721</link>
      <description>Thanks Ksharp alot,&lt;BR /&gt;Actually, it's difficult to me understand your code.</description>
      <pubDate>Tue, 02 Aug 2016 08:23:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288867#M19721</guid>
      <dc:creator>Hue</dc:creator>
      <dc:date>2016-08-02T08:23:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to split dataset with condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288883#M19726</link>
      <description>&lt;PRE&gt;
My code is IML code using Genetic Algorithm , which you need SAS/IML or SAS University Edition to run it.
If you have SAS/OR license , Here are some URL you might be interesting in .


https://communities.sas.com/t5/Mathematical-Optimization/Minimization-challenge/m-p/259055#U259055
https://communities.sas.com/t5/Mathematical-Optimization/OR-Challenge/m-p/268967#U268967
https://communities.sas.com/t5/Mathematical-Optimization/OR-Challenge-again/m-p/274861#U274861
&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Aug 2016 09:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-split-dataset-with-condition/m-p/288883#M19726</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-02T09:54:59Z</dc:date>
    </item>
  </channel>
</rss>

