Announcement

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

  • adopath :file funcx.ado not found

    I have a file named funcx.ado stored in the directory D:\ds\Statas\s17\ado\XS.
    When I execute the following commands:
    Code:
    adopath + "D:\ds\Statas\s17\ado\XS"
    do "funcx.ado"
    I always encounter the error "file funcx.ado not found." What could be the reason for this?
    Last edited by qing xuan; 27 Jul 2024, 21:47.

  • #2
    Supplementing the situation with the following command:
    Code:
    do D:\ds\Statas\s17\ado\XS\funcx.ado
    runs successfully, which is indeed quite perplexing.

    Comment


    • #3
      Just type
      Code:
      funcx
      instead of using do?

      The documentation for ado-files says this:

      When you type a command that Stata does not know, it looks in certain places for an ado-file of that name. If Stata finds it, Stata loads and executes it...
      This behaviour of searching for an ado-file only occurs when you invoke a command, not more generally. When you use do, it only looks for the file in the path you specify (or absent that, the current working directory).

      For instance,

      Code:
      . do "regress.ado"
      file regress.ado not found
      r(601);
      
      . do "/Applications/Stata/ado/base/r/regress.ado"
      
      -- runs the ado-file, output omitted --
      Last edited by Hemanshu Kumar; 27 Jul 2024, 22:28.

      Comment


      • #4
        Thank you for your help, but even though I have used adopath + "D:\ds\Statas\s17\ado\XS" to set the search path for ado commands, why are they still not being found? Has the effect of this command become ineffective, or is my understanding incorrect?

        Comment


        • #5
          They will be found when Stata searches for them, which is only when you try to invoke a program that Stata does not already have in memory.

          So if you try to type

          Code:
          funcx
          Stata looks for a program of that name, and if it is not in memory, it will search for ado-files of that name in the various ado-paths.

          However, when you execute the do command,

          Code:
          do "funcx.ado"
          Stata interprets it as needing to execute a file located in your current working directory (since you did not specify a folder), and looks only there. This is true irrespective of it being a do-file or an ado-file. It does not search the ado-paths.

          Comment


          • #6
            So, if I have funcx.ado (stored in D:\ds\Statas\s17\ado\XS) which defines an fx function, how can I use the fx function? Do I have to do "D:\ds\Statas\s17\ado\XS\funcx.ado" to use it? Or should I use do file? Maybe I haven't understood how to call a function from a file in Stata.

            Additional Information:
            When I first run the following code:
            Code:
            funcx
            It reports the following:
            Code:
            "command funcx not defined by funcx.ado"
            When I run the code a second time:
            It reports the following:
            Code:
            program fx already defined
            How should I proceed to achieve the functionality of calling the fx function as I mentioned above?
            Last edited by qing xuan; 28 Jul 2024, 03:13.

            Comment


            • #7
              It might help to show an excerpt of your funcxi.ado or funcx.ado files.

              The second error is because Stata complains when you try to define a program with a name that it already has in memory. For that reason, a program define command is almost always preceded by a capture program drop command:

              Code:
              capture program drop fx
              program define fx
              ...
              ...
              end
              note: the word define is (mostly) optional. You may have defined your program by simply typing
              Code:
              program fx
              Last edited by Hemanshu Kumar; 28 Jul 2024, 03:12.

              Comment


              • #8
                However, strangely, I have actually added "capture program drop".
                A simple example in :
                Code:
                capture program drop fx
                program define fx
                    version 15
                    syntax varname
                
                    count if missing(`varname')
                    qui scalar missing_n = r(N)
                
                    if missing_n > 0 {
                        drop if missing(`varname')
                    }
                    else{
                        display "`varname',there is no missing values"
                    }
                end

                Comment


                • #9
                  It might help to clear out memory with

                  Code:
                  program drop _all
                  or

                  Code:
                  clear all
                  and then see what errors arise.

                  Comment


                  • #10
                    Thank you, I find that if I name the ado file fc.ado, then define the fc function, and use adopath to add the path, I can directly call the fc function. However, I still do not understand if multiple functions can be written in one ado file, or if they must be written separately.

                    Comment


                    • #11
                      Your original post in #6 said the error you received was command funcx not defined by funcxi.ado, which left me confused. You have now corrected that to command funcx not defined by funcx.ado. This is a straightforward error.

                      You must have typed in

                      Code:
                      funcx
                      and, not finding a program of this name in memory, Stata searched for a do-file called funcx.ado in the ado-path. It succeeded in finding such a file. However, that file did not actually have a program called funcx defined, hence the error. Presumably the file had the program fx in it, but not funcx?

                      Comment


                      • #12
                        In #6, the "i" is a typographical error; the actual situation is that I am now providing the corrected content.

                        Comment


                        • #13
                          I still do not understand if multiple functions can be written in one ado file, or if they must be written separately.
                          Yes, multiple programs are often defined in the same ado-file. But remember that Stata will only search for and execute that ado-file when you ask it to execute a program of the same name. For that reason, usually multiple programs are defined in an ado-file only in scenarios where the other programs (e.g. fx) are themselves being called by the main program (e.g. funcx).
                          Last edited by Hemanshu Kumar; 28 Jul 2024, 03:41.

                          Comment


                          • #14
                            Now I understand, which means that in fact, an ado file has only one entry point, namely the function with the same name as the ado file. The other functions defined in the ado file are just some functional functions called by this function with the same name as the file, correct?

                            Comment


                            • #15
                              Yes, that's the usual way to construct an ado-file.

                              Comment

                              Working...
                              X