Search code examples
sasdatasetsas-macro

how to get high occurring variable from a table using SAS?


I have a table as below

level1      level2
furniture   chair
furniture   chair
furniture   table
food        pizza

I want to get unique level 1 variable and highest occurring level 2 variable for that variable. Output:

level1      level2
furniture   chair
food        pizza

Solution

    1. Summarize data to get counts, PROC FREQ.

    2. Sort into order to get the counts descending by level1.

    3. Use a data step and BY group processing to get the first observations for each level1.

      proc freq data=have order = freq;
      table level1*level2 / out=counts;
      run;
      
      proc sort data=counts;
      by level1 descending Count;
      run;
      
      data want;
      set counts;
      by level1;
      if first.level1;
      run;