Announcement

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

  • -rename group- fails within my program, but when I copy/paste the exactly same rename command out of my program, it works... Why?

    Good afternoon,

    I am trying to rename a group of variables within a program, and -rename group- fails:
    Code:
    - if !missing("`generate'") rename __quantile* `generate'*
    = if !missing("myqs") rename __quantile* myqs*
    __quantile ambiguous abbreviation
    ------------------------------------------------------------------------------------------- end qtiles ---
    r(111);
    
    . des
    
    Contains data from C:\Program Files (x86)\Stata15\ado\base/a/auto.dta
      obs:            74                          1978 Automobile Data
     vars:             7                          13 Apr 2016 17:45
     size:         3,256                          (_dta has notes)
    ----------------------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    ----------------------------------------------------------------------------------------------------------
    price           int     %8.0gc                Price
    weight          int     %8.0gc                Weight (lbs.)
    __quantile0     double  %10.0g                
    __quantile_25   double  %10.0g                
    __quantile_5    double  %10.0g                
    __quantile_75   double  %10.0g                
    __quantile1     double  %10.0g                
    ----------------------------------------------------------------------------------------------------------
    Sorted by: 
         Note: Dataset has changed since last saved.
    But when I copy exactly the same -rename- command from command line and out of my program, it works:

    Code:
    . rename __quantile* myqs*
    
    . des
    
    Contains data from C:\Program Files (x86)\Stata15\ado\base/a/auto.dta
      obs:            74                          1978 Automobile Data
     vars:             7                          13 Apr 2016 17:45
     size:         3,256                          (_dta has notes)
    ----------------------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    ----------------------------------------------------------------------------------------------------------
    price           int     %8.0gc                Price
    weight          int     %8.0gc                Weight (lbs.)
    myqs0           double  %10.0g                
    myqs_25         double  %10.0g                
    myqs_5          double  %10.0g                
    myqs_75         double  %10.0g                
    myqs1           double  %10.0g                
    ----------------------------------------------------------------------------------------------------------
    Sorted by: 
         Note: Dataset has changed since last saved.
    Has anybody seen anything like this before, and does anybody know what is going on here and how to fix the problem?


  • #2
    Nevermind, I figured it out.

    The problem is that I have defined my program to be for Stata Version 11, and -rename group- arrived some time later, maybe in Stata 13 or 14.

    So the -rename- does not work inside my program because as of Stata 11 -rename group- does not exist, and it works outside because from command line I am operating under Stata 15.

    Comment


    • #3
      I changed in my program
      Code:
       
       if !missing("`generate'") rename __quantile* `generate'*
      to

      Code:
      if !missing("`generate'") renpfix __quantile `generate'
      and it works now under Stata 11 too.


      Comment


      • #4
        Just for reference, the same problem is discussed starting in #15 of this thread.. The new rename command was introduced in Stata 12.

        It is worth mentioning that version control usually does not undo improvements made to Stata. I would not have expected rename to behave differently under version control. It is also contrary to the statement that

        Second—and this is the important part—even if a program says version 15.0 further down, that does not mean it cannot exploit the features from later versions. For instance, a version 6.0 program can be used with sortpreserve, a feature added in Stata release 7. You do not have to change the version statement at the top of an ado-file to exploit a modern feature.
        (What is the difference between a release marker and a version number in an ado-file?)

        Comment


        • #5
          Originally posted by Joro Kolev View Post
          I changed in my program to

          Code:
          if !missing("`generate'") renpfix __quantile `generate'
          Interestingly, renpfix has a bug that does not conform to Stata's (at least implicit) rule: if a command produces an error, the data should be left unchanged. Now try

          Code:
          sysuse auto
          generate FOOpg = 42
          renpfix m FOO
          and look at the variable that was formally called make and compare with the variable that is still called mpg ...

          Comment


          • #6
            Now that you mention it, Daniel, I recalled that at this thread here: https://www.statalist.org/forums/for...d-to-not-occur
            Ulrich Kohler (#15) and Nick Cox (#19) also explained to me that Version control is not a time machine, and in the words of Nick "Version control is not a time machine. There aren't old Statas wrapped in your new(ish) Stata. Version control just does what is documented: restore old behaviour in particular respects documented. But to be fair: it's hard to explain what version control does do (and does not do). I thought it could be explained in about a paragraph until I tried it. https://www.stata.com/support/faqs/programming/community-contributed-programs-and-stata-version/"

            My guess is that on this particular occasion the following thing is happening, taken from the reference you gave https://www.stata.com/support/faqs/p...rsion-number/:
            " What the version statement says is, “in the case of a change in the interpretation of a statement, use the interpretation of the version specified”."

            I guess this is what is happening here, in Version 11 -rename- meant rename a single variable.

            Originally posted by daniel klein View Post
            Just for reference, the same problem is discussed starting in #15 of this thread.. The new rename command was introduced in Stata 12.

            It is worth mentioning that version control usually does not undo improvements made to Stata. I would not have expected rename to behave differently under version control. It is also contrary to the statement that


            (What is the difference between a release marker and a version number in an ado-file?)

            Comment


            • #7
              Yes, this is interesting, when you try -rename- on your example, it generates an error and does not change the data at all.

              On the other hand, -renpfix- changes one variable that it can change, generates an error, and leaves the variable for which there is conflict unchanged...

              I was not aware of the very reasonable rule you mention. I guess the times of Stata 11 were more Wild West times, and Stata programmers were just rolling with the danger :-). Nowadays probably more effort is spent in making sure that the users cannot hurt themselves.


              Originally posted by daniel klein View Post

              Interestingly, renpfix has a bug that does not conform to Stata's (at least implicit) rule: if a command produces an error, the data should be left unchanged. Now try

              Code:
              sysuse auto
              generate FOOpg = 42
              renpfix m FOO
              and look at the variable that was formally called make and compare with the variable that is still called mpg ...

              Comment


              • #8
                Originally posted by Joro Kolev View Post
                My guess is that on this particular occasion the following thing is happening, taken from the reference you gave https://www.stata.com/support/faqs/p...rsion-number/:
                " What the version statement says is, “in the case of a change in the interpretation of a statement, use the interpretation of the version specified”."
                I see the point. However, Stata is perfectly happy to attach one value label to multiple variables when I type

                Code:
                version 9 : label values varlist lblname
                This syntax has been introduced in Stata 10. Stata 10 made label an ado-file, moving the built-in label from Stata 9 to _label; this is similar to rename, where the built-in rename has been moved to _rename in Stata 12.

                I would not call this a bug and I see why StataCorp would not spend any time on it: it does not break old code. However, it is inconsistent behavior.

                Comment


                • #9
                  Originally posted by Joro Kolev View Post
                  I was not aware of the very reasonable rule you mention. I guess the times of Stata 11 were more Wild West times,
                  renpfix is actually around at least since Stata 6.


                  Originally posted by Joro Kolev View Post
                  Nowadays probably more effort is spent in making sure that the users cannot hurt themselves.
                  I believe that was the philosopher very early on; otherwise, they would not have introduced version so early (around the time of Stata 3, I think). It is quite rather rare that you will find a behavior such as that of renpfix.

                  Comment


                  • #10
                    I can give you another example of where newer behaviour does not work under older version control.
                    Code:
                    program define ifto3lines
                    version 7
                    if 1==1 dis "I did it on one line"
                    end
                    
                    . ifto3lines
                    { required
                    r(100);
                    On the other hand

                    Code:
                    prog drop ifto3lines
                    program define ifto3lines
                    version 11
                    if 1==1 dis "I did it on one line" 
                    end
                    
                    . ifto3lines
                    I did it on one line
                    works.

                    I guess overall it is hard to guess what will work in which way. My approach to doing things in Stata is mostly trial and error.




                    Originally posted by daniel klein View Post

                    renpfix is actually around at least since Stata 6.




                    I believe that was the philosopher very early on; otherwise, they would not have introduced version so early (around the time of Stata 3, I think). It is quite rather rare that you will find a behavior such as that of renpfix.

                    Comment

                    Working...
                    X