Announcement

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

  • -rename- works in a .do file but not in an .ado file

    Hi all,

    I am trying to rename some dummy variables generated by -tab , gen()-. The code works as intended when used within a .do file. However, it errors out when run within an .ado file. It is not clear to me what exactly the issue is. The error message indicates that there cannot be open parentheses, but when I remove the parentheses altogether I get a different error stating that the variable name that I am attempting to rename is ambiguous... Perhaps someone smarter than me can figure this out?

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10 shape str6 color
    "square"     "blue"  
    "round"      "blue"  
    "round"      "red"   
    "round"      "red"   
    "round"      "blue"  
    "round"      "green"
    "square"     "green"
    "square"     "green"
    "round"      "red"   
    "round"      "blue"  
    "triangular" "yellow"
    "triangular" "red"   
    end
    Code:
    * .do file works as expected, generating 7 categorical variables and renaming them sequentially
    
    local anything shape color
    local num 1
    foreach i of local anything {
       tabulate `i', generate(_qvar) nofreq
       rename (_qvar*) (z#), addnumber(`num')
       local num = `num' + `r(r)'
     }
    Code:
    * same code produces errors when used within an .ado file
    
    capture program drop test
    program test, rclass
    
    version 11
    syntax anything
    
            tokenize `anything'
           
            local num 1
            foreach i of local anything {
                tabulate `i', generate(_qvar) nofreq
                rename (_qvar*) (z#), addnumber(`num')
                local num = `num' + `r(r)'
            }
                    
    end
    
    test shape color

  • #2
    The version 11 line is causing you grief. If you drop it, or change it to version 12, it works fine. I don't happen to remember all the syntax from version 11, but whatever it was, it isn't what you are using.

    Also, I wouldn't call your ado file test, because there is already a Stata command called test.
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      In whatsnew11to12, it says

      16. Renaming groups of variables is now easy using rename's new syntax that is 100% compatible with its old syntax. You can change names, swap names, renumber indices within variable names, and more. See [D] rename group.

      So, the rename syntax you are using did not yet exist in version 11.
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        Hi Richard,

        Thank you very much! That was the answer!

        FYI, I just created a toy ado file for this example. I would never consider naming an ado file "test"

        Thanks again!

        Ariel

        Comment


        • #5
          FWIW, here is a slight variation of the program that works with Stata 11

          Code:
          program test
              
              version 11
              
              syntax varlist
              
              // make sure new variable names do not exist
              foreach stubname in TMP_INDICATOR_NAME* _qvar {
                  
                  capture unab must_not_exist : `stubname'*
                  if (_rc != 111) confirm new variable `must_not_exist' // NotReached
                  
              }
              
              // create the inidcators
              local num 0
              foreach var of local varlist {
                  tabulate `var' , generate(TMP_INDICATOR_NAME`++num') nofreq
              }
              
              // rename the indicators
              local num 0
              foreach var of varlist TMP_INDICATOR_NAME* {
                  rename `var' _qvar`++num'
              }
              
          end

          Comment


          • #6
            Thank you, Daniel!

            Comment

            Working...
            X