It is common that two ado-files in the same package share a lot of sub-commands (also called utility commands, functions etc.). It is bad practice to write the commands in one file first and copy and paste the sub-commands to the other ado-file. This is ad practice as updating these commands is very likely to lead to errors. This is a part of the DRY coding principle that is one of the few coding paradigms that all computer scientists agrees on.
This Gist explains how you can set this up in a Stata environment.
You should give this file a unique name if you are planning on publishing commands using this method as there will be a name conflict if someone else also have a file called mySubCommandsXYZ.do or whatever you end up calling your file.
This file includes all sub-commands that should be included in the ado-files for the commands. The commands in this file may not have the same name as any sub-command written directly in the ado-files.
In the ado-files the following line of code is used capture findfile mySubCommandsXYZ.do
. findfile
is a command that looks in the adopath folders - these are the folders where user-written commands may be installed - for a file called mySubCommandsXYZ.do.
If the file is found, then the local path to that file is saved in the return local r(fn). That local is used in run "'r(fn)'"
. That line runs the file with all the functions and saves them into temporary memory so that the command can use them.
If the file is not found, an error is thrown.
If you are planning on publishing commands using this set-up, for example at SSC, all you need to do is to include the mySubCommandsXYZ.do (or whatever you called your file) with the ado-files and help-files.