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;

 

 

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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