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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
dustychair
Pyrite | Level 9
Hi RW9,
Thanks for responding my question. This codes creates 3 variables named sumex but they are all missing. Do you have any idea why they are missing?
Thanks
Kurt_Bremser
Super User

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.

andreas_lds
Jade | Level 19

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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
  • 6 replies
  • 2557 views
  • 3 likes
  • 4 in conversation