BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas_newbie3
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
sas_newbie3
Obsidian | Level 7

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

4 REPLIES 4
Reeza
Super User

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. 

 

sas_newbie3
Obsidian | Level 7

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.

Shmuel
Garnet | Level 18

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;

 

rogerjdeangelis
Barite | Level 11

* 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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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