{"id":65,"date":"2017-08-10T13:02:26","date_gmt":"2017-08-10T17:02:26","guid":{"rendered":"http:\/\/blog.uvm.edu\/tbplante\/?p=65"},"modified":"2018-08-22T12:09:29","modified_gmt":"2018-08-22T16:09:29","slug":"generic-start-of-stata-do-file","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2017\/08\/10\/generic-start-of-stata-do-file\/","title":{"rendered":"Generic start of a Stata .do file"},"content":{"rendered":"<p>I took the Stata programming class at the Johns Hopkins School of Public Health during grad school It was taught by\u00a0Dorry Segev. If you are at the school, I highly, highly, highly recommend taking it\u00a0<strong>and doing all of the assignments <\/strong>in term 4. It saved me many hours of labor in writing up my thesis. It&#8217;s a phenomenal class.<\/p>\n<p>One of the biggest takeaways from the class was using a .do file as much as possible when interacting with Stata. As in 99% of the time.<\/p>\n<p>Below is the stock header and footer of every .do file that I make.\u00a0Steps to success:<\/p>\n<ol>\n<li>Open a blank .do file<\/li>\n<li>Paste the code from below<\/li>\n<li>Save it in the same folder as your dataset<\/li>\n<li>Close Stata<\/li>\n<li>In Windows File Explorer, find your new .do file and open it up then get rolling.<\/li>\n<\/ol>\n<p>By opening the .do file through file explorer, Stata automatically knows which folder you are working in. Then you don&#8217;t have to write the entire directory to start. For example, you can write:<\/p>\n<p style=\"text-align: center\"><em>use data.dta, clear<\/em><\/p>\n<p>&#8230;and not<\/p>\n<p style=\"text-align: center\"><em>use c:\\windows\\users\\myname\\work\\research\\001project\\data\\data.dta, clear<\/em><\/p>\n<hr \/>\n<pre><span style=\"font-weight: 400\">\u00a0<\/span><\/pre>\n<pre>********************************************************************************\r\n******************************HEADER STARTS HERE********************************\r\n********************************************************************************\r\n<span style=\"font-weight: 400\">\/\/ at the beginning of every do file:<\/span>\r\n<span style=\"font-weight: 400\">macro drop _all \/\/ remove macros from previous work, if any<\/span>\r\n<span style=\"font-weight: 400\">capture log close \/\/ Close any open logs. Capture will ignore a command that gives \r\n\/\/                   an error. So if there isn't<\/span> an open log, instead of giving you \r\n\/\/                   an error and stopping here, it'll just move onto the next line.\r\n<span style=\"font-weight: 400\">clear all \/\/ clean the belfries\r\ndrop _all \/\/ get rid of everything!\r\n\r\nlog using output.log, replace text \/\/ change the name of this to whatever you'd like\r\n<\/span>\r\n<span style=\"font-weight: 400\">\/\/ The purpose of this .do file is... [say why you are writing this do file]<\/span>\r\n<span style=\"font-weight: 400\">\r\nversion 15 \/\/ Every version of Stata is slightly different, but all are backwards \r\n\/\/            compatible with previous ones. <\/span>If you open up this do file with a way \r\n\/\/            newer version, it'll run it in version 14 compatibility mode. Change \r\n\/\/            this to the current version of Stata that you are using. This will \r\n\/\/            also keep your code from running on older versions of stata that will \r\n\/\/            break with new code that it isn't designed to handle. \r\n\r\n<span style=\"font-weight: 400\">set more off, permanently \/\/ so you don't have to keep clicking through stata to \r\n\/\/                           keep it running<\/span>\r\n\r\n<span style=\"font-weight: 400\">set linesize 255 \/\/ this keeps longer lines from getting clipped. Helpful for making \r\n\/\/                  tables.<\/span>\r\n<span style=\"font-weight: 400\">\r\ncapture shell md pictures \/\/ this makes a folder called pictures in the Windows \r\n\/\/                           version of stata. Save your pictures here.\r\ncapture shell mkdir\u00a0pictures \/\/ ditto, except in the Mac version.\r\n\r\n<\/span><\/pre>\n<pre>********************************************************************************\r\n******************************IMPORT DATA HERE**********************************\r\n********************************************************************************\r\n* working with stata dta file: \r\n\/\/ use \"data.dta\", clear \/\/ change this with whatever data file you are using and \r\n\/\/                        remove the double slashes. Note: putting quotes around \r\n\/\/                        filenames lets you open files with spaces in the name.\r\n* working with excel file: \r\n\/\/ CLOSE YOUR EXCEL FILE BEFORE TRYING TO OPEN IT OR STATA WILL STOP HERE.\r\n\/\/ import excel using \"name of file.xlsx\", firstrow clear \/\/ firstrow imports \r\n\/\/                       the first row of the sheet as variable. Change to the \r\n\/\/                       appropriate name and delete the double lines as needed. \r\n* working with csv file:\r\n\/\/ import delim \"name of file.csv\", varname(1) clear \/\/ varname imports the # \r\n\/\/                       designated as the variables. \r\n******************************************************************************** \r\n******************************CODE STARTS HERE********************************** \r\n******************************************************************************** \r\n\/\/ ... you get the idea\r\n\r\n<\/pre>\n<pre>********************************************************************************\r\n******************************FOOTER STARTS HERE********************************\r\n********************************************************************************\r\n<span style=\"font-weight: 400\">\/\/ At the very end of your .do file: <\/span>\r\n<span style=\"font-weight: 400\">log close\r\n\/\/ Fin.<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I took the Stata programming class at the Johns Hopkins School of Public Health during grad school It was taught by\u00a0Dorry Segev. If you are at the school, I highly, highly, highly recommend taking it\u00a0and doing all of the assignments in term 4. It saved me many hours of labor in writing up my thesis. &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2017\/08\/10\/generic-start-of-stata-do-file\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Generic start of a Stata .do file<\/span><\/a><\/p>\n","protected":false},"author":4473,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[477491],"tags":[],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-stata-code"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/users\/4473"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":10,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"predecessor-version":[{"id":267,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/65\/revisions\/267"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}