Announcement

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

  • How to modify user-written ado file name without running into loading error

    Hi all,

    I've been working on a user-written ado file that with its current name works perfectly fine. But wanted to change the name of the ado file -and the main program accordingly- and when I do that (without closing stata) and run the do file that calls the ado it has problems loading the ado. It tells me that a program that is also created in the ado file -but which is not the main program- has already been defined and I get the following error message in brackets "error occurred while loading xx.ado". This surprises me since before creating the program appears the following line:

    cap program drop programname

    This line should force stata to drop the program with that name before creating it again but for some reason, it doesn't.

    So, I tried closing stata, opening it again and running the do-file that calls the ado with its new name and works perfectly fine. I guess this means the problem occured when changing the name of the ado and loading it again. But is there a way of keeping this from happening, that I don't need to restart stata to load the ado-file properly?

    Thanks for your help!!

  • #2
    If I understand correctly, you start with a file "abc.ado" which has definitions for two programs - the main program "abc" and a second program "def".

    Now you edit "dog.ado" and change the name of the main program to "xyz" and save the edited file in "xyz.ado".

    Do you have
    Code:
    capture program drop def
    program define def
    in the ado file?

    If not, that is why there's an error. But that's not really the best thing to do within ado files, since if the user has created their own def program before running abc (or xyz), the user's version will be replaced, with no warning to the user.

    Instead,when you're developing and testing an ado file, after saving the edited ado file, issue
    Code:
    program drop _all
    from the Stata command line to ensure that the new version is loaded and used. That is, once xyz.ado is read and the xyz program defined, if you edit xyz.ado, the edited version won't be reloaded because the old version is still in memory.

    Comment


    • #3
      Thanks for the answer William Lisowski! You completely got my problem. But I was left thinking with something you said. My code does have the following lines:

      Code:
      capture program drop def
      
      program define def
      And you mentioned that this may not be the best thing to within ado files since it replaces an existing program with the same name. Is there a way around that? Or perhaps a way of making stata tell me when this happens?

      Comment


      • #4
        I tend to use esoteric program names that others are unlikely to be using for their programs, e.g. my gologit2 program calls gologit2_svy_check.ado
        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        StataNow Version: 19.5 MP (2 processor)

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

        Comment


        • #5
          The story behind the often used

          Code:
          capture program drop ...
          in ado-files seems to confuse many Stata users. I will try and explain.

          Say, you have written an ado-file, main.ado that reads

          Code:
          capture program drop main
          program main
              sub
          end
          
          capture program drop sub
          program sub
              ...
          end
          In the code above, the first line

          Code:
          capture program drop main
          is completely unnecessary. It is unnecessary because when you type

          Code:
          main ...
          main.ado is only called if there is not already a program main in memory. It follows that the first line in main.ado is only executed if there is not already a program main in memory. If there is no such program in memory, there is no need, whatsoever, to drop such a program from memory. In fact, the respective line will always result in error; that is not a problem because you capture that error. Moreover, although the line is useless, it does no harm.

          The potential problem is with (the less often used)

          Code:
          capture program drop sub
          and I believe this is what William had in mind. Actually, there are (at least) two problems with that line. First, it will never do what you probably intended to. You probably intended the line to drop (sub-)program sub from memory to make sure main is calling the correct sub. However, main is not really calling sub; main would not even be calling sub if there was a program, sub, in memory (or defined in sub.ado). Instead, main always calls main.sub. The latter is how sub-programs in ado-files are stored in memory.*

          Therefore, if

          Code:
          capture program drop sub
          finds a program sub in memory and drops it, it drops a program that has nothing to do with main. Do not be overly concerned about this. In the worst case scenario, sub was defined in memory but not stored in sub.ado (or elsewhere). In that case, when you run main and subsequently type

          Code:
          sub ...
          Stata will issue an error that it cannot find program sub. You will need to redefine it. Note that sub will never be redefined in main.ado because main.ado defines main.sub, not sub. Usually, there will be a sub.ado in which case Stata will simply re-load the respective program the next time you type

          Code:
          sub ...

          A (very) long story short: never use

          Code:
          capture program drop ...
          inside ado-files. It never does what you want and sometimes does something that you do not want.


          Best
          Daniel


          * It is also why you cannot call sub-programs of ado-files from outside that ado-file.
          Last edited by daniel klein; 18 Dec 2019, 15:27. Reason: a little spelling and typos (probably still many left)

          Comment


          • #6
            For some reason, I've only found these messages now. Many thanks for your explanation daniel klein!

            Comment

            Working...
            X