Announcement

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

  • programmatically recover previous executed command

    Hi all,

    Is there any way to programmatically recover previous executed command ? Any local, creturn ?

    thks,


  • #2
    the command is usually found in ereturn

    reg y x

    `e(cmdline)'

    Comment


    • #3
      What is the specific context here? Programs typically call other programs that call other programs that call ..., well you get the point. Which one do you want to recover? From where? And, what for?

      Comment


      • #4
        Thanks George,

        however `e(cmdline)' does not seem to be a macro available for all commands, only a few.

        I am engaging on some metaprogramming testings here (https://github.com/wbuchanan/stataConference2023).

        thks, anyway.



        Comment


        • #5
          Maybe you could use globals or locals to set up your command and then estimate using those. You'll be constructing a fairly sophisticated do file anyway.

          Comment


          • #6
            Hi Luis
            All official estimation commands leave behind information of the estimation command call. However, you could always try to force that with some extra programming:

            Code:
            program define getcmdline, properties(prefix) rclass
                set prefix getcmdline
                gettoken first 0 : 0, parse(": ")
                `0'
                return add
                return local cmdline `0'
            end
            Now, whenever you call on a command you could type:

            getcmdline: your command here

            And it will leave behind r(cmdline) with what you typed.
            HTH





            Comment


            • #7
              Thank you,

              gathering all knowledge you shared, I code this small snippet, that will help me, in the context of very long .do files and eternal laziness to search a variable definition in code.

              Code:
              capture program drop nota
              program define nota, rclass
              version 17.0
              gettoken prefix command : 0, parse(":")
              if ustrregexm(`"`command'"', "\{([^{}]+?)\}") == 1 {
                  local var = ustrregexs(1)
              }
              local clean0 = ustrregexrf("`command'", "\{", "")
              local clean = ustrregexrf("`clean0'", "\}", "") 
              `clean' 
              notes `var': `clean'
              end
              It just add a note, with executed command, to the variable within brackets {}.

              Code:
              . webuse auto
              (1978 automobile data)
              
              . nota: gen {price2} = price ^2
              
              . nota: gen {price3} = price ^3
              
              . nota: keep in 1/10
              
              
              
              * many , many line of codes and days later,
              *  one  can retrieve the rational behind the variables, with a simple:
              
              . notes
              
              _dta:
                1.  From Consumer Reports with permission
                2.  keep in 1/10
              
              price2:
                1.  gen price2 = price ^2
              
              price3:
                1.  gen price3 = price ^3
              hope it will be helpful for you, too, and thks for the replies

              Comment


              • #8
                I must admit I am a little confused as to how this differs from having a clear system of do-files? Instead of needing to interrogate notes; shouldn't the commands "gen price2 = price ^2" etc. be saved within a do-file for reproducibility?
                If this is a way of highlighting the most important lines of code, then again, couldn't you have a section of do-file headed e.g. "MOST IMPORTANT CODE" ?

                Apologies if I am misrepresenting your use-case. Your reference to meta-programming, and to the Stata Conference, suggests that your actual use-case is a little more complex!

                On the other hand, I do like FernandoRios 's little prefix command -- I can see that having some occasional use.

                Comment


                • #9
                  I think it could be used the same way I use my f_able command. Basically keep meta information so a program can recognize how variables were created, in case that some changes are needed to be done in an automatic simulation (that is similar how I use something like this for margins)
                  Also, This could allow having all (including the dofile) as part of the data file.

                  Comment


                  • #10
                    on David“s comments, my daily usage case, is:

                    my Stata codes are long and ill-organized ( in industry, we do not care about reproducibility ), with lots of technical debts.

                    Therefore, obviously, I always get lost on my own code and I hope this little chunk of code (#7), will save me from always scrolling it up and keep losing focus to remember what the hell that variable , out of dozens, means.

                    Code:
                    . notes price2
                    
                    price2:
                      1.  gen price2 = price ^2

                    hope, other users fell represented here.




                    Comment

                    Working...
                    X