Announcement

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

  • forvalues

    Dear members,

    i would be happy if you can help me. I conduct event study. Variables: group_id is = group(company_id set); id is =group(company_id); ret is stock price of company, market_return is index
    (DAX30), estimation window is =1 if dif<-30 & dif>=-60, event date=1 if dif>=-2 & dif<=2; dif is Differenz between date and event_date. And I write:
    forvalues i=1(1)N {
    2. l id company_id if id==`i' & dif==0
    3. reg ret market_return if id==`i' & estimation_window==1
    4. predict p if id==`i'
    5. replace predicted_return = p if id==`i' & event_window==1
    6. drop p
    7. }
    STATA returns :"invalid syntax r(198);
    Why? What is incorrect?

  • #2
    Stata won't get past

    Code:
     
    forvalues i=1(1)N {
    as even if N is a variable or scalar forvalues won't evaluate it. A number must go in that position, from say

    Code:
     
    su id, meanonly 
    forval i = 1/`r(max)' {
    Please do read the FAQ Advice, especially Sections 6 and 18. Unwillingness to follow list etiquette will mean that some active users will start to ignore your questions.

    Comment


    • #3
      You have two problems in the forvalues statement. The a(b)c notation works only for numlists, but forvalues does not take a numlist, it expects two numbers separated by a slash, nothing else.

      The other problem is the N in the forvalues command. Stata expects a number there. If your intent is that N is the number of company id's, then you need to precede your code storing that number in a local macro (which you can call N if you like):
      Code:
      summ id
      local N = r(max)
      and then change your forvalues statement to dereference that local macro into the actual number:

      Code:
      forvalues i = 1/`N' {

      Comment


      • #4
        Clyde and I made the same main point at the same time. However, this example shows that forvalues is perfectly happy with a(b)c notation:

        Code:
         
        . forval i = 1(2)9 {
          2. di `i'
          3. }
        1
        3
        5
        7
        9
        and that syntax is documented.

        Comment


        • #5
          I stand corrected, Nick. Thank you.

          Comment


          • #6
            thanks for yours replies. I am Svetlana Polyakova . I will change my account later.
            N is the max id (in my case 658). I applied the code:
            su id, meanonly
            forval i = 1/`r(max)' {
            2. l id company_id if id==`i' & dif==0
            3. reg ret market_return if id==`i' & estimation_window==1
            4. predict p if id==`i'
            5. replace predicted_return = p if id==`i' & event_window==1
            6. drop p
            7. }
            The program runs for one group (in my case 3MCOMPANY18) -----------------+
            | id company_id |
            |------------------|
            6245. | 32 3MCOMPANY18 |
            +------------------+
            but it stops with the second group (ABBOT LABORATORIES19) and displays:
            no observations
            r(2000);
            I dont´t understand why.

            Comment


            • #7
              The problem is most likely arising from -reg ret market_return if id==`i' &amp; estimation_window==1-.

              I'm a bit confused by your post, though. You say it runs for one group, which presumably is the group where `i' == 1. Yet in the output of the list command, it appears that id == 32. What happened to id's 1 through 31? Did they run OK?

              Anyway, in what I write below, I'm going to assume that `i' == 2 when your code breaks. If not, replace 2 by the id number that triggers the break (maybe it's 33?).

              Try
              Code:
              count if id == 2 & estimation_window == 1 & !missing(ret, market_return)
              I'm betting you'll get zero, and that's your answer. The regression sample is restricted by your -if- conditions, and it is also restricted to those observations with no missing values in any of the regression variables.

              Assuming I'm right, the question then becomes whether Stata has discovered the absence of data for a company, a data error that you need to fix, or whether that is OK and you need to modify the code to just skip the regression whenever there are no appropriate cases.

              If I'm wrong and that count doesn't come out zero, then it would be helpful to see more information. Run the count command, then precede the original code by -set trace on-, run it again, and post the results.

              Comment


              • #8
                Originally posted by Clyde Schechter View Post
                The problem is most likely arising from -reg ret market_return if id==`i' &amp; estimation_window==1-.

                I'm a bit confused by your post, though. You say it runs for one group, which presumably is the group where `i' == 1. Yet in the output of the list command, it appears that id == 32. What happened to id's 1 through 31? Did they run OK?

                Anyway, in what I write below, I'm going to assume that `i' == 2 when your code breaks. If not, replace 2 by the id number that triggers the break (maybe it's 33?).

                Try
                Code:
                count if id == 2 & estimation_window == 1 & !missing(ret, market_return)
                I'm betting you'll get zero, and that's your answer. The regression sample is restricted by your -if- conditions, and it is also restricted to those observations with no missing values in any of the regression variables.

                Assuming I'm right, the question then becomes whether Stata has discovered the absence of data for a company, a data error that you need to fix, or whether that is OK and you need to modify the code to just skip the regression whenever there are no appropriate cases.

                If I'm wrong and that count doesn't come out zero, then it would be helpful to see more information. Run the count command, then precede the original code by -set trace on-, run it again, and post the results.
                1 to 32 id run ok (3MCOMPANY18) and with id=33 stops the program. Yes, count=0

                Comment


                • #9
                  Originally posted by sp23 View Post

                  1 to 32 id run ok (3MCOMPANY18) and with id=33 stops the program. Yes, count=0
                  I detected what is my problem, for id=33 I dont`t have dif<=30 & dif>=60 because the event date is 25.01.2010 and the the starting date for that company is 04.01.2010. I have to change either the estimation_window or starting date for the date. Many Thanks!

                  Comment


                  • #10
                    In addition to Clyde's helpful remarks, check out the capture command.

                    Comment

                    Working...
                    X