Search code examples
group-bysassas-macro

In SAS EG4 i have to find Top 10 customer name within top 10 country name by region


below is the sample data

Data read; input region$ Country$ Customername$ Amount; dataline; asia china xx 78 asia pak xx 89 africa tunisia xx contd ; run; I want top 10 customer of top 10 country by region. Can anyone guide or help

i tried Proc tabulate and proc means but not giving results. I cannot show the result as it is office laptop


Solution

  • You didn't show the Proc MEANS code you tried.

    Try using Proc MEANS with some follow up SORT and DATA steps to get the results you want.

    Example:

    data have;
      call streaminit(20230417);
    
      do region = 1 to 20;
        do customer = 1 to 100+rand('integer', 25);
          customerId + 1;
          amount = rand('integer', 1, 2500);
          output;
        end;
      end;
      keep region customerid amount;
    run;
    
    proc means noprint data=have;
      class region customerId;
      var amount;
      types region region*customerId;
      output out=summary sum=totalamount;
    run;
    
    proc sort data=summary;
      by region descending totalamount;
    
    data top10region;
      set summary;
      where _type_ = 2;
      if _n_ > 10 then stop;
    run;
    
    data top10c_in_top10r;
      merge top10region(rename=totalamount=regiontotal) summary(where=(_type_=3));
      by region;
    
      if first.region then regionCount + 1;
      if regionCount > 10 then stop;
    
      if first.region 
        then counter = 1;
        else counter + 1;
    
      if counter <= 10 then output;
    
      keep region customerId regionTotal regionTotal totalAmount;
      rename totalAmount=customerTotal;
    run;
    

    enter image description here