Solved
New Contributor
Posts: 4

# How to get the Sum by Variable

Dear All

I'm new to sas and stuck with the following issue.

I want to group my dataset by the UR_1 variable and get the summation of UR_1P variable for each UR_1?

Can anyone suggest how to get this?

Sam

Accepted Solutions
Solution
‎09-11-2014 10:12 AM
Posts: 1,270

## Re: How to get the Sum by Variable

Data step to get the desired output.

proc sort data=have;
by UR_1;
run;

data want;
set have;
by UR_1;
if first.UR_1 then sum_UR_1P=0;
sum_UR_1P + UR_1P;
if last.UR_1;
run;

All Replies
Contributor
Posts: 45

## Re: How to get the Sum by Variable

Hi,

You can use PROC SQL;

proc sql;

select UR_1,sum(UR_1P) from have group by UR_1;

quit;

Contributor
Posts: 45

## Re: How to get the Sum by Variable

Other methods...

proc print data=have;

sum UR_1P;

by UR_1;

run;

proc means data=have sum;

var UR_1P;

by UR_1;

run;

New Contributor
Posts: 4

## Re: How to get the Sum by Variable

Thanks Ravi

This perfectly works. But how can I use this inside data step?

I want to assign these values to a table and to use for further calculation.

Posts: 1,270

## Re: How to get the Sum by Variable

To get values in a table just add create table in sql syntax.

proc sql;

create table want as

select UR_1,sum(UR_1P) as UR_1P from have group by UR_1;

quit;

New Contributor
Posts: 4

## Re: How to get the Sum by Variable

How can i use this option in the data step to get a table?

proc print data=have;

sum UR_1P;

by UR_1;

run;

Frequent Contributor
Posts: 131

## Re: How to get the Sum by Variable

As far as I know, there is no way to use this kind of summary in a data step.

You could use a PROC MEANS instead and use the OUTPUT Statement and a 'SUM =' there to receive only the sum.

The SQL-solution provided above should be the quicker way, imho. :-)

Cheers

Michael

Solution
‎09-11-2014 10:12 AM
Posts: 1,270

## Re: How to get the Sum by Variable

Data step to get the desired output.

proc sort data=have;
by UR_1;
run;

data want;
set have;
by UR_1;
if first.UR_1 then sum_UR_1P=0;
sum_UR_1P + UR_1P;
if last.UR_1;
run;

🔒 This topic is solved and locked.