Announcement

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

  • Example If Program command throwing up an error of ‘2’ invalid name error

    Hi

    I am new in using the program command option. I was checking the power program and i am getting some errors. Can anyone help me why i am getting this errors?

    program power
    if ‘2’>0 {
    generate z=‘1’^‘2’
    label variable z "‘1’^‘2’"
    }
    else if ‘2’==0 {
    generate z=log(‘1’)
    label variable z "log(‘1’)"
    }
    else {
    generate z=-(‘1’^(‘2’))
    label variable z "-‘1’^(‘2’)"
    }
    end

    Then i generated some random values for age and ran the following command

    power age 2

    I am getting an ‘2’ invalid name error.

    Can some one tell me how to fix this?

    Thanks
    Veeresh



  • #2
    Veeresh,

    It looks like you are using the wrong leading quote for the macro 2. It should be `2' which uses the quote that is below the tilde (~) key on the upper left of your keyboard, also known to the French as an accent grave.

    Regards,
    Joe

    Comment


    • #3
      Joe,

      I changed the program as per your suggestions but still the error persists. Let me know if there is anything else i can do here?

      program power
      if `2’>0 {
      generate z=`1’^`2’
      label variable z "`1’^`2’"
      }
      else if `2’==0 {
      generate z=log(`1’)
      label variable z "log(`1’)"
      }
      else {
      generate z=-(`1’^(`2’))
      label variable z "-`1’^(`2’)"
      }
      end

      Thanks
      Veeresh

      Comment


      • #4
        Vereesh,

        Upon closer inspection, I see that the trailing quote is also incorrect. I'm not sure which one you used, but the correct one is the single quote that is between the colon/semicolon key and the Enter key. It has a double quote when you shift.

        Here is the correct version:
        Code:
        program power
        if `2'>0 {
        generate z=`1'^`2'
        label variable z "`1'^`2'"
        }
        else if `2'==0 {
        generate z=log(`1')
        label variable z "log(`1')"
        }
        else {
        generate z=-(`1'^(`2'))
        label variable z "-`1'^(`2')"
        }
        end
        Regards,
        Joe

        Comment


        • #5
          Awesome Joe!!! Thanks a lot

          Comment


          • #6
            As keyboards differ around the world, so too you may need to look in different corners for these keys.

            Comment


            • #7
              Hahaa!! They should probably standardize the keyboards!! Actually I didn't type the code, I just copy pasted from the stata help file and was not sure why it was throwing up error. Too many eyeballs are really helpful!!

              Comment


              • #8
                I was actually trying to use this program to write a code for regressing for only certain observations like if qualifiers(say if foreign==0). I have a lot of regressions and code and so i didn't want to put the if qualifier behind every line of code in between { }. After running the code i realized that if at the top of the program is not helping it to function as if qualifier. Is there a way i can restrict the observations like if qualifier at the top of the program?


                program power if `2'==0 { regress mpg mass } end power foreign 2


                Comment


                • #9
                  Here is the code:

                  program power if `2'==0 {
                  regress mpg mass
                  }
                  end

                  power foreign 2

                  Comment


                  • #10
                    Veeresh,

                    It sounds like what you want is this:

                    Code:
                    program power
                    regress mpg mass if `2'==0
                    end
                    However, you should also check out the syntax command which allows subsetting of observations using the Stata in qualifier. For example:

                    Code:
                    program test
                    syntax varlist [if] [in]
                    regress `varlist' `if' `in'
                    end
                    
                    . test a b c if z==0
                    See also the marksample command which provides additional flexibility.

                    Regards,
                    Joe

                    Comment

                    Working...
                    X