5. Review of Weeks 1 - 4

5. Review of Weeks 1 - 4#

We’ll use the script below to do a quick overview of what we’ve covered in the first half of the term

.do file script
qui {
	if 0 { //Background
		1. Intermediate Stata
		2. Practice session of week 1-4 content
		3. Let's create an online module
	}
	if 1 { //Log, Settings, Macros, Workdirectory, Data
		cls
		clear 
		//capture log close 
		//log using wk5.log, replace 
		local output "/users/d/desktop" 
		global url "https://raw.githubusercontent.com/yaiura1/hw1/main/hw1.do" //you all have your own URLs, this is one of your peers just for demo purposes
		global table1_fena "https://raw.githubusercontent.com/jhustata/basic/main/table1_fena.ado"
		capture confirm file "`output'"
		if _rc == 0 {
			cd "`output'"
		}
		else {
			noi di "Provide the filepath you wish to direct your output to" _request(output)
			cd "$output"
		}
		do "$url" //data simulation
	}
	if 2 { //Install table1_fena.ado, Output a Table 1
		local num_lines 7
        capture file open myfile using "$table1_fena", read text
        forvalues i = 1/`num_lines' {
            file read myfile line
	        cls
            noi di "`line'"
        }
        file close myfile
		noi di "Use the above syntax to create a Table 1 for your simulated dataset" _request(syntax)
		noi di "Type ds to review the variables available to you in this dataset" _request(syntax)
		noi di "When finished, type the commands pwd and ls to see your outputfiles" _request(syntax)
		noi di "Remember: your syntax should always be preceeded with a command (e.g., table1_fena)" _request(syntax)
		qui do "$table1_fena"  
	}
	//log close 
}
do "https://github.com/jhustata/intermediate/raw/main/wk5.do"

5.1 Overview of first-half of term#

1. Setting Up and Logging:

qui {
	cls
	clear 
	capture log close 
	log using wk5.log, replace 
}
  • cls and clear: Clear the Stata interface and all data from memory, ensuring a clean workspace.

  • log using: Starts a new log file wk5.log to record all commands and results, allowing for accountability and reproducibility.

2. Directories and Global Macros:

local output "" 
global url "https://raw.githubusercontent.com/yaiura1/hw1/main/hw1.do"
global table1_fena "https://raw.githubusercontent.com/jhustata/basic/main/table1_fena.ado"
  • local and global: Define file paths and URLs as macros for easy reference. local is for temporary use within a session, while global persists across sessions.

3. Confirm Output Directory and Run Simulation:

capture confirm file "`output'"
if _rc == 0 {
	cd "`output'"
}
else {
	noi di "Provide the filepath you wish to direct your output to" _request(output)
	cd "$output"
}
do "$url" //data simulation
  • Checking for output directory: Uses capture confirm file to check if the specified directory exists. If not, prompts the user to provide a valid directory.

  • do "$url": Executes the script located at the URL stored in the global macro, simulating data.

4. Installing External Programs:

do "$table1_fena" //install remote program
  • External Scripts: Installs the table1_fena.ado script, which is useful for generating descriptive statistics tables (Table 1).

5. Outputting Table 1:

local num_lines 7
capture file open myfile using "$table1_fena", read text
forvalues i = 1/`num_lines' {
    file read myfile line
    cls
    noi di "`line'"
}
file close myfile
noi di "Use the above syntax to create a Table 1 for your simulated dataset" _request(syntax)
  • Reading .ado File: Demonstrates how to open and read from an external .ado file. This snippet could be part of a tutorial on how to modify or understand .ado files.

  • forvalues Loop: Loops through the first 7 lines of the file, displaying them. Useful for understanding or debugging the contents of external programs.

6. Closing Log:

//log close 
  • Close the log: It’s commented out here, but typically, you’d close your log file to finalize all records of your Stata session.

5.2 Lab#

Understanding Logging and Script Management

  1. Code Review: Examine why I commented out the following lines: capture log close, log using, and log close.

    • Investigation Task: Determine the implications of including these lines in the script execution. How does it affect the session log and potential error handling?

5.3 Homework#

  1. Debugging External Programs:

    • Debug the external programs and commit the revised versions to your hw5 repository.

  2. Script Execution and Documentation:

    • After updating the external programs, rerun the script provided above. Your script should create a .log file to document a debugged session.

    • Submit your .do to CoursePlus DropBox

      • Do not submit your .log file

      • Open science demands that we reproduce your results.

    • Finally, drop the URL of your .do file in GitHub Discussions for your peers and teaching team to review