Stata: Working with Do Files

.do file
This is a topic that many people are looking for. https://granthamandira.org/ is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, https://granthamandira.org/ would like to introduce to you Stata: Working with Do Files. Following along are instructions in the video below:


in this video Ill go over the basics of using do files in Stata do files are scripts that store a sequence of commands that Stata will execute in order they offer several advantages if you have commands youll use repeatedly the do file can save you a lot of time they also serve as a record of exactly what you did to your dataset and they allow you to share your work easily with collaborators and others to demonstrate how a do file works Im going to repeat the commands I demonstrated in video 3 renaming and labeling variables and values in that video I showed several ways to execute the commands through the data menu through the variables manager and by typing the commands into the command window in this video Ill show how to run the same commands by running them from a do file first Ill show how to run the commands one by one and at the end Ill demonstrate how to run a complete do file you can open a new do file using this icon on the toolbar or you can go to the window menu choose do file editor and then choose new to file editor first we need to open the original version of the data this is a file called TV data DTA the use command opens the data file so Ill type use and then the complete path to my data set then I can highlight the line and use the execute button on the toolbar to run it if I toggle to the main window I can see that my data set is now open if youre not sure of the exact path to your data set you can open the file through the file menu from the file menu choose open in the dialog box navigate to the folder that contains your data select your data set and click open hopefully when you do this state of prints the command you need here in the results window you can select it use ctrl C to copy it and paste it in your do file I want to point out a few

things about this command first you can see that Stata puts the path in quotation marks by default if any of the names on your path contains a space you need to put the path in quotes if you dont have spaces the quotes are optional but its a good idea to do as a habit just to be on the safe side if youre using a Windows computer Stata may use back slashes instead of regular slashes this shouldnt be a problem Stata can read the command either way finally notice that Stata ends the command with comma clear anytime a command has specifications after our comma those specifications are known as options by default Stata adds the clear option to the used command this is a good idea to do in general whenever you execute the use command clear tells data to clear away any open data set before trying to open the one in the command if you dont add clear and you have a data set open already youll get an error and Stata will stop executing your two file you can also use clear as a command by itself to simply clear the open data set without opening a new one you can see that theres now no data set open if you dont want to enter the complete path to your data set you can simply change your working directory the working directory is the main folder where Stata will look first for files and where itll write any files you create unless you specify otherwise to figure out the current working directory use PWD we can see my current directory is My Documents folder so if I save any files without giving a full path Stata will save them to the Documents folder when I try to open files itll only look for them in the Documents folder unless I direct it somewhere else in the path I can change that if I want to I have my data saved in my TV study folder and Id like to make that my working directory I can use the command CD for change directory now that the TV

study folder is my working directory I can simply tell Stata to use the TV data file I can highlight these two lines and run them together when I check in the main window I see that both commands ran successfully and the data set is again open a great habit in writing to files is adding comments about your code comments or lines of text in your new file that wont be executed by Stata comments are an excellent way to clarify what your code does this will help you later when you return to a do file you havent used in a while or if you share your do files with collaborators students and others commenting can be very helpful and I encourage you to make it part of your do files there are two ways to write a comment for a single line comment you can write a star at the beginning of the line and then the text of your comment here Ill clarify that Im changing my working directory and opening my data set I dont need the old version of the command anymore but if I dont want to delete it I can comment it out adding a star at the beginning of the line so the line doesnt run you can also write multi-line comments start the comment with the combination slash star and end it with star slash anything in between those sets of characters will be a comment this is often a good way to put a note at the top of the do file explaining what the file does it doesnt matter if the closing characters come at the end of the comment or on their own line as I keep working on my do file I want to make sure to save it early and often I can use the Save icon to save my file or I can go to the file menu and choose save Ill navigate to the folder where I want to save my file Ill name it names and labels and click Save now that I have my data open I want to rename several variables as you

saw in video 3 the command to rename a variable is renamed and the syntax is rename old name new name so to rename the identification number variable to ID I just type rename identification number ID Ill highlight the command and run it you can see in the variables window that its been renamed recall from video 3 that you can rename multiple variables at once using parentheses the syntax for this is rename the list of current variable names and parentheses and the list of new names in parentheses this is a very long command and if it were all on one line it would be hard to read however if I just put a new line in the middle Stata will give me an error because each line is treated as a separate command and these are incomplete commands instead I can add three slashes where I added the line break this tells Stata to continue on to the next line and treat it all as one command also note the ordering of the variables it doesnt have to match the order that variables appear in the dataset but the order in the first set of parentheses does have to match the order in the second set now I can highlight the command and run it in the variables window we can see that theyre all renamed the next thing I want to do is to label some variables the command for this is label variable and the syntax is label variable variable name label so if I want to give age the label years of age I type label variable age years of age recall that I have to put this label in quotation marks because it contains spaces Ill highlight the command and run it we can see in the variables menu that age has a label now Ill add the commands to label the other variables Ill highlight them and execute again we can see in the variables window that all of these variables have been labeled finally I want to label the values of some of my variables just as before this is a two-step process first

I create the label then I apply to the variable to create a label I use the command label define the syntax is label to find the label name and then the values and their corresponding labels so to define a label for the variable gender all right label define gender label one male to female once Ive defined the label I can apply it using label values the syntaxes label values the variable name then the label name here Ill write label values gender gender label Ill do the same for the variable happy first all the final label then Ill apply it the exact order of these commands doesnt matter too much as long as each label is defined before its applied so I can change the order like this and itll be fine Ill highlight these commands and run them we can use tab gender and tab happy to see that the values have been labeled at the end of the do file Im going to add a line to save the revised data file Ill save it as TV data labelled so I can keep both the labeled and unlabeled version Ill add the replace option which tells data to overwrite any older version of this file that may exist since I changed my working directory at the top of this new file I can save the data using just the file name if I hadnt changed the working directory or if I wanted to save it to some other folder I would have to give the complete path here now in this short do file Ive accomplished everything we had previously done through the drop-down menus and toolbar the best part is I can easily run it again to demonstrate this first Ill clear the data to run the whole do file at once rather than individual lines I just click on the execute button without highlighting any lines you can see that the whole to file has run and repeated all the commands we used before Ill make sure to save the new file and were all set thank you for joining me for video for using du files

tags:
Stata (Software), Data Mining (Software Genre), Statistics (Field Of Study), Data Management (Literature Subject)
Thank you for watching all the articles on the topic Stata: Working with Do Files. All shares of https://granthamandira.org/ are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.

Leave a Comment