Skip to content

Instantly share code, notes, and snippets.

@rpardee
Created May 27, 2014 16:13
Show Gist options
  • Save rpardee/5c6143fd81d8a40adb0b to your computer and use it in GitHub Desktop.
Save rpardee/5c6143fd81d8a40adb0b to your computer and use it in GitHub Desktop.
SAS macro to produce a FREQ of the top-N most frequently ocurring values of a variable.
%macro limited_freq(inset = s.car_adm_f, var = , rowlim = 10) ;
proc sql outobs = &rowlim nowarn ;
create table topn as
select &var, count(*) as frq
from &inset
group by &var
order by 2 desc
;
quit ;
data cntlin ;
length hlo $ 13 ;
set topn ;
fmtname = 'X' ;
start = &var ;
end = &var ;
label = catx(':', put(_n_, z2.0), &var) ;
type = vtype(&var) ;
call symput('vtype', type) ;
hlo = ' ' ;
keep fmtname start end label type hlo ;
run ;
proc sql ;
insert into cntlin(fmtname, start, end, label, type, hlo) values ('X', '**OTHER**', '**OTHER**', '~~All other values', "&vtype", 'O')
;
quit ;
proc format cntlin = cntlin ;
run ;
proc freq data = &inset order = formatted ;
tables &var / missing format = comma9.0 ;
%if &vtype = C %then %do ;
format &var $x. ;
%end ;
%else %do ;
format &var x. ;
%end ;
run ;
%mend limited_freq ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment