Search code examples
countsasdistinctfrequency

In SAS, how do get distinct counts of a variable that has multiple observations for each individual?


A simple question, but I have three variables in my dataset: ID, ICDCode, and a Visit Date. There are multiple occurrences of each ICDCode per person(ID). How do I obtain a total, distinct count of icdcodes for the entire dataset, without counting a certain ICDCode twice for an individual? For example, I want to say that there are 100 cases of Heart Disease in my dataset (without counting heart disease 10 times for the same person).
Below is code I have tried:

    proc freq data= cases;
    table ICDCode;
    run;

    proc sql;
    select ICDCode,
    count(*) as Frequency
    from cases
    group by ID;
    quit;
    

Solution

  • How about simply: (Given that 429.9 represent heart disease)

    data cases;
    input ID ICDCode;
    datalines;
    1 429.9
    1 429.9
    1 2    
    1 3    
    3 429.9
    3 429.9
    3 3    
    2 1    
    2 2    
    ;
    
    proc sql;
       select count(distinct ID) as n
       from cases
       where ICDCode = 429.9;
    run;