Announcement

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

  • Help with using local and generate command

    Dear all,
    I used the local command:

    local happy q1_5
    local sad q1_6

    etc.. I have a lot more variables but to show my problem i resrtict to these 2.
    Then I run the command:

    gen status = `happy' * 1.7+ `sad' *3

    STATA responds: invalid varname
    r(198);

    How Can i solve this problem? I am using STATA 13.1.

    Many thanks


  • #2
    Did you run your code in one go? Also, why not simply rename the variables?

    Comment


    • #3
      yes I run the codes in one go. I have so many variables and also I have interaction variables from the local command.

      Comment


      • #4
        Maybe something changed in Stata 14.2, but when I run

        Code:
        clear all
        set obs 5
        gen q1_5 = 2
        gen q1_6 = 3
        
        local happy q1_5
        local sad q1_6
        
        gen status = `happy' * 1.3 + `sad' * 3
        It works just fine? I still don't see the gain from using locals instead of renaming though...

        Comment


        • #5
          Perhaps the example in post #1 was not an actual representation of the code run but instead a sample of similar code that was meant to show the syntax used, without being an actual failing example. Maybe there are a large sequence of similar commands within a do-file, and it is not clear which command is failing.

          In that case, trying
          Code:
          set trace on
          before running the commands that fail will generate lots of output to your Results window, and will show you which command fails, and what the command looked like after substituting for the local macros. Read the output of help trace for more in-depth advice.

          Comment

          Working...
          X