Announcement

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

  • Differences between command and do-file

    Hi, I am a beginner in Stata.
    Recently, I am suffering from some errors that happen only on do-file.
    For instance,
    Code:
    sysuse auto, clear
    generate lprice = log(price)
    preserve
    collapse (max) max_lprice = lprice max_mpg = mpg (iqr) iqr_lprice = lprice iqr_mpg = mpg if !missing(rep78), by(rep78)
    sort rep78
    restore
    This code works very well if I use command window line-by-line, but does not work on do-file.
    (The code above is cited from "An introduction to Stata programming, CF Baum")

    I have found a lot of same kind of problems.
    So, My question is in a sentence that "Is there any difference between do and command window?"

    Thank you for your time to read this question.

  • #2
    I ran that code without problem from the do-file editor. So what exact error message did you get?
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      My guess is that Minchul ran

      Code:
      ...
      preserve
      collapse ...
      from the do-file and expected to find the collapsed dataset. Minchul probably did not know that preserve will automatically restore the original dataset when the respective session, here: the do-file, ends.

      To answer the question: execution of a do-file is considered a session that starts with do-ing (or run-ing) the do-file and end when the do-file ends; the session in which you use the command line starts when you open Stata and ends when you exit Stata.

      Comment


      • #4
        Dear Maarten and Daniel,

        I found the fact that the preserve and restore should be ran together and same kind of problems can be solved by run the code with other lines.
        In a sentence, the reason why I cannot get the result was that I ran the do-file line-by-line.
        Sorry, my very rudimentary mistake and thank you for your response.

        Comment

        Working...
        X