SIDM4 Producing Tables of Stata Results and Stata Loops 2017

SIDM4 Producing Tables of Stata Results and Stata Loops 2017

SIDM 4: Publication quality tables, loops in Stata and Outputting results Publication quality tables, loops and outputting results Learn an efficient way to produce publication quality tables from Stata & save loads of time (using Excel). Learn simple loops in Stata & save more time. For sophisticated users, there are ways to automate the extraction of statistical results which can save time, when there is considerable repetition. Recap on preceding workshops, SIDM1, SIDM2 and SIDM3. These covered finding your way around Stata, useful basic commands, good house-keeping (such as importance of keeping a do file of commands), and getting your data into an appropriate format for analysis. This includes the need to check what you are doing, as you go along, and to label data appropriately. This was extended to include merging datasets, and restructuring datasets. Learning objectives of this Session SIDM4 The easiest way to produce tables from Stata output is probably to cut and paste from the output window into Excel, copying data in table format. This guide teaches you quick ways within Excel to create good-looking tables. Loops are a way of repeating some Stata coding, so that it runs on different variables/ values. Especially when you need to go back and amend code, so this can save time compare to cut and pasting. Relatively simple loops are the limit to which automation can help save time for many users. For sophisticated users/ those duplicating very many similar analyses: there are higher levels of automation for extracting regression coefficients and similar. Contents 1. Producing quality tables from Stata output using excel ................................................................. 2 Student Exercises: Producing tables in Excel from Stata output ........................................................ 3 2. Loops in Stata: repeatedly running code on different variables/ with different values ................. 3 Student Exercises on loops: ................................................................................................................ 6 3. Accessing saved results of analyses for use in other commands – useful with very many repetitions ............................................................................................................................................... 7 Student Exercises on extracting data saved by Stata commands: ...................................................... 8 4. Outputting Data from Stata ............................................................................................................ 9 SDM=Stata Data Management.doc Hilary Watt SIDM=Stata Introduction and Data Management.doc workshops SCCS=Stata Commands Crib Sheet.xls 4.1 SIDM 4: Publication quality tables, loops in Stata and Outputting results 1. Producing quality tables from Stata output using excel Select the table required from Stata output window, being sure to select complete rows. Right click within Stata, and select “copy as table”. Move to Excel and paste. Check that the data is reasonably well in columns – if not try again. (Note that copying as a picture and pasting into word is useful if you just want a record of certain parts of the output, perhaps adding graphs in between, without being able to manipulate it). This web example pastes into word, but you can use the same method to copy into excel: http://www.ats.ucla.edu/stat/stata/faq/outgraph.htm Excel tables, efficient methods to create these: In excel, I often write confidence intervals, surrounded by brackets, for example: 23.4 (19.3, 27.5) - all in one excel cell. It is straightforward to do this, referring to data in other cells, and then copy down/ across to get different rows/ columns of the table. For example, if the excel cells D4 & E4 contain these values: D4=23.346, E4=43.22, then a cell containing: =“(“&D4&“, “&E4&”)” contains various parts separately by & (&=concatenate, that is write one thing after the other): =“(“ shows ( &D4 shows 23.346 &“, “ shows , Remember to include any required spaces in quotes, such as after comma &E4 shows 43.22 &“)” shows ) resulting in (23.346, 43.22) when =“(“&D4&“, “&E4&”)” is written in Excel cell. By referring to data contained in cells, such as D4 and E4, copying this command down will automatically refer to data in cells below. Copying right refers to cells to the right. It is usually necessary to round off numbers, to avoid loads of decimal places in the output. When D4=23.346, then round(D4,1)=23.3 (to 1 decimal place) round(D4,0)=23 (to nearest whole number) round(D4,2)=23.35 (to 2 decimal places) =“(“& round(D4,1)& “, “&round(E4,1),”)” in an Excel cell will show: (23.3, 43.2) An alternative to the round Excel command is to use the text Excel command. This has the advantage of always giving the same number of decimal places, even when the last decimal place is 0. When D4=23.346, then text(D4,”0.0”)=23.3 (to 1 decimal place) text(D4,”0”)==23 (to nearest whole number) text(D4,”0.00”)=23.35 (to 2 decimal places) SDM=Stata Data Management.doc Hilary Watt SIDM=Stata Introduction and Data Management.doc workshops SCCS=Stata Commands Crib Sheet.xls 4.2 SIDM 4: Publication quality tables, loops in Stata and Outputting results See producing tables in excel by cutting and pasting from stata output.xls It is easy to then cut and paste from Excel into a Word document. More details on concatenating, in case you have not understood so far: https://support.office.com/en-us/article/CONCAT-function-9b1a9a3f-94ff-41af-9736- 694cbd6b4ca2?ui=en-US&rs=en-US&ad=US https://support.office.com/en-us/article/CONCATENATE-function-8f8ae884-2ca8-4f7a-b093- 75d702bea31d ) Student Exercises: Producing tables in Excel from Stata output Use dataset ihddata3.dta. a) Summarise the data for age, bmival and nummeds. Copy this data into Excel from the output window (select complete rows then right click to copy as a table, then paste into Excel). Now produce a table, which contains 4 columns as follows: Col 1 – variable name of interest with units (write these in yourself) Col 2 - the number of observations with non-missing data Col 3- mean (SD), i.e. mean with the SD in brackets in the same cell (concatenate using & symbols) Col 4 – [min – max], i.e. min & max values, in square brackets around, with “–“ between b) Now add extra lines to the table, with columns 1 and 2 as above, for data: doctor diagnosed diabetes (dm1), presence of chronic pain (cpain) and presence of CVD (cvd1). Remember to check how these are coded. Col 3 should now contain number with each condition (and percentage with each condition in brackets). (col 4 is empty). c) Now add extra lines to the table with numbers and percentages by smoking group, with a line for each smoking category. Also add extra lines with numbers and percentages by BMI category. You can then cut and paste this table into word. It is easiest to change the presentation in Excel beforehand, if desired. 2. Loops in Stata: repeatedly running code on different variables/ with different values The most widely used loops in Stata are the foreach loop, which loop over different variables names (out of a list of variables) and the forvalues loop (which loops over numerical values, either SDM=Stata Data Management.doc Hilary Watt SIDM=Stata Introduction and Data Management.doc workshops SCCS=Stata Commands Crib Sheet.xls 4.3 SIDM 4: Publication quality tables, loops in Stata and Outputting results consecutive numbers, or else numbers which increase by a specific set value). The syntax is as follows: An example is: foreach xxx in var1 var2 var3 { // list of variables to loop over, line ends with open bracket { replace `xxx'=. if `xxx'==999 // commands referring to `xxx’ – note 2 different types of quotes } // this close bracket goes on its own line. This performs the following commands, or more generally it repeated all the command lines which are enclosed (though on separate lines) between the foreach brackets { } replace var1=. If var1==999 // first time around the loop, `xxx’ replaced by var1 replace var2=. If var2==999 // second time around the loop, `xxx’ replaced by var2 replace var3=. If var3==999 // third time around the loop, `xxx’ replaced by var3 Since only these 3 variables are listed in the foreach command, Stata moves on to the following commands lines after this. There can be many commands inside the loop – using `xxx’ to refer to each of those variables that are being looped over in turn. Note the use of different single quotes at different sides of `xxx’, i.e. starting with a single quote ` from far left key on standard keyboards, quote sloping top left to bottom right. Then after xxx write ‘ a single quote taken from right hand side of standard keyboards, which either looks vertical or else slopes from bottom left to top right. Note: xxx in the foreach statement can be replaced with any other words/letters that you like. The same word/letters in then referred to within the loop in single quotes. Local constant values The local command can be used to store a constant value that you can then refer to local k=12 This sets k=12, and stores this as a constant within Stata, though only temporarily. local k=12 // this needs to be run in a do file at the same time as the following commands di `k’ // this displays the value of k, which is set temporarily to 12. gen newvar=var12+`k’ // this creates a new variable “newvar” by adding 12 to var12 local k=`k’+1 // this adds 1 to k – note use of single quote on right hand but not on left hand side!! Note that for this to work, you

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us