Announcement

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

  • Question with regard to nested loop

    Hi guys,

    I've got a question on my specific set of data.

    It is ordered like this:
    Description Manager number Application year Holding firm identifyer Cited firm 1 Cited firm 2 Cited firm 3 Cited firm 1356
    Variable name mgrno appyear cusip cusipcited 1 cusipcited 2 cusipcited 3 cusipcited 1356
    1 1980 A G . H
    1 1980 B B D I Z
    1 1980 C A . . .
    1 1980 D D G H I
    1 1981 E E B . .
    1 1981 A B
    1 1981 B F X Y Z
    1 1981 C G
    1 1982 D
    1 1982 E
    1 1982 A
    1 1982 B
    1 2006 B
    So for 1 manager number (mgrno), I have a list in which year (appyear) he held which firms (cusip).

    Example (in bold): in 1981, manager 1 held firm E. In that year, firm E cited patents held by firm E and B. I want to know, if firm E and B were also owned by manager 1, in the years before 1980 (which is true for both in this example).

    Unfortunately, I have absolutely no idea where to start. Even on paper I can't solve it without using mutliple nested loops. Does anyone have an idea on how to take on such an issue?

  • #2
    You might find it helpful to start by using the reshape command to transform your data from wide to long format. Copy this sample into your do-file editor and run it to see the entire output; I've just shown the final result.
    Code:
    // set up sample data changing "." to string missing value ""
    clear
    input mgrno appyear str1 (cusip cusipcited_1 cusipcited_2 cusipcited_3) 
    1 1980 A G . H 
    1 1980 B B D I 
    1 1980 C A . . 
    1 1980 D D G H 
    1 1981 E E B . 
    1 1981 A B . .
    1 1981 B F X Y 
    1 1981 C G . . 
    1 1982 D . . .   
    1 1982 E . . .   
    1 1982 A . . .   
    1 1982 B . . .   
    end
    foreach cited of varlist cusipcited* {
        replace `cited' = "" if `cited'=="."
        }
    list, noobs sepby(appyear)
    // demonstrate reshape
    reshape long cusipcited_, i(mgrno appyear cusip) j(cite_num)
    list, noobs sepby(appyear)
    // clean up and delete extraneous stuff
    rename cusipcited_ cited
    drop if cited=="" & cite_num > 1
    drop cite_num
    list, noobs sepby(appyear)
    Code:
      +---------------------------------+
      | mgrno   appyear   cusip   cited |
      |---------------------------------|
      |     1      1980       A       G |
      |     1      1980       A       H |
      |     1      1980       B       B |
      |     1      1980       B       D |
      |     1      1980       B       I |
      |     1      1980       C       A |
      |     1      1980       D       D |
      |     1      1980       D       G |
      |     1      1980       D       H |
      |---------------------------------|
      |     1      1981       A       B |
      |     1      1981       B       F |
      |     1      1981       B       X |
      |     1      1981       B       Y |
      |     1      1981       C       G |
      |     1      1981       E       E |
      |     1      1981       E       B |
      |---------------------------------|
      |     1      1982       A         |
      |     1      1982       B         |
      |     1      1982       D         |
      |     1      1982       E         |
      +---------------------------------+

    Comment


    • #3
      Thank you William. In the meantime, I was working on the file and found out the "." observations don't interfere with any codes I run on the file.

      I have come quite far with writing a code in the wide format, but it is becoming extremely complicated. I had to use the wide format to match patent data (appyear - cusip - cusipcited) to intistutional holdings (manager number - appyear), but it might indeed be easier to reshape into long for this format. There is a difficulty with the dimensionality of the data, as some firms in some years cite up to 460 other firms but I can at least give it a try.

      Comment


      • #4
        This is not really a mata question and a variant of the same question was posted on the general forum so I posted a reply there.

        Comment

        Working...
        X