New Contributor
Posts: 2

# Determine the largest and second largest value in proc freq

Hello,

i am searching for a solution to determine and put out the largest and second value in a proc freq analysis. The proc freq look as the follow:

proc freq data = Personen (where = (
sex = '1' AND
type in ('X','Y','Z') ));
title 'Sex Types';
tables typ * geschlecht
/ NOROW NOCOL Nopercent format=commax18.0 out=WORK.sex_type;
run;

In Pesonen is a variable like 'tallness', with a numeric tallness value. Now i want to give out the tallest and the second tallest person in the defined group, each in an own column in the table above.

Do you haven idea?

T1000

SAS Super FREQ
Posts: 4,245

## Re: Determine the largest and second largest value in proc freq

See the article "Create a bar chart with only a few categories," which starts by using PROC FREQ to extract the top categories.

The main idea is to use the ORDER=FREQ option to sort the categories and use the OUT= option on the TABLES statement to save the categories to a data set. Here is an example of the top 2 categories of vehicles:

``````proc freq data=sashelp.cars ORDER=FREQ noprint;
tables make / out=FreqOut;
run;

proc print data=FreqOut(obs=2);
run;``````
SAS Super FREQ
Posts: 4,245

## Re: Determine the largest and second largest value in proc freq

I just noticed that you have a 2-way table. Therefore you need to throw in a PROC SORT call to find the two cells that have the largest frequency counts:

``````proc sort data=FreqOut;
by Count;
run;

``````
Super User
Posts: 13,583

## Re: Determine the largest and second largest value in proc freq

It might help to show what your output is expected to look like.

Are you looking for the tallest within each level of Type (or typ) or combination of typ * geschlecht

or overall in the data set?

New Contributor
Posts: 2

## Re: Determine the largest and second largest value in proc freq

Thank´s a lot for your replies. As output I am looking for the second tallest observation in the combination of typ* geschlecht. Like the Maximum in proc means, but just the second Maximum (I have just one variable for analysis and one for classification) - I hope that´s the correct translation.

Is it possible to sort in a data step a select the second observation of a variable?

I wish you a nice Weekend

Discussion stats
• 4 replies
• 1126 views
• 0 likes
• 3 in conversation