Section 6 Stata

6.1 Coding

What is coding? Computer programming, or coding, is the process of issuing a series of commands (writing instructions) that a computer (or other hardware) can understand and execute in order to perform a task.

  • Imagine a computer as being like a particularly mischievous genie; if your instructions aren’t completely unambiguous, the computer won’t do what you asked for
  • Like a genie though, if you can get the instructions just so, you can create magical things

6.2 File Management

File management is the process of creating an organized structure to store information for easy retrieval. Proper file management practices are critical for being able to access data and code files.

  • Every file has an address on your computer, and the location is usually a series of nested folders. – When you’re coding, this address is how you tell the computer/software how to find the file – You can’t tell the computer to search for a file like you do since the computer doesn’t know what YOU think is the right file
  • If you use an Eggers 040 computer, rds.syr.edu, or any on-campus computer, your files will travel with you if you save them to OneDrive.
  • IMPORTANT: the default is to download files from a web browser to the Downloads folder on a computer’s hard drive. This will NOT travel with you.

6.3 Opening Data in Stata

6.3.1 Point and click: Import Wizard

These are the steps we took to open up the ECN 310 Class Survey data in Stata. You can use them to repeat that exercise, or to open up other datasets in Stata.

  1. Navigate to and open the Stata application
  2. Click on File menu, choose Import, then Text data (delimited,*.csv,…)
  3. Browse to the location you saved the file in
  4. Change “First row as variable names” to “Custom” and “1” (this was what we did for the class survey data, note that this may not always be the option that you need)
  5. Click “OK”

Following these steps will open the file and generate the first command: import excel. You can see this in the history window or the output window in the middle of the screen. It will look something like…

import excel "C:\\Users\\username\\OneDrive - Syracuse University\\Documents\\GitHub\\exercises\\application_survey_anon.xlsx", sheet("anon") firstrow

You can then use this line of code, going forward, to import your data.

6.3.2 Using a line of code

If you are opening a Stata data file with a “.dta” file extension, you can use the use command. If you are attempting to open a dataset in Stata that exists as a different type of file, it may be easier to open it using the Point and Click Import Wizard, and then copy that command from the history window.

  1. You must find the file path to the dataset you seek to open. Once you navigate to the file in File Explorer, you can copy and paste the path at the top of the window.
  2. You must also have the name of the file. It is best practice to copy and paste the file name and the file path instead of re-typing them yourself.
  3. Once you have those two things, you can execute the use command in Stata. Such as…

use "C:\\Users\\username\\OneDrive - Syracuse University\\Documents\\GitHub\\exercises\\dataset"

6.4 Running Code

6.4.1 Note on Stata instances

Each time you click on a file (e.g., a do-file and a data file), a new copy of Stata opens. To have both a data file and a do-file open in the same instance (copy) of Stata, you’ll need to click on one to open it, then open the other from within Stata. If you have a do-file open in one instance, and data open in a different instance, the code that you run will not operate on the data you are seeking to work with.

Note that you can NEVER have two datasets open at the same time in the same instance of Stata.

6.4.2 Three ways to obtain information using Stata

  1. Execute command from a do-file
  1. Type the command in the do-file
  2. Highlight the command
  3. Hit “Control+D”
  1. Execute command in the Command Window
  1. Type the command in the Command Window
  2. Press enter
  1. Execute the command using a menu
  1. Click on the appropriate menu (e.g. Graphics)
  2. Choose the command (e.g. Pie Chart)
  3. Fill in the dialog box
  4. Click on “Submit” or “OK”

6.4.3 Using do-files

Use a do-file if…

  1. You’re likely to do something similar in the future. Do-files let you save, revise, and rerun commands.
  2. You’re going the use anything your code produces. Do-files and the comments within them are a key form of documentation
  3. You’re going to share the code or product with anyone

6.5 GSS Mini Report: Accessing & Visualizing Data

Task

Working in your “GSS Mini Research Report Team”:

  1. Pick one variable from the 2016 General Social Survey (GSS2016.dta) that makes sense in a pie chart
  2. Enter the variable name in PollEverywhere
  • Your team’s variable must be unique
  • Both team members should submit it
  1. Make the pie chart in Stata
  2. Save it wherever you want on your computer, as .png or .jpg (NOT as a Stata graph—nothing except Stata can open those)
  3. Go to the “First (mini) research report” issue on the exercises repo, https://github.com/ecn310/exercises/discussions/4
  4. Write and submit your report on the Github discussion
  • One report per team
  • Instructions are in the first post
  • I’ll put an example in the first comment on the issue

How to access and analyze the data

  1. Navigate to the exercises repo on Github
  2. In the list toward the left of the screen, click on GSS2016.dta
  3. Click on “View raw” or the download symbol all the way on the right of the gray “Code” bar
  4. Save the file to your computer, noting the location you save it to
  5. Navigate to where you saved the file and double-click on it (this only opens Stata because it’s a .dta)
  6. Explore the data in Stata. Use browse to see the data, use describe or describe, short to get a summary of the data file.
  7. Explore one variable by using codebook varname, where you input the name of your variable where “varname” is to get an understanding of your variable. Use tabulate varname to see what the observations for that variable look like.

Make a pie graph

  • Use graph pie, over(worryjob) to make a pie chart.

    • Use help graph pie to view documentation on the graph pie command.
  • Use graph pie, over(worryjob) title("How many people worry about losing their job?") to add a title.

  • Use graph pie, over(worryjob) title("How many people worry about losing their job?") plabel(_all percent)to add percentages

  • Finally, use graph export "C:\Users\...\pie_worryjob.png",as(png), replace to save the graph as an image file. Ensure that you fill in the “…” to match the file path on your computer.

Understand and apply syntax for saving a file

  • graph export is the command to save a graph
  • "C:\Users\krist\OneDrive - Syracuse University\ECN_310\pie_worryjob.png" is the path and file name ON PROFESSOR BUZARD’S COMPUTER, not on yours! You need to customize!
  • , is the required comma between command and options
  • as(png) is the option that saves the file in PNG format
  • replace is the option to write over any existing file in the same location with the same name

Mini Report Improvements

  1. Go to the “First (mini) research report” discussion on the exercises repo
  2. Can anything else be improved? How about making the pie chart smaller?
  3. Make .do file that produces the image in your post (detailed instructions on next slide)
  4. Upload your do-file to the GSS_mini_report folder (detailed instructions in two slides)
  5. As a reply (leave your original post alone!!!) to your original comment, post improved version:
  • image is smaller;
  • image matches .do file;
  • has new sentence at the bottom that says something like “The code that created this pie chart is here” where “here” is a link.

You will make a .do file that produces the image in your post. Professor Buzard will run it and make sure your code works/matches what’s in your post. Start by… - Opening Stata, choose Window menu, then Do-File Editor, then New Do-File Editor - The first line of your .do file should be the command to open the data - Insert code to produce the same pie chart from last week - End the file with command to save the pie chart at end - Save file as name1_name2.do (e.g., Dylan_Soph.do) to your computer - Then, upload your do-file to the GSS_mini_report folder: - Navigate to GSS_mini report folder of the exercises repository - Above the list of files, select the Add file dropdown menu and click Upload files. - Alternatively, you can drag and drop files into your browser. - To select your do-file for upload, drag and drop the file, or click choose your files - In the “Commit message” field, type a short, meaningful commit message - Below the commit message fields, choose “Commit directly to the main branch” - Click “Commit changes” - Refresh the GSS_mini_report page and click on your file to make sure it looks right - Copy the url to add to your post on the discussion