## SUM and Percent

Solved
Occasional Contributor
Posts: 16

# SUM and Percent

Hello,

How to create table like this one below. Sum first and second value and than from each value get rate. Is there anybody who know code for this problem?

My data

 date Value E D 201612 82842 0 0 201612 621 0 1 201612 31595 1 0 201612 64 1 1

Want data

 date Value E D Sum Rate 201612 82842 0 0 83463 0,74% 201612 621 0 1 201612 31595 1 0 31659 0,20% 201612 64 1 1

Accepted Solutions
Solution
‎07-11-2017 04:35 AM
Super User
Posts: 10,239

## Re: SUM and Percent

``````data have;
input date \$ value e d;
cards;
201612 82842 0 0
201612 621 0 1
201612 31595 1 0
201612 64 1 1
;
run;

data want;
merge
have
have (firstobs=2 rename=(e=_e d=_d date=_date value=_value))
;
if d = 0 and _date = date and _e = e
then do;
sum = value + _value;
rate = _value / sum;
end;
format rate percent7.2;
keep date value e d sum rate;
run;

proc print data=want noobs;
run;
``````

Result:

``` date     value    e    d     sum        rate

201612    82842    0    0    83463     0.74%
201612      621    0    1        .      .
201612    31595    1    0    31659     0.20%
201612       64    1    1        .      .
```
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Super User
Posts: 10,239

## Re: SUM and Percent

[ Edited ]
``````data want;
merge
have
have (firstobs=2 rename=(e=_e d=_d date=_date value=_value))
;
if d = 0 and _date = date and _e = e
then do;
sum = value + _value;
rate = _value / sum;
end;
format rate percent7.2;
keep date value e d sum rate;
run;``````

Edit: added a closing bracket in the merge statement, to correct a syntax error.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 16

## Re: SUM and Percent

This is result of this code...it doesnt work properly.

 date Value E D Sum Rate 201612 621 0 0 1242 50.0% 201612 31595 0 1 . . 201612 64 1 0 128 50.0% 201612 64 1 1 . .
Solution
‎07-11-2017 04:35 AM
Super User
Posts: 10,239

## Re: SUM and Percent

``````data have;
input date \$ value e d;
cards;
201612 82842 0 0
201612 621 0 1
201612 31595 1 0
201612 64 1 1
;
run;

data want;
merge
have
have (firstobs=2 rename=(e=_e d=_d date=_date value=_value))
;
if d = 0 and _date = date and _e = e
then do;
sum = value + _value;
rate = _value / sum;
end;
format rate percent7.2;
keep date value e d sum rate;
run;

proc print data=want noobs;
run;
``````

Result:

``` date     value    e    d     sum        rate

201612    82842    0    0    83463     0.74%
201612      621    0    1        .      .
201612    31595    1    0    31659     0.20%
201612       64    1    1        .      .
```
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.