limit 10
is not valid code in SAS proc sql
. In general, there isn't a good way to limit observations on output in SAS PROC SQL; (obs=10)
is not valid as a dataset option for some reason.
You also can't order by count(*)
; you can only order by the created column.
So you could do this:
proc sql;
create view work.temp as
select model, count(*) as frequency
from sashelp.cars
group by model
order by 2 desc
;
quit;
2
there refers to the second column (so, the frequency column).
If you want to limit to a smaller number of observations, you can use (obs=#)
where you actually use the view, in some cases (but not in others - in general, I don't think it's valid in PROC SQL in most places).
In general this particular query is probably easier in base SAS rather than SQL, especially if you want to limit its output.
proc freq data=sashelp.cars order=freq noprint;
where origin='Asia';
tables make/out=makelist(where=(count>10) keep=make count);
run;
That will run a bit faster than SQL (assuming you don't gain anything from the view, anwyay; if you're only outputting a few rows view won't help you much). Of course here I'm limiting to a particular count not a particular # of obs, but it's much the same.
To accomplish the title's question, you could then simply select the top 10 entries from that dataset (assuming you asked PROC FREQ
to return the dataset sorted by frequency as I do above):
data topten;
set makelist;
if _n_ le 10; *first 10 iterations;
run;
Or use proc rank
or any number of options.