Announcement

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

  • Question about continue demand

    Dear fellow Stata users,

    I've got a question about the 'continue' command. This is my dataset:
    identifyer observationyear firstyear
    1 1990 1990
    2 1991 1990
    3 1991 1990
    4 1993 1990
    5 1994 1990
    6 1995 1990
    I have got a list of observation identifyers in the variable 'identifyer'. I created a loop for these observations which only needs to run if a certain equality is not met, therefore I created this code:

    foreach identifyer in identifyer {
    if `observationyear' == `firstyear' {
    continue
    }
    ...
    }

    This should thus only run for identifyer 2-5. I tried some different things, but even without the 'if and continue' command the loop only runs one time (only for identifyer 1). What could be the cause that the loop only runs 1 time (so only for observation identifyer[1])?

    This also runs only one time:
    foreach id in identifyer {
    disp `id'
    }

    Result:
    1
    .

    Kind regards,

    Remon
    Last edited by remonrace; 28 Apr 2015, 08:45.

  • #2
    The if command doesn't work in the way you want. See http://www.stata.com/support/faqs/pr...-if-qualifier/

    In my view the title of this FAQ is backwards from the usual question. The FAQ has this title:

    "I have an if or while command in my program that only seems to evaluate the first observation. What’s going on?"

    but the usual question is (as here) more like


    "I have an if or while command in my program that yields puzzling results. What’s going on?"

    and the answer then is: it evaluates only in the first observation.

    Coming back to your question: it is most unlikely that you need to loop in this way, but to get further advice you'd need to explain what you want to do with the rest of your code.





    Comment


    • #3
      Hi Nick, thank you for your reply.

      The truth is that after each observation number, there are some additional variables:
      .
      .
      Manager identifyer observationyear firstyear cusip cusipcited 1 cusipcited 2 cusipcited 600
      1 1 1990 1990 A
      1 2 1991 1990 A
      1 3 1991 1990 B
      1 4 1993 1990 C
      1 5 1994 1990 A
      1 6 1995 1990 B G A T
      So each identifyer represents observations of one single manager. At certain points in tme (observation year), he holds various cusips (which represent firm numbers). During that year, the firm he holds made innovations which cited other firms. In this example, in 1995 manager 1 held firm B, and in that year that firm cited firm G, A and T.

      Now I want to find out whether firm A, was held by that same manager in the past. In this case, firm A is held by the same manager in 1994, 1991 and 1990. Therefore I thought that that if I could loop over all identifyers, and then loop over the cusipcited* of that identifyer, I could see whether those firms were held previously.

      But aren't there two issues: 1) My code only loops over the first observation, 2) Like you pointed out, on top of this the if statement only looks at the first observation. I thought that as it would loop for each observation, it would check for the if statement (if `observationyear' == `firstyear') for each identifyer separately.
      Last edited by remonrace; 28 Apr 2015, 09:22.

      Comment


      • #4
        Solved it:

        I sorted on (manager observationyear cusip), and then created a variable obs = _n. I was able to create local macro's dependend on manager number, year and id by using summarize and then using r(mean).

        Then I created this loop, I don't write all codes exactly due to the size:
        forval i = 1/max(obs)
        local j = 0
        local m = m[`i']
        local y= y[`i']
        local id = id[`i']
        foreach var of varlist cusipcited*

        forval yr = minyr(m)/(maxyr(m)-1)
        forval id = 1/maxid(m,y)
        local obsid = obs at `m'`yr'`id'
        if `var'[`i']==cusip[`obsid'] {
        local j = `j'+1
        }

        }
        }
        }
        replace count = `j'
        }

        The only downside is that it takes a very long time to calculate all values.

        The next question is the following: for each patent, I want to know for each previous year separately, how many matches there are. In the example: for id == 6, I want to know in lagged year 1 (1994) how many matches there are between the cited patents, and the holdings of that same manager in that same year. Then also for lagged 2 years (1993), etc. etc. up till the first observation year.

        What would be an easy way to do this? I was thinking of something like this, as we have data from 1980 till 2006:

        gen match_1980 == .
        gen match_1981 == .
        ...
        gen match_2006 == .

        [Code in italics from above]
        forval yr = `minyr' / (`maxyr'-1)
        forval id = 1/`maxid(m,y)' {
        [same code as above in bold]
        replace match_`yr' = `j'
        }
        Last edited by remonrace; 29 Apr 2015, 08:07.

        Comment


        • #5
          The same question has also been asked in the mata forum here. Since the question has nothing to do with mata (except perhaps for a hope that someone would provide a solution using mata), I'll answer here instead. Picking-up on William's suggestion (in #2 of the mata thread) of reshaping the data to long form and using his data setup, it seems that this can be done without any loop at all:

          Code:
          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
          
          tempfile original
          save "`original'"
          
          * master list of the manager's firms (first year they appear)
          bysort mgrno cusip (appyear): keep if _n == 1
          keep mgrno cusip appyear
          rename (cusip appyear) (cited firstyear)
          tempfile managerfirms
          save "`managerfirms'"
          
          * convert original data to long form
          use "`original'"
          reshape long cusipcited_, i(mgrno appyear cusip) j(cite_num)
          rename cusipcited_ cited
          drop if cited == "."
          drop cite_num
          
          * match cited firms with manager's firms
          merge m:1 mgrno cited using "`managerfirms'", keep(master match) nogen
          
          sort mgrno appyear cusip cited

          Comment

          Working...
          X