Announcement

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

  • Syntax

    Hello!
    Someone can help me to correct this syntax:

    * Charger les données
    clear
    input time var1 var2
    1 1.2 2.3
    2 1.5 2.4
    3 1.6 2.5
    4 1.8 2.7
    5 1.7 2.6
    6 2.0 2.8
    7 2.1 3.0
    8 2.3 3.2
    9 2.4 3.3
    10 2.6 3.4
    end

    * Générer une variable d'indice de temps
    gen t = _n

    * Définir la taille de la fenêtre glissante (par exemple, 3 périodes)
    local window_size = 3

    * Calculer la corrélation dynamique
    gen corr_dynamic = .

    forvalues i = `window_size' / _N {
    local start = `i' - `window_size' + 1
    local end = `i'
    corr var1 var2 in `start'/`end'
    matrix r = r(C)
    replace corr_dynamic = r[1,2] in `i'
    }

    * Afficher les résultats
    list time var1 var2 corr_dynamic



    invalid syntax
    r(198);

  • #2
    Code:
    forvalues i = `window_size' / `=_N' {
        ...
    }

    Comment


    • #3
      Tanks Kelin
      i have tried it but it doesn't work

      Comment


      • #4
        #2 looks bang on to me.

        "doesn't work" is no information really. What did you do (exactly)? What did or did not happen (exactly)?

        Why didn't it work?

        1. Possibly you didn't type exactly what is needed. So, you need to show us the code you tried and error messages (if any).

        2. You may have tried referring to a local macro in a space where it isn't visible. This is more subtle but see https://journals.sagepub.com/doi/pdf...36867X20931028

        Comment


        • #5
          Your answer is not informative. What exactly did you try? What exactly "doesn't work"?

          Making my suggested change (and only this change) to your code works for me:

          Code:
          (output omitted))
          . 
          . forvalues i = `window_size' / `=_N' {
            2. local start = `i' - `window_size' + 1
            3. local end = `i'
            4. corr var1 var2 in `start'/`end'
            5. matrix r = r(C)
            6. replace corr_dynamic = r[1,2] in `i'
            7. }
          (obs=3)
          
                       |     var1     var2
          -------------+------------------
                  var1 |   1.0000
                  var2 |   0.9608   1.0000
          
          (1 real change made)
          (obs=3)
          
                       |     var1     var2
          -------------+------------------
                  var1 |   1.0000
                  var2 |   1.0000   1.0000
          
          (1 real change made)
          (obs=3)
          
                       |     var1     var2
          -------------+------------------
                  var1 |   1.0000
                  var2 |   1.0000   1.0000
          
          (1 real change made)
          (obs=3)
          
                       |     var1     var2
          -------------+------------------
                  var1 |   1.0000
                  var2 |   0.9820   1.0000
          
          (1 real change made)
          (obs=3)
          
                       |     var1     var2
          -------------+------------------
                  var1 |   1.0000
                  var2 |   0.9608   1.0000
          
          (1 real change made)
          (obs=3)
          
                       |     var1     var2
          -------------+------------------
                  var1 |   1.0000
                  var2 |   0.9820   1.0000
          
          (1 real change made)
          (obs=3)
          
                       |     var1     var2
          -------------+------------------
                  var1 |   1.0000
                  var2 |   1.0000   1.0000
          
          (1 real change made)
          (obs=3)
          
                       |     var1     var2
          -------------+------------------
                  var1 |   1.0000
                  var2 |   0.9820   1.0000
          
          (1 real change made)
          
          . 
          . * Afficher les résultats
          . list time var1 var2 corr_dynamic
          
               +-------------------------------+
               | time   var1   var2   corr_d~c |
               |-------------------------------|
            1. |    1    1.2    2.3          . |
            2. |    2    1.5    2.4          . |
            3. |    3    1.6    2.5   .9607691 |
            4. |    4    1.8    2.7          1 |
            5. |    5    1.7    2.6          1 |
               |-------------------------------|
            6. |    6      2    2.8   .9819803 |
            7. |    7    2.1      3   .9607689 |
            8. |    8    2.3    3.2   .9819804 |
            9. |    9    2.4    3.3          1 |
           10. |   10    2.6    3.4   .9819807 |
               +-------------------------------+

          Comment


          • #6
            I'd just like to calculate the dynamic correlations for each of the three years, but the program shows me errors, and I don't know where.

            Comment


            • #7
              Click image for larger version

Name:	erreurs.PNG
Views:	1
Size:	26.0 KB
ID:	1760512

              Ok

              Comment


              • #8
                You missed out the = sign before _N

                Comment


                • #9
                  Here is some general advice: never re-type code; always copy and paste. This goes for both posting code to Statalist and obtaining code from Statalist.

                  Comment


                  • #10
                    Well, they say "never say never," and this is one situation where that applies. In general, copy and paste is the way to go. And I think that one should always copy from Stata and paste into Statalist--I know of no problems that can create, and it prevents the introduction of errors. But when copying code from other sources, including this forum, you will sometimes pick up "non-printing" characters that are not displayed by Stata but are, unfortunately, there in the copied material and visible to the parser. These can then lead to peculiar messages such as
                    Code:
                    command   is not recognized
                    with no (visible) indication of what command is not being recognized. It doesn't happen very often when copying from Statalist, but it occasionally does. And when that happens, the solution is to delete the line of code that leads to the error message and retype it by hand. To be clear, copy and paste should always be used first. But when it fails in this particular way, hand-typing extremely carefully may be needed to fix it.

                    By the way, it happens often when copying from Word or PDF documents--so my advice is to avoid copying code from these to the extent possible. If you must copy from one of those, I recommend "laundering" it through a primitive text editor rather than pasting it directly into Stata. By a primitive text editor, I mean one that deals only with plain ASCII characters. That kind of editor will typically delete the non-printing characters when you paste into it. Then you can re-copy from the editor and paste that into Stata. (Of course, this could be problematic if the code you need to work with actually includes non-ASCII characters.)
                    Last edited by Clyde Schechter; 02 Aug 2024, 11:57.

                    Comment


                    • #11
                      Note that daniel klein and I are conveying the same message in essence.

                      The error here is not trivial in my view. It's quite subtle and needs some understanding of exactly how Stata works.

                      Here's my analysis.


                      _N in this context is not a local macro name. It's a dataset constant, the number of observations, and sui generis.

                      Now referring to a local macro that doesn't exist is not itself a syntax error. A local macro that doesn't exist is equivalent to an empty string.

                      So in this context Stata evaluates the syntax `_N' as an empty string.

                      But the result is (given a local macro window_size that does exist and contains the text 3)

                      Code:
                       
                       forvalues i = 3 / {
                      which is illegal. Stata supplies the syntax `= ' for what you want. It means evaluate the expression following the equals sign, and then substitute the result here.

                      Comment


                      • #12
                        Clyde makes a valid point on non-printable characters. I wish to add that these are much less likely when using code delimiters to present code and output.

                        Comment

                        Working...
                        X