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.
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;
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;
thanks @FreelanceReinh for your help
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.