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

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
       
       


 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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 

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

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.

noda6003
Quartz | Level 8

updated

PeterClemmensen
Tourmaline | Level 20

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 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 3 replies
  • 1017 views
  • 1 like
  • 3 in conversation