Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Testing: creating loop that references parts of other variable names to determine value of a variable

    Hi all,

    My question is about how to create a loop that references the name of a variable in determining the value of another variable. First, allow me to explain and illustrate my data.

    I have data about multiple investors investing in multiple industries. For each of the 718 industries in my data, I have created a variable (ever_x, where “x” is the industry id number) which takes the value of 1 if a given investor has ever invested in that industry. A very small sample of the data are shown below with only the first and last industry investment (ever_1 and ever_718) indicators shown. I apologize for not formatting these with –dataex-, I could not get it to appear properly.


    investorid ever_1 ever_718 industrypair industrypair1 industrypair2 product_1 product_1615
    1 0 0 1/10 1 10 . .
    1 0 0 1/100 1 100 . .
    1 0 0 1/101 1 101 . .
    1 0 0 1/102 1 102 . .
    1 0 0 1/103 1 103 . .
    1 0 0 1/104 1 104 . .
    1 0 0 1/105 1 105 . .
    1 0 0 1/106 1 106 . .
    1 0 0 1/107 1 107 . .
    1 0 0 1/108 1 108 . .

    “industrypair” is a string variable which is simply every possible combination of the 718 industry id’s, separated by a “/”. “industrypair1” is the first industry in the pair, and “industrypair2” is the second industry in the pair.

    The product_x variable is intended to be an indicator of whether an investor has invested in both of the industries listed in industrypair. A different product_x variable exists for each of the 1615 investors (i.e., product_1 – product_1615), though I only show product_1 and product_1615 here. Currently, all of the product_x variables are blank.

    What I want to do is create a loop that achieves the following. For each row of industrypair, I want product_x to take a value of 1 if investorid==x (i.e., the same number as the product_x variable) AND the ever_x variable for the value of both industrypair1 and industrypair2.

    So, for example, if we look at the first row of the data I shared above, industrypair1 = 1 and industrypair2 = 10. I want product_1 to equal 1 if investorid==1, ever_1==1, and ever_10==1. Similarly, in the same row, I would want product_2 to equal 1 if investorid==2, ever_1==1, and ever_10==1. And so on, for all of the industry pairs, for all of the product_x values.

    I hope the above is clear. I am happy to provide further info if necessary.


    Last edited by Andy El-Zayaty; 13 Jun 2019, 22:45.
Working...
X