Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Summing Multiple Columns using SAS

Tags:

sas

I want to add multiple columns using SAS.

Data Have:

D C1 C2 C3 C4 C5.....   
J 01 02 00 04 15 
F 05 00 09 11 00  
M 12 14 88 00 00
A 55 03 00 00 00  
M 67 00 00 00 00

I don't want to do this

Data Want;

Set Have;  
N1 = C1;  
N2 = C1+C2;  
N3 = C1+C2+C3;  
N4 = C1+C2+C3+C4; 
N5 = C1+C2+C3+C4+C5;
Keep N:    
Run; 

want my table to look like this.

Data table Want

D N1  N2  N3  N4  N5.....   
J 01  03  03  07  22 
F 05  05  14  25  00  
M 12  26  114 00  00
A 55  58  00  00  00  
M 67  00  00  00  00

Note that I will have a lot of columns and the number will vary. I need a dynamic code which will automatically count the number of columns and perform the calculation. I need the bottom triangle to stay 0 and not keep adding it up. It still need to perform if there is zero value like in the case of (J,C3) in the example. I also need it to keep the order. Cannot change the order of the data.

like image 929
Myburge Avatar asked Oct 18 '25 09:10

Myburge


1 Answers

sas sum function can take range of variables like this:

data work;
    c1=1;
    c2=2;
    c3=3;
    n=sum(of c1-c3);
run;
like image 109
Bagin Avatar answered Oct 22 '25 04:10

Bagin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!