0 of 5 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 5 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0) 0 Essay(s) Pending (Possible Point(s): 0)
The code runs without error. The first line changes the directory to the specified folder. The directory in the second line does not exist on my machine, but rather than return an error and stop the .do file, Stata just ignores this command because of the ‘capture’ prefix. ‘capture’ tells Stata to ignore the command if there is an error and continue executing the .do file. Then Stata imports the data in the third line, merges it with the other dataset in the fourth line, and saves a new dataset in the fifth line.
If I did not include the ‘capture’ prefixes, then Stata would return an error on the 2nd line since Christy’s folder does not exist on my machine, and would stop running the .do file. Similarly, if Christy tried to run this code on her machine, it would run successfully with the capture prefixes, but stop and return an error on the 1st line without the capture prefixes.
Since Christy’s folder does not exist on my machine, Stata would try and fail to change the directory to Christy’s folder on the 2nd line. With the capture prefix, Stata just ignores this error, skips the command, continues executing the rest of the .do file. But without the capture prefix, Stata would return an error and stop executing the .do file.
In the 1st line Stata would successfully change the directory to “C:/Users/Jeff/Stata Best Practices”. Stata would ignore the 2nd line since Christy’s folder does not exist on my machine, and with the ‘capture’ prefix Stata just skips the failed command and continues. But in the 3rd line Stata would look for and fail to find the “data1.dta” dataset since that dataset is in a subfolder called “Data”. So Stata would return an error on the 3rd line and stop executing the .do file. To fix this problem, I could either change the directory in the 1st line to “C:/Users/Jeff/Stata Best Practices/Data”, or specify the subdirectory in the 3rd line when I import the dataset:
use “Data/data1.dta”, clear
Since I saved the new dataset with the same name as the original dataset, and included the ‘replace’ option in the save command, Stata will run this code without error and overwrite data1.dta without the first 100 observations. If you ran this code multiple times then each time it would delete the top 100 observations until all of the data was gone. You would not be able to recover the previous dataset unless you had it backed up in another folder or on the cloud. This is a warning to not overwrite datasets with the same name!
Your code could produce different results if you executed a command that involves some kind of random selection or random sort, without first setting the seed or including the ‘stable’ option in the sort. If you set the seed or if your code does not include a random number function, then assuming that the input data is the same, Stata will produce the same results each time that you run it. Stata will not round results differently or execute the commands in a different order than the order that you gave it.
1. Suppose that datasets “data1.dta” and “data2.dta” were in a folder on my machine called “C:/Users/Jeff/Stata Best Practices”. What would happen if I ran this code as it is written?
2. What if I ran this code without the ‘capture’ prefix on the first two lines?
3. What if I ran this code but the datasets “data1.dta” and “data2.dta” were in a folder “C:/Users/Jeff/Stata Best Practices/Data”?
4. What would happen if you ran this code (assuming that “data1.dta” exists in your current working directory)?
5. What are possible reasons why a .do file would produce different results each time you run it? (Choose all that apply)
27 November 2024
18 November 2024
13 November 2024
11 November 2024
5 November 2024
25 October 2024
22 October 2024
21 October 2024
12 September 2022
Username or Email Address
Password
Remember Me