## find min value in a column within different groups

Solved
Frequent Contributor
Posts: 84

# find min value in a column within different groups

[ Edited ]

Hi All,

How to output the min value in a column within different groups? I want to use data step, not proc sql.

I have a data like below, I want output min pValue in each group. The observations in red color are what I want. Any idea? THanks,

 Obs group Sex pValue 1 (2) Young Female 0.0023 2 (2) Young Male 0.0536 3 (3) Child Female 0.0814 4 (3) Child Male 0.3191 5 (4) Teen Female 0.0561 6 (4) Teen Male 0.0979

Accepted Solutions
Solution
‎05-08-2017 09:45 AM
Super User
Posts: 23,980

## Re: find min value in a column within different groups

Sort it and use FIRST.

``````proc sort data=have;
by myByGroup pvalue;
run;

data want;
set have;
by myByGroup ;

if first.myByGroup;
run;``````

The SQL solution is one step.

All Replies
Super User
Posts: 2,049

## Re: find min value in a column within different groups

Hi , May i ask why not proc sql? Thanks

Frequent Contributor
Posts: 84

## Re: find min value in a column within different groups

Oh, nothing, I just want to know how to do it in data step.

Solution
‎05-08-2017 09:45 AM
Super User
Posts: 23,980

## Re: find min value in a column within different groups

Sort it and use FIRST.

``````proc sort data=have;
by myByGroup pvalue;
run;

data want;
set have;
by myByGroup ;

if first.myByGroup;
run;``````

The SQL solution is one step.

Super User
Posts: 13,913

## Re: find min value in a column within different groups

Please show exactly what you expect the output for that example data to look like.

Frequent Contributor
Posts: 84

## Re: find min value in a column within different groups

Hi ballardw,

I've marked the observations as red color.

Thanks,

C

☑ This topic is solved.