In this module, students will learn (1) how to set up the working directory, (2) how to use the STATA do-file, and (3) how to read and save the STATA data file (.dta) via do-file.
WORKING DIRECTORY: STATA works on a certain physical location (working directory). It is useful to know how to change this location so that the user could determine where to read and store.
1. STATA Working Directory: If you open STATA from the window menu, you are setting the working directory set by the installation process. Type the following command into the command window in order to see the current working directory. The "pwd" is to tell STATA "present working directory."
pwd
Remember, STATA could not read data unless you put your data into the working directory. Otherwise, you have to change the working directory manually.
2. Changing Working Directory: Let's change the working directory so that you can read a data set from a different folder.
Go to the data you want to read in STATA. On the right click, go to the properties and copy the entire location. Then, use the command "cd" to change directory.
cd "C:\Users\DongkyuKim"
The command "cd" is to tell STATA "change directory."
In case the folder names contain a space, please use the double quotation mark.
Now, with pwd command, you can see the working directory has been changed.
WORKING DIRECTORY: STATA works on a certain physical location (working directory). It is useful to know how to change this location so that the user could determine where to read and store.
1. STATA Working Directory: If you open STATA from the window menu, you are setting the working directory set by the installation process. Type the following command into the command window in order to see the current working directory. The "pwd" is to tell STATA "present working directory."
pwd
Remember, STATA could not read data unless you put your data into the working directory. Otherwise, you have to change the working directory manually.
2. Changing Working Directory: Let's change the working directory so that you can read a data set from a different folder.
Go to the data you want to read in STATA. On the right click, go to the properties and copy the entire location. Then, use the command "cd" to change directory.
cd "C:\Users\DongkyuKim"
The command "cd" is to tell STATA "change directory."
In case the folder names contain a space, please use the double quotation mark.
Now, with pwd command, you can see the working directory has been changed.
Introduction to the STATA do-flie: the do-file is a batch file for storing data commands.
1. Open the do-file: In the STATA tool bar, click the first icon with a pencil, "New Do-file Editor." Then, it will pop up the do-file editor. Type out the same command to present the working directory.
pwd
Click the last icon on the tool bar, "Execute (do)."
If you click this icon without selecting anything inside the do-file, it will execute every command in it.
If you want to execute a particular line, click the number on that line and click the icon.
2. Asterisk function: In the do-file, you can put texts with asterisk.
* This website is prepared for students to learn STATA from scratch.
If you put asterisk in front on the line, STATA would recognize the entire line as texts (not commands)
You can also put texts with command in the same line like this:
pwd /* This command tells STATA to present working directory */
The text-considered parts are expressed in green in do-file.
3. Save the file: First create a folder where you want to put your do-file. By clicking "save," save the file into the folder you created with a file name. Once you click "Save," you will see the file.dta in that folder. (.dta) is the STATA data file.
4. Close off everything
LOADING & SAVING DATA
1. Starting STATA from do-file: First, create a folder inside your working folder that has the do-file and label it "Data." Download "GSS2016.dta" from the class blackboard and save it into the "Data" folder. In the working directory, double click the do-file. Let's bring the data into the STAT with "use" command.
use "DATA\GSS2016.dta", clear
It tells STATA "Go to the subfolder "Data" and load the dataset of "GSS2016.dta."
The option, "clear," makes you load the data when STATA already has a data. Of course, you will lose the previous data with this option.
Let's assume we did the necessary data manipulation. So, let's save it.
save "GSS2016_DK.dta", replace
Without designating a sub-folder, you are saving your data into the working directory directly.
The option replace makes you to override the existing data set on that folder.
FYI: Why do we use double quotation mark? if the data name has space, STATA is not able to read the entire label of a folder or a variable. So, when the folder names / variable labels have spaces, you have to use the quotation mark.
save "GSS2016 DK.dta", replace
Assignment 1: Follow the instruction.
Download a survey of your choice from the General Social Survey (GSS: http://gss.norc.org). Please look around and download a survey data of a certain year with a STATA format. Make sure where you stored the data.
Create a do-file and identify the working directory with the pwd command.
Change the working directory to the folder where you saved the data is with cd command.
Read the data with use command. And save the data with save command.
Please submit the do-file to the Assignment 1 in Blackboard.