I am looking through lots of Stata documentation and still having a hard time finding simple examples of some basic tasks.

One item that is particularly difficult to understand is how to store the results of certain operations in variables (in the programming sense, not a field/column) so that I can compare them against one another. And I'm not talking about statistical models, for which I might use something like estimates.

Here's an example from the world of R, in which I store the means of groups of two fields within the same number of variables (again, in the programming sense):

What you are reaching for could be quite different, e.g. using local macros or scalars or using Mata. There are several ways of doing it in Stata, just as there will be in R.

As for documentation, there is no real substitute for reading the manuals, starting with [U]. You can waste a lot of time Googling, as there are many very limited or fragmentary tutorials on the internet. Mostly they support what the authors intended, but a fuller understanding is only possible through systematic reading.

I’m no stranger to documentation. In my opinion, a several hundred page litany of commands is one piece of the puzzle, but certainly shouldn’t qualify as complete. Based on my reading, the Stata manuals lack any sort of flow and fail to provide context, making them generally inaccessible to the “naive”.
– duncanragerSep 28 '18 at 13:59

2

Oddly enough, that's my impression of R documentation. The "official" Stata documentation is of the order of 10000 pages; it is kind of hard to know which 100 ... 200 ... 500 ... 1000 pages anyone needs most and in what order. Muenchen and Hilbe wrote a book on R for Stata users. The opposite book is harder to imagine, and certainly is not on my to do list, although I have tried hard to explain bits of Stata. In essence, Stata is not a programming language, but a programmable statistical program, anyone wanting something else will be puzzled at best.
– Nick CoxSep 28 '18 at 14:09

Mata is more like mainstream programming languages (although in programming the mainstream is oddly always where the observer is).
– Nick CoxSep 28 '18 at 14:12

I disagree. Stata's ado language is a programming language. But it is statistical not a general one. the fact that it is not by nature object-oriented does not mean it is not a programming language. Mata is inherently object-oriented but again it is a matrix language not a general programming one.
– Pearly SpencerSep 28 '18 at 16:15

@Pearly Spencer Same message, different boundaries. I find that there is always some criterion that people can think up that makes Stata not a programming language. It's always something for which there is a much better language.
– Nick CoxSep 28 '18 at 16:19

I am not an expert in Stata (I mostly work in python and dabble in R), but this code should work. collapse in stata is similar to group_by in R. After collapsing the data, I generate the difference variable, and then keep only that variable in my dataframe. Finally, I save the file as temp (and replace any other file named temp.dta in the cd).

This is what egen does with its associated mean() function under the hood. The advantage of "manually" doing this, is that you can combine the round() and abs() functions to round and take the absolute value of the difference respectively in one step.

Helpful, but rounding to multiples of a decimal fraction isn't an especially good way to approximate a display format. This is now discussed directly in stata-journal.com/article.html?article=dm0095 but it follows from the fact that most decimal fractions (multiples of 0.1, 0.01, 0.001, etc.) can't be held exactly in binary.
– Nick CoxSep 28 '18 at 8:59

I agree, but depending on the use case it can often be a more than adequate approximation.
– Pearly SpencerSep 28 '18 at 10:02