- To return more precise statistics, because Stata sometimes truncates values in output, but not in return values.
- To reorganize data output to make it easier to read, copy, or interpret.
- To build programs in Stata.
Example 1: Basic Usage
Lets use return list on a value we have generated. Type the following commands into stata:
- 1
- 2
- 3
- 4
sysuse citytemp4.dta
tab division
return list
di `r(N)'
tab division
return list
di `r(N)'
The scalar r(N) is returned and displayed using di r(N). |
We can display these values by typing display and surrounding them with a forward and backward quote. That is, a ` and '. The ` is shared with the tilde(~) key while the ' is a single quote. It seems strange to me (I really wish Stata devised a simpler syntax, but I don't think they are planning on changing it anytime soon).
Example 2: Creating your own Table
Sometimes you may want to construct your own table. For instance, in cases where you use summarize on a large number of subsets of your data and want to retrieve a desired stastic from each. This can be a tedious, error-prone process. The solution is to construct your own table using macros and loops. This will make the process quicker and more accurate.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
forvalues num = 1/9 {
noisily summarize heatdd if division == `num'
local mean`num' `r(mean)'
}
noisily display _column(1) "Division" _column(20) "Mean"
forvalues num = 1/9 {
local div_label : label(division)`num'
noisily display _column(1) "`div_label'" _column(20) string(round(`mean`num'',0.001))
}
noisily summarize heatdd if division == `num'
local mean`num' `r(mean)'
}
noisily display _column(1) "Division" _column(20) "Mean"
forvalues num = 1/9 {
local div_label : label(division)`num'
noisily display _column(1) "`div_label'" _column(20) string(round(`mean`num'',0.001))
}
This above code is a do file which uses citytemp4.dta to produce a table of the mean of heatdd by regions coded in the division variable. To load this, type sysuse citytemp4.dta. Copy and paste the above code into a do file and try running it. The output will look as follows:
The outputted table |
There are two loops in the do file which are designated using forvalues. We loop through the values 1-9 because there are 9 geographic regions coded in the variable division. division is a numeric variable which is coded using labels: N. Eng = 1; Mid Atl = 2; E.N.C. = 3, etc.)
The First Loop
- 1
- 2
- 3
- 4
forvalues num = 1/9 {
noisily summarize heatdd if division == `num'
local mean`num' `r(mean)'
}
noisily summarize heatdd if division == `num'
local mean`num' `r(mean)'
}
In line 3 we create macros. Again, `num' will be replaced by the number the loop is currenty on (1-9). This will cause 9 macros to be created: mean1, mean2, mean3, ... , mean9. Each macro will contain the mean returned from summarize.
Line 4 closes the loop.
- 6
noisily display _column(1) "Division" _column(20) "Mean"
The Second Loop
- 8
- 9
- 10
- 11
- 12
forvalues num = 1/9 {
local div_label : label(division)`num'
noisily display _column(1) "`div_label'" _column(20) string(round(`mean`num'',0.001))
}
local div_label : label(division)`num'
noisily display _column(1) "`div_label'" _column(20) string(round(`mean`num'',0.001))
}
Each time the loop occurs, div_label is replaced by the label corresponding to the value of the loop we are on. Then it is printed in line 10 and the process occurs again.
line 10 gives us what we were always after. It prints the geographic region coded under division and its corresponding heatdd mean. string(round(`mean`num'',0.001)) is a lot to digest. It is a series of nested functions. They are performed from the inside to the outside. The following list should clear things up. Each line shows what the computer does, successively on loop 3.
string( | round( | `mean | `num' | ' | ,0.001) | ) | The computer starts with the most 'nested' thing, in this case `num'. |
string( | round( | `mean | 3' | ' | ,0.001) | ) | The computer starts in the middle, figuring out the value of the loop we are on. |
string( | round( | 6353. | 72815533980 | ,0.001) | ) | The value of mean3 is retreived. | |
string( | 6353. | 728 | ) | This value is rounded to the nearest 0.001 | |||
"6353. | 728" | The value is converted to a string so it can be outputted. This is the end result. |
Hopefully your off to a good start building output in this custom manner and saving considerable time doing so!
See Also
Great blog.you put Good stuff.All the topics were explained briefly.so quickly understand for me.I am waiting for your next fantastic blog.Thanks for sharing.Any coures related details learn...
ReplyDeleteR Programming Online Training
Download STATA 15 Full Version
ReplyDeletehttps://www.youtube.com/watch?v=3Wf1yLV6668
This Video Provides Download Links To Software STATA 15. It is Software STATA 15 Full Version. Thank You for See Video Download STATA 15 Full Version
Really very happy to say,your post is very interesting to read.I never stop myself to say something about it.You’re doing a great job.thanks a lot.
ReplyDeleteC and C++ Training Institute in chennai | C and C++ Training Institute in anna nagar | C and C++ Training Institute in omr | C and C++ Training Institute in porur | C and C++ Training Institute in tambaram | C and C++ Training Institute in velachery
it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
ReplyDeleteJava training in Chennai
Java Online training in Chennai
Java Course in Chennai
Best JAVA Training Institutes in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Training
Java Online Training
Many companies are pivoting to virtual events and More than 90% of event marketers plan to make virtual a key part of their strategy going forward. hybrid ideas and thank you note subject line
ReplyDelete