title: SAS Code - Trying to understand how to build GINI Index
body: <p>I am participating in a data science competition and my final predictions would be measured via a GINI Index. It is a regression problem. I have the source code for the calculation in SAS but I dont know SAS and am not able to understand what is going on. </p>
  
  <p>I want to build the same in Python. Any help would be appreciated. If someone knows Python code for this, it would help a lot.</p>
  
  <pre><code>    *define GINI;       
      %macro  gini(input=, output=, y=, py=, filter=, split_ind = );
      data indsn;
          set &amp;input.;
          _random=ranuni (123456789);
          w=1;
          if &amp;split_ind.="&amp;filter.";
      run;
  
      proc sort data=indsn;by &amp;py _random;run;
          /*accumulate w to calculate Gini    */
          data test;
              set indsn;
              if _N_ = 1 then do;
                  cumm_w0=0;
              end;
              retain cumm_w0
              ;
              cumm_w0=cumm_w0+w;
          run;
  
          /*calcualate Gini */
          proc sql noprint;
              create table &amp;output
              as
              select 1-2/(sum(w)-1)*(sum(w)-sum(&amp;y.*cumm_w0*w)/sum(&amp;y.*w)) as gini
              from test;
          quit;
  
  
          proc print data=&amp;output;
              title " GINI on &amp;filter.";run;
      %mend;
  </code></pre>