sqlsasproc-sqlenterprise-guide4gl

How to count distribution in numbers and percent of binary column in table?


i have table in SAS Enterprise Guide like below:

Below table DOES NOT have duplicates in ID column.

ID  | TARGET 
----|--------
123 | 0
456 | 0 
777 | 1 
889 | 0 
122 | 1

And I would like to aggregate above table with sample data (original table has many more data) to have something like below:

How can I do that in SAS Enterprise Guide in normal SAS or in PROC SQL ?


Solution

  • PROC FREQ gives you the data but not in the desired format.

    proc freq data=have;
    table target /out=want outpct;
    run;
    

    If you really want that format, SQL is probably the easiest though the most manual. If you have missing values this probably needs to be adjusted.

    proc sql;
    create table want as
    select sum(target=1) as q_1,
           sum(taget=0) as q_0, 
           mean(target) as p_1 format=percent12.1,
           1-mean(target) as p_0 format=percent12.1
    from have;
    quit;