A Comprehensive Guide To Coding and Programming in Stata 1st Edition Verified Download
A Comprehensive Guide To Coding and Programming in Stata 1st Edition Verified Download
1st Edition
Visit the link below to download the full version of this book:
https://medipdf.com/product/a-comprehensive-guide-to-coding-and-programming-in-s
tata-1st-edition/
Rafael Gafoor
Designed cover image: © Shutterstock, Stock vector ID 1714491562, Vector Contributor
Iurii Motov
Reasonable efforts have been made to publish reliable data and information, but the
author and publisher cannot assume responsibility for the validity of all materials or the
consequences of their use. The author and publisher have attempted to trace the
copyright holders of all material reproduced in this publication and apologize to
copyright holders if permission to publish in this form has not been obtained. If any
copyright material has not been acknowledged please write and let us know so we may
rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted,
reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other
means, now known or hereafter invented, including photocopying, microfilming, and
recording, or in any information storage or retrieval system, without written permission
from the publishers.
For permission to photocopy or use material electronically from this work, access www.
copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood
Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC
please contact [email protected]
Typeset in Minion
by MPS Limited, Dehradun
Contents
Foreword, vii
About the Author, viii
CHAPTER 1 ■ Introduction 1
CHAPTER 5 ■ Loops 48
CHAPTER 7 ■ Reshape 63
CHAPTER 8 ■ Dates 68
v
vi ▪ Contents
INDEX, 161
Foreword
T HIS BOOK WAS WRITTEN AT A TIME WHEN I HAD JUST CHANGED ROLES FROM
being a medical statistician (primarily concerned with the analysis
of data from randomised controlled trials) to being an analyst for large
datasets. I discovered that I had to learn new suites of commands in Stata
and that there were advanced functions that I had never previously
explored. This book comes from this experience and introduces the
reader to the commands that I found the most useful. This is not to say
that the commands I have chosen to explain are exhaustive in any sense
of the word.
I wanted the book to be a gentle introduction to the most commonly
used commands for the analyses of data obtained from both
experimental as well as observational studies. I have relied on my own
personal experience for guidance. No doubt you will find the book
incomplete; however I hope that this guide will provide a sound platform
from which you can explore further as you become an advanced Stata
programmer.
This book is suitable for a wide range of professions involved in data
analysis (medical statisticians, epidemiologists, data analysts, etc.).
vii
About the Author
viii
CHAPTER 1
Introduction
01_Data_In
02_Programming
03_Data_Out
You can place additional folders within these folders, but this is the main
structure.
NOTE: It is important to use the underscore and not leave blank
spaces when programming. While leaving blank space is not necessarily
an issue for Stata users in Windows, it becomes a major issue for some R
commands and in other programming environments.
DATA DOWNLOAD
You should now place the files for analysis in your file named
“01_Data_In”. This file now should never be overwritten. Any temporary
files or files which you make in the interim should now be placed in the
folder named “03_Data_Out”.
DOI: 10.1201/9781003483779-1 1
2 ▪ A Comprehensive Guide to Coding and Programming in Stata
cd "C:/Analysis_Folder"
. cd "C:/Analysis_Folder"
C:\Analysis_Folder
Introduction ▪ 3
dir "`c(pwd)'/01_Data_In/"
The macro ‘c(pwd)’ is where the contents of your working directory are
stored, and you can use this as a short cut in filepaths so that you do not
have to hard code again (once you have previously set the working
directory). Once you set the working directory, ‘c(pwd)’ will always point
to the correct location.
FOLDER STRUCTURE
Your coding for all projects encompasses several steps in data processing:
production of interim datasets, graphs, tables, etc. If you code all of your
programs for an assignment in one file, it can become very long, and you
can’t easily distinguish the stages in your analysis.
It is essential that you create a master file that you can use to call the
subprograms from. The master file sits in the top level of the folder
structure, and the subdirectories and files for your programming steps
sit in the folder named “Programming”. You may wish to add
additional programming files in your programming folder entitled
“01_Data_Input”, “02_Data_Processing”, “03_Tables”, etc.
Don’t worry if this all sounds a bit complicated. It will, with time,
become second nature. There will be examples of folder layouts and
master files later on in the book for you to copy if you wish. Choose any
file structure you wish (or adopt any you come across). The most
important characteristic of a good file structure is that a reviewer should
relatively easily be able to find the code, the data and the outputs without
too much trouble.
SPECIAL CHARACTERS
Be sure you can identify the backtick, apostrophe, backslash, forward-
slash and curly bracket characters. These will be used extensively in the
course.
CHAPTER 2
Temporary Names,
Variables and Files
6 DOI: 10.1201/9781003483779-2
Temporary Names, Variables and Files ▪ 7
. display "`c(pi)'"
3.141592653589793
clear
. scalar pi = 5.543234
. set obs 20
number of observaons (_N) was 0, now 20
. gen pi = 87
. scalar list
pi = 5.543234
. display scalar(pi)
5.543234
. display "`c(pi)'"
3.141592653589793
. display pi
87
. clear
8 ▪ A Comprehensive Guide to Coding and Programming in Stata
scalar drop pi
scalar list
I. TEMPORARY NAMES
One way of making sure you don’t overwrite existing scalars is to use a
tempname. Stata keeps track of these and never uses the same name
twice once you load any given name, so you needn’t worry about
confusing names and values. Once the program is run, and it comes to
an end, the temporary entity disappears. Don’t worry too much about
this at this time. Just remember that you should use tempnames
whenever you use a scalar. In the example below, the object is now
saved as “__000001” as a temporary object in Stata, which is deleted once
the program is run.
. tempname pi
. scalar list
. tempname pi
. scalar list
000001 = 5.543234
you need to create a temporary entity that holds a single value. When
you close Stata, the scalar will disappear.
A note on the naming of temporary names. You may wish to consider
these as local macros. So, the syntax ` and ’ that surrounds each macro is
used to tell Stata that it is a macro or a scalar. You will learn more about the
syntax of macros later, but it’s important to realise that temporary variables
are in effect local macros. Not only do they carry the syntax of a local macro,
but they also disappear once used (another feature of local macros).
tempvar coefficient
display "`coefficient'"
. tempvar coefficient
. display "`coefficient'"
000003