Say I have a variable x in a data set, I use PROC FREQ to sort the dataset.
PROC FREQ MyData;
tables x;
run;
So I get the frequencies. Say x has three groups. Group one has 4, group two has 24, group three has 59. Now I want to print the details (names and ages) with the smallest count group(group one). How?
What is group in your code? I get an error. Variable group is not on the file xxxxxx. By the way, I haven't learnt sql yet.
Proc freq doesn't sort a dataset and that code, as posted, is incorrect.
You can apply a WHERE to filter your dataset.
Data want;
Set mydata;
Where group = 1;
Run;
If you wanted this to be automatic/dynamic it's probably easiest to use a SQL query instead.
What is group in your code? I get an error. Variable group is not on the file xxxxxx. By the way, I haven't learnt sql yet.
In your example, let say X has values like: 'S', 'L','B' and
the 3 frequency groups are: S freq=is 4, L freq=24, B freq=59.
Now you are looking for details of group where X='S', i.e.
Title "Data for group with x=S ";
proc print data=have (where=(x='S'));
var ... enter here variables to print in desired order saparated by space ...;
run;
* print detail data for the category with the least observations;
* you can do this in one proc sql but not using a report like proc print;
%symdel sex;
proc sql;
select
sex into :sex separated by ""
from (
select
sex
,count(sex) as sexcnt
from
sashelp.class
group
by sex
)
having
sexcnt= min(sexcnt)
;quit;
proc print data=sashelp.class(where=(sex="&sex"));
run;quit;
or
* this tends to be more flexible;
* stored program;
* dosubl with a 'libname' command;
%symdel sex;
data _null_;
rc=dosubl('
%symdel sex;
proc sql;
select
sex into :sex separated by ""
from (
select
sex
,count(sex) as sexcnt
from
sashelp.class
group
by sex
)
having
sexcnt= max(sexcnt)
;quit;
');
sexmax=symget('sex');
call symputx('sexmax',sexmax); * pass to dosubl;
rc=dosubl('
proc print data=sashelp.class(where=(sex="&sexmax"));
run;quit;
');
run;quit;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.