title: What is the Stata equivalent of an array?
body: <p>I've been trying to solve this problem reading different material but I just can't figure it out. It's pretty simple and takes about 5 seconds to do in languages that work with arrays, but I can't work it out in Stata.</p>
  
  <p>Please ignore the substitution of the backticks in some places, I couldn't get them to appear correctly. Let's say I have an ID variable and, among others, variables that show me whether a person has company or is alone and whether he drinks alcohol or not. If someone has company, I have a variable showing me how many these companions are and who they are (by their IDs in the data set). I'm trying to see if these companions drink alcohol and do a repetitive process with that info. Obviously in Stata if the companion has ID 345 I can't use the form alcohol[345] because that gives me the alcohol state of the observation 345, not of the ID 345. Is there a similar form in Stata? I can only list it by doing list alcohol if id==345. What I want to do is take these IDs and pass them on into a repetitive process that allows me to see whether all of these companions don't drink alcohol, part of this process is to have a variable d which I modify for every companion who does not drink alcohol (i.e. when the variable alcohol is not 1). I do the following, if m is the number of companions (let's say two here) and comp'k'`x' are the IDs of the companions of the person with ID k (let's say ID 8 here) :</p>
  
  <pre><code>gen d = .  
  local m = 2  
  local k = 8  
  forvalues x= 1/'m' {   
   replace d = 'x' if alcohol[comp'k'`x'] !=1  
  } 
  </code></pre>
  
  <p>so for example the first companion of person with ID 8 would have ID comp81= 345, the second comp82=567. But of course this code does not do what I want, for the reason above.</p>
  
  <p>The following will give me the alcohol state of the person whose companions are IDs comp81 and comp82, and not what I want which is the alcohol state of these companions:</p>
  
  <pre><code>. local m=2  
  . local k = 8  
  . forvalues x= 1/'m' {  
  2. list alcohol comp'k'`x'  
  3. }  
  </code></pre>
  
  <p>And the following doesn't work either:</p>
  
  <pre><code>local m = 2  
  local k = 8  
  forvalues x= 1/'m' {  
  2. list alcohol if id==comp'k'`x'  
  3. }  
  </code></pre>
  
  <p>I don't just want to list the alcohol state of the companions, but the above are examples to show that I can't find the Stata equivalent of an array and use it to solve the problem I have.
  To be concise, I'd like to know what I can use in stata to get the alcohol state of the companions, i.e. what in other programming languages using arrays would be alcohol[comp'k'`x'].</p>
  
  <p>Thank you all!</p>