M Elkasabi
M Elkasabi

Reputation: 19

Array reference in Stata?

I need a Stata code similar to this SAS code:

i=1; 
do while (i <= 6);
    A=B(C(i));
    i=i+1;
end;

Both B and C are arrays as

B_1, B_2, B_3, B_4, B_5 and B_6
C_1, C_2, C_3, C_4, C_5 and C_6

For example, if i = 1, then C(i) = C_1.

For row 3, if C_1 = 5, then I want to set A to B_5.

my problem is with accessing the array entry B(C(i)). How can I write this access in Stata?

Upvotes: 1

Views: 288

Answers (2)

Brendan
Brendan

Reputation: 4011

This is a little clumsy, but (insofar as I understand the question) it does what you are asking for:

clear all
forvalues i = 1/6 {
    scalar c_`i' = 7-`i'
}
forvalues i = 1/6 {
    scalar b_`i' = ceil(100/`i')
}

// test method
forvalues i = 1/6 {
    scalar a = b_`=c_`i''
    di a
}
scalar list

Note that as you've written the question, a is overwritten each time, so presumably there are commands following "a = b(c(i))". The Stata syntax '=...' (with a proper opening single quote) evaluates the scalar c_'i' and inserts the resulting value in the name for b_?, which a is then set equal to.

Upvotes: 1

Nick Cox
Nick Cox

Reputation: 37208

There isn't a one-to-one equivalent. See

FAQ . . . . . . . . . . . . . . . . Implementing SAS-like ARRAYs in Stata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . W. Gould 2/03 How do I implement SAS-like ARRAYs in Stata?

Here is a link.

Upvotes: 1

Related Questions