File structure and Basic Operations for Stata 10
This section outlines fundamental concepts related to levels and cross-sectional analysis, along with methods for organizing data It provides a concise overview of the file structure in Stata version 10 Reading this section is crucial before engaging in the cross-sectional session.
Overview When you open Stata 10 for the first time, you will see four different windows within the program—
• the Results window (results of a command are displayed in this window),
The Review window displays the commands submitted to the processor, while the Variables window lists the variable names from the opened data set Additionally, the Command window serves as the active interface for typing commands upon startup.
You can resize and reposition any of the windows in Stata Below is an example of the default arrangement of the windows
If you wish to rearrange the windows and keep your new arrangement, select
Type a name for the new preference set and click on Ok
To return to the original arrangements, from the same menu choose Edit
Other windows are available, but are not opened at startup These windows are:
• Viewer (used to view help files and log files, SMCL - markup and control language- files, and print log and other files This window is not contained in the STATA
10 program window but stands alone and appears on the task bar as another icon.)
• Data Editor (where you can view the data you have loaded into the program’s memory)
The Do-file Editor is a standalone text editor used to create "do" files, which contain executable commands for Stata Unlike other features, it does not reside within the STATA 10 window but appears as a separate icon on the taskbar.
You can switch between the windows within Stata by using the
Window choice from the Menu Note that shortcuts are also listed, e.g to switch to the Command window, you can press
4, to switch to the Variables window, press 6
Stata version 10 offers user-friendly menus for navigation, but users can also input commands directly in the Command window This tutorial will guide you through menu usage while also providing the corresponding Stata commands to enhance your familiarity with them Additionally, Stata allows you to paste commands into a do file for execution, and you can easily copy commands from the Results window or the Command window into the Do-file editor.
How Stata uses memory: a) The set memory command
Before conducting any analysis in Stata, a data file must be loaded into memory Stata/SE allocates 10 megabytes, Intercooled Stata uses 1 megabyte, and Small Stata utilizes 300 kilobytes for data storage While the memory allocation for Small Stata cannot be altered, users of the other versions can adjust the memory settings both temporarily and permanently To change the memory allocation, the appropriate command should be used.
To check to see how much memory is being used and how much is remaining, use the following command:
Before loading a file into memory, the result of this command in Intercooled Stata is:
After loading a small file, the results are: set memory [amount of memory] set memory memory
Details of set memory usage overhead (pointers) 0 0.00% data 0 0.00%
Other memory usage system overhead 745,090 set matsize usage 16,320 programs, saved results, etc 105
One megabyte can be used up fairly quickly, so it is recommended that you set the memory at the beginning of the session to a larger size, e.g
To configure memory settings in Stata when launching the program from a desktop icon, you can modify the startup command by adding the desired memory parameter This ensures that the memory is pre-set each time you start Stata, streamlining your workflow.
1 Highlight the icon on your desktop, right click and select
2 In the Comment: box, add /m30 (or whatever amount of memory you want to set it to) so that the command reads:
Upon installation, Stata sets the default data directory to the location where the program was installed, as indicated in the "Start in" box However, Stata also retains the last accessed file location, using it as a reference for future program launches.
4 If you have made any changes, click on to save the changes
When launching STATA from the icon, the memory and default directory will automatically configure to your specified settings However, if you start the program from the Start menu under All Programs, the memory parameter won't be set unless you adjust the shortcut in the Stata 10 directory.
Details of set memory usage overhead (pointers) 6,096 0.58% data 67,056 6.40%
Other memory usage system overhead 745,090 set matsize usage 16,320 programs, saved results, etc 1,029
Ok c) Increasing the amount of memory in the middle of a Stata session:
To increase memory during your session, you must first close the data file using the appropriate command.
Another option is to just close the Stata program and set the memory using the set memory command after you open the program and before you open a data file
Types of files used by Stata and their extension names
1 Data files - files containing data (Extension *.dta)
Data files have an extension of dta From the Stata 10 window, you can open a data file
If you are not in the directory where your files are, change to the appropriate directory Only files with an extension name of “.dta” will be listed
From the Command window (if you are working in the correct directory), you can type:
2 Log files - commands and output (Extension *.SMCL)
Stata markup and control language
- commands and output (Extension *.log)
- ASCII text: commands only (Extension *.txt)
Stata allows users to record commands and their outputs in a log file, which can be activated by turning on the log feature There are two types of logs available for this purpose.
A log is a comprehensive record of all submissions executed and the corresponding outputs generated from commands Users can choose between two formats for the log: SMCL or ASCII text.
To start logging your work in Stata, navigate to the Menu and select File, then Log, and choose Begin You will be prompted to enter a file name, with the default extension being SMCL, which is formatted in Stata's markup and control language After typing your desired file name, click OK If you wish to save the information as ASCII text, make sure to specify the appropriate file extension.
The log using command From the Command window, type: drop _all use "name of file", clear
The above command opens a file to record the session and uses SMCL format This file can only be opened in the Stata Viewer or type:
The above command opens a file to record the session and uses ASCII format This file can be opened in any text editor or word processor
The cmdlog using command The other type of log file records only the commands and not the output from the commands The command is
This command creates a file that records only the commands
In the Stata Command window, type:
A file is opened which is named “session1.txt", and information will be appended to anything that already exists in this file
The log close command To close the log, in the Command window, type
To share your Stata log files with someone who does not have Stata, save them in TEXT format with a log extension, as the SMCL format can only be opened in Stata Any text editor or word processor can access these files, but ensure that the font is set to a fixed-width font like Courier New for proper readability.
Otherwise, the output will be difficult to read.
3 Do files -Stata commands (Extension *.do)
A ".do" file in Stata is a script that contains executable commands, created in the Do-file Editor Users can either type or paste commands directly into the editor Additionally, commands can be generated through various methods: by creating a log file with the “cmdlog” command, saving the Review window contents with a right-click, or copying commands from the Results window using and pasting them into the Do-file Editor with Another option is to select a command from the Review window, cut it from the Command window using , and then paste it into the Do-file Editor.
to paste the command
Option c) may become your preferred method to build the do-file Option e) is also useful
Adding comments to do-files in STATA enhances clarity and organization Single-line comments can begin with an asterisk (*), while multi-line comments should be enclosed between /* and */ Additionally, comments can be added after commands using double slashes (//) This practice helps to document your code effectively.
* your name here and the date the file was created
/* do file to examine variables using the methods of Tabulate and tab1 */ describe // describing the variables in the file
Basic functions: Stata files, Descriptives and Data Transformations
This self-paced training aid serves as an introduction to essential commands for conducting typical statistical survey analyses using Stata 10 Designed as a stand-alone tool, it is most effective when supplemented by guidance from an experienced Stata user who can assist you with questions during your independent study Additionally, this tutorial can be utilized as a resource for classroom training.
A copy of the questionnaire on which the data is based can be found in the Mozambique project 1992 NDAE Working
The socio-economic survey of smallholders in Nampula province utilizes specific research methods and includes three tables located in the annex section of the manual For additional information, Dr Michael Weber can be contacted at webermi@msu.edu The questionnaire comprises four sections, each linked to a corresponding Stata data file, along with two additional Stata files necessary for unit conversion.
Questionnaire Section Stata Data File
Main Household Section c-hh.dta
Table IA: Household Member Characteristics c-q1a.dta
Table IV: Characteristics of Production c-q4.dta
Table V: Sales of Farm Products c-q5.dta
Conversion factors for computing kilograms conver.dta
Conversion factors for computing calories calories.dta
This training comprises four sections, each designed to take around two hours to complete It is advisable to finish each section in one sitting The tutorial materials are based on specific assumptions.
• You know how to use Windows with a mouse
• The six data files listed above should be stored in a directory of your choosing on your hard disk
Always ensure you SAVE your data changes after completing each exercise and section, using a new file name Additionally, consider saving the contents of the Review window to a do file if you haven't been copying commands to a do file previously.
To get started with Stata, open the software and familiarize yourself with key components such as the Command Window, Review Window, Results Window, Do-file Editor, and Viewer If you haven't already, it's essential to complete Section 0 for a better understanding of these features.
Data files and the working file
Data from questionnaires entered into Stata are stored in data files, which must be opened to access and work with the data set in the program.
The working directory is the location where your data files are stored To access these files, use the 'cd' command to navigate to the appropriate directory containing your data.
In the Command Window type:
To streamline the process of creating a do-file, navigate to the directory where your files are stored, which removes the necessity of specifying the directory name within the do-file itself If the directory contains spaces, be sure to enclose the entire path in quotes.
“C:\Documents and Settings\My Documents\data”
When a data file is opened, it is loaded from the disk into the computer's RAM, becoming the working file and making its data accessible for use For instance, to access the data for Table IA: Household Member Characteristics, you will need to open the corresponding file, c-q1a.dta To do this, follow the specified steps.
1 From the File menu, select Open
This will open the Open File dialog box
2 If you have run the “cd” command you should see a list of data files to be used with this tutorial Select the file c-q1a.dta
3 Click on the button to open the file The command appears in the Review window
In the Review Window you will see the text
Open use "….\c-q1a.dta", clear cd "name of working directory"
“….” will be replaced with whatever the name of the directory is where you are working
To create a do-file for saving commands, start by copying the executed command from the Results window using Then, open the Do-File Editor by clicking the corresponding button in the Tool Bar and paste the command into the file with Additionally, the use command you just executed will appear in the Review window for your reference.
which places the command in the Command window where you can copy it Switch to the Do-file editor and paste the command
To enhance the clarity of your do-file, copy the “cd” command and position it just above the “use” command Additionally, include comments that explain the purpose of the do-file Make sure to insert some blank lines above the pasted commands for better organization.
/* “your name here” - “the current date here” */ (example: /* beaver – 5 Jan 2009 */)
To ensure optimal performance in your do-file, it is essential to include commands that close any open log files, clear the memory workspace, and drop all macro variables For instance, you can use the commands: `capture log close` and `log using name_of_log_file, replace` to effectively manage your logging process.
/* Tasks to be done in this do file */
/*program setup */ version 10 clear all macro drop _all
/* change to directory where files are stored*/ cd "C:\Documents and Settings\aec_user\My Documents\StataTraining"
6 Save the do-file From the File menu in Do-File Editor select Save As
The do extension will be added to the name automatically
The do-file is now saved to disk
We have opened the household-member data file which is now the current file in memory
Describing the contents of a data file:
To understand the contents of a data file, it's essential to identify the variables it includes This information, along with additional details, can be obtained by utilizing the Describe data command found in the Data menu.
1 From the Data menu select Describe data
2 There are several choices under this option: Select
Describe data in memory A dialog box opens: There are several options in this dialog box
At the bottom of the dialog box, there are three icons on the left The first (a question mark) opens the
“Help” screen to explain the options in the dialog box
The second icon (an R) clears the selections in the dialog box, while the third icon copies the command to the clipboard, allowing you to easily switch to the do-file and paste the command there.
On the right hand side you have the choices to click on
When using the dialog box, selecting "Ok" or "Submit" will automatically execute the command and close the box However, if you choose "Cancel," the dialog box will remain open, allowing you to select another option without needing to reopen it.
We want a description of all variables; therefore, we can leave the list of variables blank Before you click on Ok, click on Switch to the do-file, and press
to paste the command Switch back to the dialog box and click on Ok
In the Results window, you will see the description of the variables To obtain the same results from the Command window type