Tricks in Stata Anke Huss Generating „automatic“ tables in a do-file.

Slides:



Advertisements
Similar presentations
Stata as a Data Entry Management Tool
Advertisements

Structured ASIC Xcellence Framescript A case study demonstrating the power of Framescript to automate the generation of a Data Book or Catalogue.
Basics of Biostatistics for Health Research Session 2 – February 14 th, 2013 Dr. Scott Patten, Professor of Epidemiology Department of Community Health.
Using Excel Biostatistics 212 Lecture 4. Housekeeping Questions about Lab 3? –replace vs. recode Final Project Dataset! –“Housekeeping” commands vs. data.
Using Excel Biostatistics 212 Lecture 4. Housekeeping Finish Lab 2 today and/or start Lab 3 Mac Addendum Copying and pasting from Stata.
Stata and logit recap. Topics Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with.
RESEARCH WORKFLOW USING STATA How to Be an Effective Researcher CCPR Workshop.
Excel Tutorial 6 Managing Multiple Worksheets and Workbooks
Microsoft Excel 2010 ® ® Tutorial 6: Managing Multiple Worksheets and Workbooks.
Tutorial 6: Managing Multiple Worksheets and Workbooks
I NTRO TO S TATA James Ng Center for Digital Scholarship Hesburgh Libraries.
Using Excel to Analyze OSAT Results Download OSAT results from the internet Analyze results with formulae Create blank calculation pages Filter and copy.
Computing for Research I Spring 2011 Primary Instructor: Elizabeth Garrett-Mayer Stata Programming February 28.
Computing for Research I Spring 2013 Primary Instructor: Elizabeth Garrett-Mayer Stata Programming February 21.
STATA TUTORIAL: LAB STATA windows  The command window  The viewer/results window  The review of commands window  The variable window.
XP New Perspectives on Microsoft Office Excel 2003, Second Edition- Tutorial 6 1 Microsoft Office Excel 2003 Tutorial 6 – Working With Multiple Worksheets.
Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.
Stata Introduction Sociology 229A, Class 2 Copyright © 2008 by Evan Schofer Do not copy or distribute without permission.
Introduction to Spreadsheets Microsoft Excel. What is a spreadsheet? Enter data. Analyze data. Make graphs.
Qualtrics 360 Peer Review Survey Instructions
Getting Started with your data
STATA User Group September 2007 Shuk-Li Man and Hannah Evans.
Stata 12 Merging Guide Nathan Favero Texas A&M University October 19, 2012.
1 CCPR Computing Services Workshop: Introduction to Stata June, 2006.
Air Quality System Precision and Accuracy Data Transaction Generator (AQSP&A) Training Session.
Stata Workshop #1 Chiu-Hsieh (Paul) Hsu Associate Professor College of Public Health
Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research.
Lesson No:9 MS-Word Tools, Mail Merge and working with Tables CHBT-01 Basic Micro process & Computer Operation.
Harvard-MIT Data Center (HMDC)
MS Word – Mail Merge Basic Steps Create Letter/Labels general information Create Excel File with variable Data Link Files through Mail Merge in Word Print.
API-208: Stata Review Session Daniel Yew Mao Lim Harvard University Spring 2013.
Experiences with multiple propensity score matching Jan Hagemejer & Joanna Tyrowicz University of Warsaw & National Bank of Poland.
Key Data Management Tasks in Stata
STATA Mini Course Fall 2015 Jane Leber Herr Littauer 113 1Stata Mini Course – Spring 2015.
Organizing a project, making a table Biostatistics 212 Lecture 7.
Organizing a project, making a table Biostatistics 212 Session 5.
Being Productive with Stata and VA Data Give me six hours to chop down a tree and I will spend the first four sharpening the axe. --Abraham Lincoln Todd.
Organizing a project, making a table Biostatistics 212 Lecture 7.
Created: by Paik S Tan Date : November 2 nd 2009.
Introduction to Enterprise Guide Jennifer Schmidt Rhonda Ellis Cassandra Hall.
SW318 Social Work Statistics Slide 1 Get ready to work on practice problems 1. Create a directory and subdirectory on your computer named C:\StudentData\SW318_Spring_2004.
Microsoft Excel 2003 Illustrated Complete Data with Other Programs Exchanging.
STATA for S-052 M. Shane Tutwiler Your Friendly S-040 Lecturer William Johnston IT Services Harvard Graduate School of Education.
Dec-15H.S.1 Stata 8, Programing Hein Stigum Presentation, data and programs at:
Basics of Biostatistics for Health Research Session 1 – February 7 th, 2013 Dr. Scott Patten, Professor of Epidemiology Department of Community Health.
Comparison of different output options from Stata
Today Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with Stata – Estimation – GOF.
Quick Videos: A tutorial on creating reports. Select a report and click this to view it. Select a report and click this to change it. Select a report and.
Stata Review Session Economics 1018 Abby Williamson and Hongyi Li November 17, 2006.
Topics Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with Stata – Estimation –
Mar-16H.S.1 Error check in data Hein Stigum Presentation, data and programs at:
Stata: Getting Starting and Being Productive with VA Data Give me six hours to chop down a tree and I will spend the first four sharpening the axe. --Abraham.
Excel Introduction to computers. Excel 2007 Starting the Excel program.
Stata – be the master Stata. “After I have run my standard commands, what can I do to make my model better (and understand better what is going on)?”
Ec 2390: Section 1 Useful STATA commands Jack Willis September 14th, 2015.
April 9 th, Saturday PDP N° Malik Koné. I. Cells A. The Concept B. Properties, Types and Formats II. Spreadsheets A. Spreadsheet’s Elements B. Selection.
Topics Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with Stata – Estimation –
Outline for today 1. Advice for your literature review (due next Monday) 2. Announcement about Cleveland International Film Festival next week and an optional.
MS-EXCEL PART 2.
Microsoft Office Illustrated
Tutorial 6: Managing Multiple Worksheets and Workbooks
Econometrics 704 Emilio Cuilty
ECONOMETRICS ii – spring 2018
Illinois Goal Assessment Program
STATA User Group September 2007
Stata Basic Course Lab 2.
Presentation, data and programs at:
TransCAD Working with Matrices 2019/4/29.
Econ 463-Recitation 1 STATA
Two Issues on Remote Data Access
Presentation transcript:

Tricks in Stata Anke Huss Generating „automatic“ tables in a do-file

Why programming tables? It‘s much more writing in the do-file! BUT: once you have done it, the next one will be faster (copy & paste...) No more troubles with updates of your data No more copying mistakes, because Stata does it for you

Caerphilly castle Used data: Caerphilly Prospective study (CAPS) download at: essentialmedstats/datasets.htm

Basic idea Use the Stata data sheet for your table- to-be

illn MI diabetes %

Stored results in r() and e() Use stored results usually from r-class: results after general commands such as summarize are saved in r() and generally must be used before executing more commands. For an overview type: return list e-class: results from estimation commands (regress/logictic…) are saved in e() until the next model is fitted. Overview: ereturn list

Steps 1.DESIGN TABLE FIRST: what do I want my table to look like? 2.generate a new variable for each column 3.replace cell with number of interest 4.use „outsheet“ to write your new variables in text/ excel file

Example 1 1. DESIGN FIRST: what do I want my table to look like? E.g.: Illness % Myocardial inf diabetes 1.85

Example 1 2. Generate a new variable for each column gen str illness = ““ gen percent =. Illness %

Example 1 3. Replace cell with contents/ number of interest: first column sort id replace illness = “myocardial inf“ in 1 replace illness = “diabetes“ in 2 Illness % Myocardial inf diabetes

Example 1 3. Replace cell with contents/ number of interest: second column sum mi sort id replace percent = r(mean)*100 in 1 sum diabetes sort id replace percent = r(mean)*100 in 2 format percent %9.2f Illness % Myocardial inf diabetes 1.85

Example 1 4. use „outsheet“ to write your new variables in text/ excel file outsheet illness percent in 1/2 using textres/illns.txt For further *comment 1: this works only if you have set STATA to work in a specific STATA folder. Eg: cd "d:/Statistisches/automatic_tables/STATA„ *comment 2: you can also export as excel file (*.xls), but automatic import of new textfile lets graphics survive...

Example 1 *Alternative way to do the same: program a small loop: gen str name = "" gen percent =. local i = 1 foreach var of varlist mi diabetes { replace name = “`var'“ in `i' sum `var' sort id replace percent = r(mean)*100 in `i' local i = `i' + 1 } format percent %9.2f

Example 2 1. DESIGN TABLE FIRST: Categorypercent underweight 4.20 normal32.03 overweight51.29 obese12.49

Example 2 2. Generate a new variable for each column gen str category = "" gen percent =. Categorypercent

Example 2 3. Replace cell with contents/ number of interest: first column sort id replace category = "underweight" in 1 replace category = "normal" in 2 replace category = "overweight" in 3 replace category = "obese" in 4 Categorypercent underweight normal Overweight obese

Example 2 3. Replace cell with numbers: second column ta bmicat, gen (bminew) *4 lines with percentages *4 variables with ending in numbers from 1 to LOOP! forvalues i = 1/4 { sum bminew`i' sort id replace percent = r(mean)*100 in `i' } format percent %9.2f Categorypercent underweight 4.20 normal32.03 Overweight51.29 obese12.49

Example 2 4. Outsheet...same as in example 1

Less writing... label list bmicat capture drop percent category bminew* ta bmicat, gen (bminew) gen category =. gen percent =. forvalues i = 1/4 { sum bminew`i' sort id replace category = `i' in `i' replace percent = r(mean)*100 in `i' } label values category bmicat format percent %9.2f

Example 3 1. THINK FIRST: table after logistic reg. Myocardial infarctionORucilcipval Current smoking (+ age) Current smoking(+ age + bmi)

Example 3 2. Generate a new variable for each column gen str currsmok = "" gen OR =. gen uci =. gen lci =. gen pval =.

Example 3 3. Replace cell with contents/ number of interest: first column sort id replace currentsm = "current smoking" in 1 replace currentsm = "current smoking + age" in 2 replace currentsm = "current smoking + age + bmi" in 3

Example 3 3. Replace cell with numbers: second column logistic mi cursmoke sort id replace OR = exp(_b[cursmoke]) in 1 replace lci = exp(_b[cursmoke] *_se[cursmoke]) in 1 replace uci = exp(_b[cursmoke] *_se[cursmoke]) in 1 est store A logistic mi est store B lrtest A B sort id replace pval = r(p) in 1... In lines 2 and 3

Example 3 4. outsheet...as in example 1

Resulting table Myocardial infarctionORucilcipval Current smoking e -06 Current smoking (+ age) Current smoking(+ age + bmi)

Other way to save results after estimation commands Use the statsby command: eg: statsby "logistic mi diabetes smoking" _b _se, saving (D:\Statistisches\automatic_tables\STATA\data\caerphillysta tsby.dta) replace Statsby will collapse your dataset! Store results in a new dataset and open the original file again. Rerun " statsby " with next variables and append data to first stored results.