## arrays

Hi all,

The bold statement in the below code is working, at least I did not get any errors. Based on this code I'm supposed to have 4 variables: p_m1, pm_2, pm_3, and pm_4. The values for pm_2 are correct however others are wrong. Also all values are the same for each of those variables. Could you help me to find what I'm missing?

Thanks

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.;
array p_m{4} ;
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});
p_m{j}=((ex1_1/sumex1)*(ex2_1/sumex2))+((ex1_2/sumex1)*(ex2_2/sumex2))+
((ex1_3/sumex1)*(ex2_3/sumex2))+((ex1_4/sumex1)*(ex2_4/sumex2));
end;
end;
run;

4 REPLIES 4

## Re: arrays

You can probably diagnose this one yourself by adding PUT statements inside the loops. For example just before the first END statement add

put i= j= sumex1= sumex2= sumex3= ex1_2= ex2_2= ........ ;

To avoid a ton of clutter also use:

set all_pars (obs=2);

at the beginning.

## Re: arrays

@dustychair wrote:

Hi all,

The bold statement in the below code is working, at least I did not get any errors. Based on this code I'm supposed to have 4 variables: p_m1, pm_2, pm_3, and pm_4. The values for pm_2 are correct however others are wrong. Also all values are the same for each of those variables. Could you help me to find what I'm missing?

Thanks

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.;
array p_m{4} ;
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});
p_m{j}=((ex1_1/sumex1)*(ex2_1/sumex2))+((ex1_2/sumex1)*(ex2_2/sumex2))+
((ex1_3/sumex1)*(ex2_3/sumex2))+((ex1_4/sumex1)*(ex2_4/sumex2));
end;
end;
run;

Whtout data to run this or test it, or an understanding of what you're trying to calculate it's unlikely we can answer your question.

We can make suggestions based on guesses from common errors.

1. Are there any missing and if so, are you handling them in the manner you expect?

2. Why are the values in the p_m(j) hard coded? Note you're using hardcoded variable values not array references, which seems a bit weird. Since not all will be initialized at the start is that giving you the incorrect values?

## Re: arrays

@Reeza @ballardw You guys are right!

I wrote hardcode because I don't know how to write loops in arrays. So, I was trying to calculate without loops and arrays then see the pattern and write the arrays. Although I understand the pattern/idea of formula I don't know how to write it with arrays. For example for p_m1 the equation is,

p_m1=(ex1_1/sumex1)*(ex3_1/sumex3) + (ex1_2/sumex1)*(ex3_2/sumex3) + (ex1_3/sumex1)*(ex3_3/sumex3) + (ex1_4/sumex1)*(ex3_4/sumex3)

For p_m2 it is,

p_m2=(ex1_1/sumex1)*(ex2_1/sumex2) + (ex1_2/sumex1)*(ex2_2/sumex2) + (ex1_3/sumex1)*(ex2_3/sumex2) + (ex1_4/sumex1)*(ex2_4/sumex2)

and lastly for p_m3 it is,

p_m3= (ex2_1/sumex2)*(ex3_1/sumex3) + (ex2_2/sumex2)*(ex3_2/sumex3) + (ex2_3/sumex2)*(ex3_3/sumex3) + (ex2_4/sumex2)*(ex3_4/sumex3)

I'll appreciate you if you help me to write these codes with arrays/loops whatever I need.

Thank you

## Re: arrays

@dustychair wrote:

Hi all,

The bold statement in the below code is working, at least I did not get any errors. Based on this code I'm supposed to have 4 variables: p_m1, pm_2, pm_3, and pm_4. The values for pm_2 are correct however others are wrong. Also all values are the same for each of those variables. Could you help me to find what I'm missing?

Example data and the value of the macro variable VARLIST.

Since you say pm_2 (assuming you mean p_m2) is "correct" then I kind of have to assume there is something data related. Also, how do we know what "wrong" would be?? If you expect certain values then you should provide the expected values.

I suspect that there may be some issue around

```array sumex{3} 8.;

```

You create the sumex array with 3 elements but as far as I can see you only use sumex1 and sumex2 in calculations.

Discussion stats
• 4 replies
• 630 views
• 2 likes
• 4 in conversation