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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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