* BIOSTAT 212: Introduction to Statistical Computing in Clinical Research * SUMMER 2017, TICR * Kristen Aiemjoy * LAB 1 * Outline: * I. COMMENTING * II. SETTING UP * A. SET A PROJECT DIRECTORY * B. KEEPING SESSION LOGS * C. OPENING A DATA SET * III. EXPLORING DATA (BASICS) * A. DESCRIBE * B. BROWSE * C. SUMMARIZE * D. TABULATE * IV. Ending your .do file ****************************************************************************** * I. COMMENTING ****************************************************************************** * There are three main ways to comment your .do file. *(1)* * You can put an asterisk(*) at the beginning of a line like this * an asterisk cannot be placed at the end of a command line des * describes the variables in the data <-- this is wrong! *(2)* // you can use double slash to make comments at the beginning or end of a line des // describes the variables in the data // command 'des' describes data *(3)* /* You can use a '/*' to open a comment that will carry-on for multiple lines. Remember to close it with '*/' */ ****************************************************************************** * II. SETTING UP ****************************************************************************** /* It's good practice to make sure all previous work has been cleared at the start of your do-file using the 'clear' command */ clear all //clears all previously open data, variables, labels, matrices, memory * and close all open files, graph windows, etc. /* Remember your keyboard short-cuts. HIGHLIGHT the desired command by clicking on the line number. Or click and select a potion of the command (you do not need to highlight it all. Then use the keyboard shortcut: For Mac users cmd+shift+d For PC users ctr+d */ * A. SET A PROJECT DIRECTORY /* A working directory is an address to where your files are stored. Specifing working address in your .do file allows you to access files and data seemlessly every time you open run your .do file Windows uses \ to deliminate paths Mac uses / */ pwd // pwd stands for 'print working directory.' use it to check the current working directory. /* Now lets create a folder for this lab and change the directory to that folder: in Documents (or whatever directory you would like) create a folder for 'Biostat 212', inside 'Biostat 212' create a folder called 'Labs', inside 'Labs' create a folder for 'Lab 1' */ cd "/Users/yourname/Documents/Biostat 212/Labs/Lab 1" //adapt to your working directory path /* HINT: you can copy and paste the desired filepath by using the drop down menu: File > Change Working Directory > Navigate to the Lab 1 folder */ /* shortcut for your home directory: Stata understands ~ to mean your home directory. Thus, you can refer to a dataset named mydata.dta in a subdirectory named mydir within your home directory by referring to the path ~\mydir\mydata.dta in Stata for Windows or by referring to the path ~/mydir/mydata.dta for Mac or Unix */ * B. KEEPING SESSION LOGS /* Stata can record your session into a file called a log file. A log file records both commands and output. Stata will not start a log automatically; you must tell Stata to record your session. Logs are recorded in a format called Stata Markup and Control Language (SMCL). The file can be printed or converted to plain text for incorporation into documents you create with your word processor. */ cap log close // Close any existing log files first. //this is a good command to include at the beginning of your do-file * Start a log file using the drop-down menu: // File -> Log -> Begin *start a log file within your .do file (reccomended) log using lab1.log, replace //will replace existing log if one is save in the folder with the same name log using lab1.log, append //will append existing log file with same name if you have one // The log is saved in your working directory, *try opening the previous log file by executing the following line of code: log using lab1.log /* stata will return error: "file...already exists" To use a previously created log file, you need to use either "replace" or "append" after the comma */ * Close a log file (Usually done at the end of the .do file.) log close *Then open it again log using lab1.log, append * C. OPENING A DATA SET /* Let's start exploring one of Stata's "Shipped"/built-in data sets. Check out the data that comes loaded into Stata using the drop-down menu: File