Solved
New Contributor
Posts: 4

# How to get summary value of A field based on the min value of B field in same PROC MEANS?

Dear All,

Assume, there are a dataset. The dataset name is my_data, there are 3 fields:term, financed amount, interes_rate.

My purposes are calculate the min interest_rate per term. And, calculate the sum of financed amount for that term's interest rate is min value.

Now, I use two PROC MEANS to finish the job.

could you I just use only one PROC MEANS to do that?

Thanks.

Accepted Solutions
Solution
‎11-09-2014 09:26 PM
Super User
Posts: 23,689

## Re: How to get summary value of A field based on the min value of B field in same PROC MEANS?

You could use the ways/type statement to generate all combination and then only keep the ones you want using a where statement in a single proc means.

If you post sample data I can post sample code.

All Replies
Super User
Posts: 10,770

## Re: How to get summary value of A field based on the min value of B field in same PROC MEANS?

Sure.

by term ;

var interest_rate ;

output out=want  min=min sum=sum

New Contributor
Posts: 4

## Re: How to get summary value of A field based on the min value of B field in same PROC MEANS?

thx. but it is not what do i want.

for each term, how to get sum of financed amount that according interest rate is the lowest rate.

Posts: 1,147

## Re: How to get summary value of A field based on the min value of B field in same PROC MEANS?

i believe we could achieve the output you are expecting, but we need to process the data before using proc means,

first we need to create a variable, which stores the min interest rate for each term and retains the same value across all records for that term. Then subset the data on the min interest rate records and pass the same in proc means to get the sum,

Try

data have;

input term interest amount;

datalines;

1991 10 20000

1991 10 20000

1991 10 20000

1991 20 30000

1991 15 25000

;

run;

proc sort data = have;

by      term interest;

run;

data have2;

set have;

retain min;

by  term interest;

if first.term then min= interest;

run;

proc means data=have2(where=(interest=min));

by term ;

var amount;

output out = want sum=sum;

run;

Thanks,

Jag

Thanks,
Jag
Super User
Posts: 10,770

## Re: How to get summary value of A field based on the min value of B field in same PROC MEANS?

As Jag pointed out, you need to post some dummy data to clarify your question. It is not possible for one proc means ,but easy for proc sql.

```

data have;
input term interest amount;
datalines;
1991 10 20000
1991 10 20000
1991 10 20000
1991 20 30000
1991 15 25000
;
run;
proc sql;
create table want as
select  term,interest,sum(amount) as sum_amount
from (select * from have group by term having interest=min(interest))
group by term,interest;
quit;

```

Xia Keshan

New Contributor
Posts: 4

## Re: How to get summary value of A field based on the min value of B field in same PROC MEANS?

thx everyone. actually, now, i done that with what your suggestion. i use two separate step. i just want to know whether there are more sas style solution.

Solution
‎11-09-2014 09:26 PM
Super User
Posts: 23,689

## Re: How to get summary value of A field based on the min value of B field in same PROC MEANS?

You could use the ways/type statement to generate all combination and then only keep the ones you want using a where statement in a single proc means.

If you post sample data I can post sample code.

New Contributor
Posts: 4

## Re: How to get summary value of A field based on the min value of B field in same PROC MEANS?

Dear Reeza,

Thx. I will try to do by mysefl based on your suggestion.

Best Regards,

Liu ming

🔒 This topic is solved and locked.