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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.