Announcement

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

  • Forval loop invalid syntax

    Hello,

    I am using forval code as below and then it says it is 'invalid syntax'. I checked that all the variables from macro with $ works fine with other codes. When I put the value of first equation without forval to check for that equation, it worked fine.
    Do you see any error in this code below? thank you.

    forval i = 1999/2017 {

    gen com_bV`i'_1=(${Pi`i'_1}-${Pi`i'-1_1})*${rg`i'_1}
    gen com_bV`i'_2=(${Pi`i'_2}-${Pi`i'-1_2})*${rg`i'_2}
    }

  • #2
    I can't see anything wrong there syntactically, assuming that the globals all exist, and we have no way of checking.

    As for style, 38 extra variables all holding constants??? What are you going to do with them?

    Comment


    • #3
      aha okay. Thank you. I don't understand what do you mean by '38 extra variables all holding constants???' I just want to see the data visually...
      Last edited by Young Hwang; 01 Apr 2019, 10:33.

      Comment


      • #4
        I see a well-hidden syntax problem in the code in post #1.

        Consider this term from your first generate command.
        Code:
        ${Pi`i'-1_1}
        After substituting 1999 for `i' it becomes
        Code:
        ${Pi1999-1_1}
        There can be no global macro Pi1999-1_1 because the minus sign is not allowed as part of a macro name. I will guess that what you want is
        Code:
        ${Pi1998_1}
        which means the term should be written
        Code:
        ${Pi`=`i'-1'_1}

        Comment


        • #5
          I don't understand what do you mean by '38 extra variables all holding constants???
          Let me ask this. What are is the value of the global macro $Pi1999_1? Is it constant, such as 42, or is it a variable name, such as bV?

          I think Nick is assuming the former, but I think you are continuing to attempt to create the the plot you described in an earlier topic.

          https://www.statalist.org/forums/for...pe-the-dataset

          I have just posted to that topic a discussion of the syntax error you encountered in post #3.

          Comment


          • #6
            #4 Very well caught by William Lisowski.

            I am indeed guessing that these global macros may hold constants. I can't be certain -- Young is telling us nothing about their dataset -- but if they do then any command of the form

            gen whatever = (constant1 - constant2) / constant3

            is just creating yet another constant as a result and holding it again and again in every observation in the dataset. Occasionally that is what you want; usually it isn't.

            Another guess -- I am full of them -- is that Young comes to Stata with much programming experience in other software. Sometimes that helps; sometimes it doesn't!

            I think we have the x-y problem here. http://xyproblem.info/ You're asking a narrow question about syntax, but more experienced Stata users are asking why are you doing this and what do you want to achieve? "I just want to see the data" is no doubt true, but doesn't provide a precise description.

            Comment


            • #7
              Thank you for the help. Yes, it is former one. and I am using panel data to see time trends with wages. I will be more specific next time.
              Last edited by Young Hwang; 01 Apr 2019, 11:25.

              Comment


              • #8
                are there supposed to be exactly like below? because when I tried this, that color changed to the black one. and as you said, i want to get Pi=1998-1,so the purpose is right.
                Thank you!
                Code:
                ${Pi`=`i'-1'_1}
                [/QUOTE]

                Comment

                Working...
                X