0

I have a customer_no and class. I would like to calculate ratio from the same column for each customer.

ratio= (number of secured/ total number classes)

customer_no        class  
1                  unsecured   
1                  secured   
1                  secured   
2                  unsecured   
2                  secured   
3                  secured   
3                  unsecured   
3                  secured   
3                  unsecured   

The output sample will be

customer_no   ratio   
1              0.666 
2              0.50  
3              0.50 
.       
.   
.  
20000      
6
  • Do not post sample data as images please, always post it as formatted text.
    – Ilyes
    Oct 8, 2017 at 8:18
  • Sorry, i will modify that Oct 8, 2017 at 8:22
  • 2 / 3 = 0.666666 ! ratio of 1 how become 0.75?
    – shA.t
    Oct 8, 2017 at 8:36
  • I put the answer as sample. I have modify the question. The correct answer is 0.666 Oct 8, 2017 at 9:06
  • What code did you use that got 0.75 as the ratio? Are you counting the number of observations or the number of distinct values that occur? Or do you need to count the number of distinct values that could occur? If it is that last then you need a source for the number of possible values as some values might not appear in your actual data.
    – Tom
    Oct 8, 2017 at 15:12

2 Answers 2

0

You can calculate ratio in a query like this:

select customer_no,
    (count(case when class = 'secured' then 1 end)     -- count if class is secured
    +0.0)                                              -- add by a double value to cast values to double to get scales
    / count(*) ratio                                   -- count of all class
from yourTable
group by customer_no;

SQL Fiddle Demo

0
proc sql;
   create table want as  
   select customer_no   
   ,sum(case when class='secured' then 1 else 0 end)/count(customer_no) as ratio   
   from  WORK.ENQUIRY_30   
   group by customer_no;   
quit;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.