Print the details with the least count group.

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

Print the details with the least count group.

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?


Accepted Solutions
Solution
‎02-11-2017 08:40 PM
Contributor
Posts: 26

Re: Print the details with the least count group.

[ Edited ]

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.

View solution in original post


All Replies
Grand Advisor
Posts: 17,411

Re: Print the details with the least count group.

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. 

 

Solution
‎02-11-2017 08:40 PM
Contributor
Posts: 26

Re: Print the details with the least count group.

[ Edited ]

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.

Super User
Posts: 1,243

Re: Print the details with the least count group.

[ Edited ]

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;

 

Valued Guide
Posts: 505

Re: Print the details with the least count group.

* 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;

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 186 views
  • 0 likes
  • 4 in conversation