Desktop productivity for business analysts and programmers

data step calculation with arrays

Reply
Contributor
Posts: 31

data step calculation with arrays

Rookie here. I am trying to evaluate the following. The idea is that this should evaluate lp with an initial value (given in the first equation) and then loop to populate with those fixed values (e.g. kA, kB) and array values of A, B, C values. Initial values are zero for array AX, BX and CX. When I run it, i get zeroes for all instances of A, B and C and no LP values. Any idea where i am wrong?

 

 

data test1 (drop = i);
array AX[128] A1-A128;
array BX[128] B1-V128;
array CX[128] C1-T128;

do i = 1 to 128;
AX[i] = 0;
BX[i] = 0;
CX[i] = 0;
end;
run;

data test2 ; set test1; array AX[128] A1-A128; array BX[128] B1-V128; array CX[128] C1-T128; kA = 23.99196; kB = 21.04797; kC = 54.98294; lp = (log(kA) * 127/2) + (log(kB) * 127/2) + (log(kC) * 127/2); do i=2 to 128; lp = lp - kA * (AX[i] - AX[i-1])**2 / 2 - kB * (BX[i] -BX[i-1])**2 / 2 - kC * (CX[i] - CX[i-1])**2 / 2; end; run;

 

Super Contributor
Posts: 271

Re: data step calculation with arrays

Few typos in the code, please correct it.

B1-V128 should be B1-B128

C1-T128 should be C1-C128

 

After this correction, if I run the code I get lp =649.70316134 because of first equation

lp = (log(kA) * 127/2) + (log(kB) * 127/2) + (log(kC) * 127/2);

Rest main loop is not helping becuase all A1-A128, B1-B128 and C1-C128 are zero.

 

Let me know what are you looking for. 

 

 

data test1 (drop = i);
array AX[128] A1-A128;
array BX[128] B1-B128;
array CX[128] C1-C128;
do i = 1 to 128;
AX[i] = 0;
BX[i] = 0;
CX[i] = 0;
end;

run;



data test2(keep=lp) ;
set test1;
array AX[128] A1-A128;
array BX[128] B1-B128;
array CX[128] C1-C128;
kA = 23.99196;
kB = 21.04797;
kC = 54.98294;

lp = (log(kA) * 127/2) + (log(kB) * 127/2) + (log(kC) * 127/2);
       do i=2 to 128;
              lp = lp - kA * (AX[i] - AX[i-1])**2 / 2
                                  - kB * (BX[i] -BX[i-1])**2 / 2
                                                - kC * (CX[i] - CX[i-1])**2 / 2;
		end;

run;

 

 

Contributor
Posts: 31

Re: data step calculation with arrays

apologies. that was actually a wrong calculation on my end. Had to scrap the approach. Thanks for the help!
Ask a Question
Discussion stats
  • 2 replies
  • 170 views
  • 0 likes
  • 2 in conversation