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

  • return clear

    I noticed something that may or may not be a bug (although likely just me making some sort of mistake or not understanding the command's purpose):

    return clear does not clear r-class results. According to the documentation, "return clear clears the return() class" and, indeed, ereturn clear does clear the e-class results:

    sysuse auto, clear
    quietly summarize mpg
    return clear
    return list
    quietly regress price mpg displacement
    ereturn clear
    ereturn list
    Anyway, again---probably something I'm doing wrong (I've almost posted on this forum a thousand times, but in the end realised my own mistake!). But if anyone has an answer, I'd greatly appreciate it---I'd like to use the command + capture in a program to return an empty macro if a result doesn't exist (currently I'm just running an unrelated r-class command as a work-around).


  • #2
    I can reproduce the problem in Stata 11.2 and 12. I agree that this seems to be a bug.

    Unfortunately, from your description, I do not get what you want to do. Returning empty locals does not lead to error and, thus, capture is not needed:

    program foo , rclass
        return local foo
    sysuse auto , clear
    quietly summarize mpg
    return list
    Note that by declaring the program rclass, any previous r() results are cleared.



    • #3
      Thanks for your answer. I really appreciate it.

      Sorry for not being clearer about what I wanted to do---I just mentioned it for context.

      You are right, returning empty locals does not lead to an error. What I wanted to do was clear rclass (in a non-rclass program) and then run an rclass command on a variable which may or may not exist---hence capture. I would then call `r(something)', and that would be empty if the variable did not exist. If I don't clear rclass, however, it might contain previous rclass results.

      Anyway, obviously there are a million ways to do this, and what I've just described is undoubtedly not the best way. I just mentioned it because that's what I was trying to do when I discovered that return clear didn't work!

      Thank you again for your help.


      • #4
        Originally posted by Erin Hengel View Post
        What I wanted to do was [...] run an rclass command [...] I would then call `r(something)', and that would be empty if the variable did not exist. If I don't clear rclass, however, it might contain previous rclass results.
        It most likely would not. As you see from my example above, calling an r-class program will clear previously existing r() results in any case, whether it returns new results or not, whether it exits with error or not. Here is an example

        sysuse auto , clear
        summarize mpg
        capture noisily tabulate foo
            /* error as -foo- does not exist */
        return list
            /* despite error still no r() results left */
        There are ways of preventing this and preserve previous r() results, so you are right, there is no guarantee that r() will be empty.

        In your case it sounds like it would be easier (and faster) to code

        capture confirm variable myvar
        if !(_rc) {
            /* variable myvar exists */
            command myvar
            ... `r(something)'
        else {
        All the best
        Last edited by daniel klein; 17 Sep 2015, 04:44.


        • #5
          Thanks Daniel.

          You are right. Thanks for the tip.