Hi everyone,
I have a question. I want to sum the variables ex(i,j) where i=1 or 2 or 3. For example,
data all_pars1;
set all_pars;
array t {3} theta1-theta3 ;
array ex {3,4} &vlist;
array s {4};
array in {4};
do i=1 to 3;
do j=1 to 4;
ex(i,j)=exp(t(i)*s(j)+in(j));
end;
end;
run;
with this code I'm getting variables ex1_1 ex1_2 ex1_3 ex1_4 ex2_1 ex2_2 ex2_3 ex2_4 etc.So I want to create sumex1 variable which is sum of ex1_1 ex1_2 ex1_3 ex1_4 and likewise sumex2 where it is sum of ex2_1 ex2_2 ex2_3 ex2_4.
It may be a simple code, but difficult for me. Many thanks!
Updated:
data all_pars1; set all_pars; array t {3} theta1-theta3 ; array ex {3,4} &vlist; array s {4}; array in {4}; array sumex{3} 8.; do i=1 to 3; do j=1 to 4; ex(i,j)=exp(t(i)*s(j)+in(j)); sumex{i}=sum(sumex{i},ex{i,j}); end; end; run;
sumex1 = sum(of ex1:);
sumex2 = sum(of ex2:);
Also look at your log (Maxim 2). Naming an array "in" is NOT recommended.
The simplest way is to just add in another array:
data all_pars1; set all_pars; array t {3} theta1-theta3 ; array ex {3,4} &vlist; array s {4}; array in {4}; array sumex{3} 8.; do i=1 to 3; do j=1 to 4; ex(i,j)=exp(t(i)*s(j)+in(j)); sumex{i}=sumex{i}+ex{i,j}; end; end; run;
They need to be initialized away from missing:
data all_pars1;
set all_pars;
array t {3} theta1-theta3 ;
array ex {3,4} &vlist;
array s {4};
array in {4};
array sumex{3} 8.;
do i=1 to 3;
sumex{i} = 0;
do j=1 to 4;
ex(i,j)=exp(t(i)*s(j)+in(j));
sumex{i}=sumex{i}+ex{i,j};
end;
end;
run;
If you look at your log (Maxim 2!), you'll see the NOTEs caused by missing values.
Keep in mind that all variables in a data step (except those coming from datasets or named in a retain statement) are set to missing at the start of a data step iteration.
As @Kurt_Bremser already said: read the log! If a missing value is part of an addition the result is always a missing value. The problem can be avoided by using sum-function instead of + sign.
Updated:
data all_pars1; set all_pars; array t {3} theta1-theta3 ; array ex {3,4} &vlist; array s {4}; array in {4}; array sumex{3} 8.; do i=1 to 3; do j=1 to 4; ex(i,j)=exp(t(i)*s(j)+in(j)); sumex{i}=sum(sumex{i},ex{i,j}); end; end; run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.