Solved
Contributor
Posts: 53

# collapse multiple records into unique one and add column value

Hi;

i have large data set look like as following, where for each (id) there are 5 (type)s that have (value) and (count). what i want is to transpose these (type)s and add any same (value), in addtion to the summation of (count) for the unique (id).

data have;
input id type value count;
cards;
1 1 32 2
1 3 54 1
1 1 10 7
1 2 20 10
1 5 56 0
1 4 68 2
1 2 59 2
1 3 82 4
2 1 52 8
2 5 64 9
2 3 76 6
2 4 98 8
2 2 39 9
2 4 96 5
3 1 58 2
3 2 63 6
3 4 72 3
3 5 99 4
3 3 37 1
3 1 66 0
;
data want;
input id type_1 type_2 type_3 type_4 type_5 count;
cards;
1 42 79 136 68 56 28
2 52 39 76 194 64 45
3 124 63 37 72 99 16
;run;

any help or suggestion is appreciated.

Accepted Solutions
Solution
‎03-08-2016 12:55 PM
Posts: 1,242

## Re: collapse multiple records into unique one and add column value

[ Edited ]

Hi @samnan,

Here is a data step solution, but the computation could be done using PROC SUMMARY or PROC SQL as well and (unlike the data step) these procedures would not require dataset HAVE to be sorted (or at least grouped) by ID.

``````data want;
do until(last.id);
set have;
by id;
array type_[5];
type_[type]=sum(type_[type], value);
scount=sum(scount, count);
end;
drop type value count;
rename scount=count;
run;``````

All Replies
Solution
‎03-08-2016 12:55 PM
Posts: 1,242

## Re: collapse multiple records into unique one and add column value

[ Edited ]

Hi @samnan,

Here is a data step solution, but the computation could be done using PROC SUMMARY or PROC SQL as well and (unlike the data step) these procedures would not require dataset HAVE to be sorted (or at least grouped) by ID.

``````data want;
do until(last.id);
set have;
by id;
array type_[5];
type_[type]=sum(type_[type], value);
scount=sum(scount, count);
end;
drop type value count;
rename scount=count;
run;``````

Contributor
Posts: 53