🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 02-05-2021 04:12 AM
(1141 views)
Hi,
I want to get cumulative sum between 2 groups. I have data something like this
data WORK.NSUBJ1;infile datalines dsd truncover;input cnt_subj:32. trt:$13. EOSE:32.;datalines4;1,X1D1,0.311,X1D1,11,X1D15,33,X1D15,104,X1D15,305,X1D8,35,X1D8,10;;;;and i want sum of cnt_subj across each trt and eose.I did something like thisdata nsubj2;set nsubj1;by trt exose;if First.trt then cnt=0;cnt + cnt_subj;if Last.trt;keep trt exose cnt;run;But i will miss first observations of eose for example 0.3 will missI want something like below which has sum per rt and xde and populate for across for these 2 groups i.e cnt should be added up within trt and populated group wise like example trt=X1D1 cnt_subj should be 1+11=12 and i want 12 in trtr=X1D1 cnt =12 should be for EOSE=0.3 and 1 as below. my code removes the first record
cnt_subj | trt | EOSE | cnt |
1 | X1D1 | 0,3 | 12 |
11 | X1D1 | 1 | 12 |
1 | X1D15 | 3 | 8 |
3 | X1D15 | 10 | 8 |
4 | X1D15 | 30 | 8 |
5 | X1D8 | 3 | 10 |
5 | X1D8 | 10 | 10 |
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I don't know what you mean by "2 Groups". Bot I think you want to do this
data have;
input cnt_subj trt $ EOSE;
datalines;
1 X1D1 0.3
11 X1D1 1
1 X1D15 3
3 X1D15 10
4 X1D15 30
5 X1D8 3
5 X1D8 10
;
data want;
do until (last.trt);
set have;
by trt;
cnt + cnt_subj;
end;
do until (last.trt);
set have;
by trt;
output;
end;
cnt = 0;
run;
Result:
cnt_subj trt EOSE cnt 1 X1D1 0.3 12 11 X1D1 1.0 12 1 X1D15 3.0 8 3 X1D15 10.0 8 4 X1D15 30.0 8 5 X1D8 3.0 10 5 X1D8 10.0 10
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sorry, but the code you have posted is partially unreadable, please edit it. Also it might be a good idea to explain from what the values of cnt should be derived.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
updated
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I don't know what you mean by "2 Groups". Bot I think you want to do this
data have;
input cnt_subj trt $ EOSE;
datalines;
1 X1D1 0.3
11 X1D1 1
1 X1D15 3
3 X1D15 10
4 X1D15 30
5 X1D8 3
5 X1D8 10
;
data want;
do until (last.trt);
set have;
by trt;
cnt + cnt_subj;
end;
do until (last.trt);
set have;
by trt;
output;
end;
cnt = 0;
run;
Result:
cnt_subj trt EOSE cnt 1 X1D1 0.3 12 11 X1D1 1.0 12 1 X1D15 3.0 8 3 X1D15 10.0 8 4 X1D15 30.0 8 5 X1D8 3.0 10 5 X1D8 10.0 10