Stata: Hiding command lines
Asked Answered
C

4

5
. sysuse auto, clear
(1978 Automobile Data)

. di "I am getting some summary statistics for PRICE"
I am getting some summary statistics for PRICE

. su price

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
       price |        74    6165.257    2949.496       3291      15906

. 
end of do-file

I want to hide the command lines, and show only the results as follows:

I am getting some summary statistics for PRICE

        Variable |       Obs        Mean    Std. Dev.       Min        Max
    -------------+--------------------------------------------------------
           price |        74    6165.257    2949.496       3291      15906

How can I do this? Thanks.

Crochet answered 26/3, 2013 at 16:58 Comment(0)
F
5

Try this: The output text file (quiet_noise. txt) will have the one you want.

quietly {
     log using quiet_noise.log, text replace
     sysuse auto
     noisily: di "I am getting some summary statistics for PRICE"
     noisily: su price
     log close
}
Freiman answered 26/3, 2013 at 17:15 Comment(1)
Another option, if you are willing to try something a bit more fancy, is to go for the "knitr" approach to do-files, which mixes Markdown-formatted text with Stata code.Mathematics
C
11

The answer from user1493368 is correct, but writing code like that is tedious and error-prone for more complicated examples. Another answer is just to learn how to write Stata programs! Put this in a do-file editor window and run it

program myprog 
    qui sysuse auto, clear
    di "I am getting some summary statistics for PRICE"
    su price
end 

Then type interactively

myprog 

As in practice one makes lots of little mistakes, a very first line such as

capture program drop myprog 

is a good idea.

This really is prominently and well documented: start with the later chapters in [U].

Contorted answered 26/3, 2013 at 19:0 Comment(0)
F
5

Try this: The output text file (quiet_noise. txt) will have the one you want.

quietly {
     log using quiet_noise.log, text replace
     sysuse auto
     noisily: di "I am getting some summary statistics for PRICE"
     noisily: su price
     log close
}
Freiman answered 26/3, 2013 at 17:15 Comment(1)
Another option, if you are willing to try something a bit more fancy, is to go for the "knitr" approach to do-files, which mixes Markdown-formatted text with Stata code.Mathematics
S
4

Commenting Stata output, especially when you want to share your logfiles become a problem which is very well reflected in your question.

As Nick Cox nicely has explained, Writing a program to display the text is a very good idea. However, including text in a program comes at a cost i.e. you cannot use that program with other variables. For example, if you write a program to run a regression with the given variables, you cannot use that program with other variables if you comment the findings. In other words, writing comments about a particular findings will make the program less useable. As a result, you will end up writing a program for each analysis, which is not that appealing.

So what is my suggestion? Use the MarkDoc pakcage to comment your results.

In MarkDoc (ssc install markdoc) you can write comments using Markdown / HTML /LaTeX and have it exported to a dynamic document within Stata. In your example it would be as follows:

    qui log using example, replace

    sysuse auto, clear

    /***
    Writing comments in Stata logfiles
    ==================================

    I am getting some summary statistics for PRICE
    ***/

    summarize price

    qui log c
    markdoc example, replace export(pdf)

And MarkDoc will produce a PDF for you that has interpreted your comments as Markdown. In addition to pdf, you can convert the same log file to other formats such as docx, html, tex, Open Office odt, slide, and also epub.

The PDF and HTML formats will also have a syntax highlighter for Stata commands, using Statax Syntax Highlighter.

Stratfordonavon answered 10/9, 2014 at 18:54 Comment(3)
The assertion that a program cannot be used for other variables applies only to programs that wire in dependence on particular variables. It's nonsense otherwise: a competently written program makes no assumptions about variable names. Stata commands could hardly work otherwise.Contorted
I can only agree with your comment. Perhaps I was not clear enough. I pointed out one shortcoming of writing a comment in a program if we want to use that program over and over with other variables or in different occasions. In situations like that, having a comment about a particular finding would make that program unusable in other occasions. Apart from that, there is no problem with writing smaller problems.Stratfordonavon
Thanks for the clarification. I guess we are in basic agreement.Contorted
C
0

Another possibility is to run your log within a loop. When you run a loop the code is displayed once in the result window then only the output is displayed. So for instance if you do as follow:

foreach z in 1 {
    log using "myfile"
    di "I am getting some summary statistics for PRICE"
    su price
    log close
}

The content of the foreach will be displayed in the result and when the log will only keep the results of the commands.

Continue answered 20/10, 2023 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.