BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
samnan
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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;

 

 

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

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;

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1222 views
  • 0 likes
  • 2 in conversation