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.3
11,X1D1,1
1,X1D15,3
3,X1D15,10
4,X1D15,30
5,X1D8,3
5,X1D8,10
;;;;
and i want sum of cnt_subj across each trt and eose.
I did something like this
data 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 miss
I 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 |
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
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.
updated
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
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.