Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stata Basic Course Lab 2.

Similar presentations


Presentation on theme: "Stata Basic Course Lab 2."— Presentation transcript:

1 Stata Basic Course Lab 2

2 Layout Screenshot Variables’ window (name and label)
Output here Variables’ window (name and label) History of commands Results Windows Variables’ description Written Command window

3 Where I am working? The Screenshot also contains an important piece of information: the Personal Working Directory, or pwd. In the screenshot, it is listed as C:/Users/DerricoM/Documents/. The pwd is the directory to which any files created in your Stata session will be saved. Likewise, if you try to open a file and give its name alone, it is assumed to reside in the pwd. If it is in another location, you must change the pwd [File  Change Working Directory] or qualify its name with the directory in which it resides. You generally will not want to locate or save files in the default pwd. A common strategy is to set up a directory for each project or task in a convenient location in the filesystem and change the pwd to that directory when working on that task. This can be automated in a do-file with the cd command. Folder organization!

4 Folder organization

5 Folder organization

6 Folder organization

7 Folder organization

8 Use the DO File! (1) Main advantages of using a do-file
Get a track of what you were doing Get a track of who was doing what Get a sense of what is the purpose of that file Share your work Record your work Re-do your work Check your work

9 Use the DO File! (2) What’s your name? What are you doing here?
Who made this mass? Different colours = different use Red Green Blue black

10 Use the DO File! (3) Do-files allow you to record all the commands you typed and need during a work session. MOST IMPORTANT THING: with do-files you have a copy of what you did, which allows you and other researchers to replicate exactly your analysis.  You will have a permanent record of typed commands. You can run the commands directly from STATA typing: doedit Or using CTRL-D Write the commands, select the line(s), and click on the last icon in the do-file. or click this icon We’ll see later the meaning of that If you have a long command that you need on separate lines you need to   add /// at the end of each line will be read as part of the same command.

11 Use the DO File! (4) New: Open a new do-file in a new tab in the Do-file Editor Open: Open a do-file from disk in a new tab in the Do-file Editor. Save: Save the current do-file to disk or in the directory you specified Print: Print the entire Do-file or what you select Find: Open the Find dialog for finding text Cut: Cut the selected text and put it in the Clipboard.

12 Use the DO File! (5) Copy: Copy the selected text to the Clipboard
Paste: Paste the text from the Clipboard into the current document Undo/Redo: Undo the last change/ Redo the last undo Toggle Bookmark: Turn on or off the bookmark on the current line. Bookmarks are a way to move quickly within the do-file. They are quite useful in long do-files or when debugging. Previous Bookmark: Go to the previous bookmark (if any).

13 Log file (1) Log files are very important because they keep a record of all the commands you typed and the resulting output during your work session. Stata does this by writing everything into a log file. Helpful tips: You should start a log file almost any time you start working in Stata. Best thing to do is to include log file in your do-file. This will create a file called ‘logfilename’.log that will save the Stata commands you execute, as well as the output. If you do not open a log file, your work will not be saved. You can name your log file anything you want. If you want to overwrite a log file you can use log using logfilename.log, replace. Be careful: the replace option overwrite the existing file without warning you.

14 Log file (2) Very important
In order to save the log file, you have to type log close when you are done. If you do not close the log file and close Stata, your log file will be lost! Write in your do-file: log using mylog.log To close the log file at any time type log off To re-open the log file type log on When you have finished everything type log close

15 Typical structure of a do-file
capture log close clear set more off log using “filename”.log, replace . log close Everything between the log using line and the log close line will be saved in the .log file replace in the log using line tells Stata to overwrite the log file (if it exists) Be sure this is what you need Stata assumes that each line in the do file is a command.

16 Macro 1 Macro assign a set of word or a number to a name. There are two types of macros: “Global” macros stay in memory until you leave Stata “Local” macros exist only with a program or a loop The syntax is relatively simple: global gmname = “ expression “ local lmname = “ expression “ To use these macros later, you must use special symbols to tell Stata they are macros: $gmname One use of the global macro is to store the name of the folder with the data. global PATH = “c:\resilience\Burkina_faso\2003\Stata” use $PATH\food_expenditure.dta, clear

17 Macro 2 In addition to saving you some time, this macro is useful if you share the program with others who have different names for the folders on their computer. By using the macro, your colleague can change the global command once rather than trying to change the path in every command that opens a file or saves a file. Local macros are used (among other places) in loops.

18 Use the DO File! (6) Let’s start our do-file FOLDERS! C data lab_stata
ado, data, basic, graphs, log, rawdata, temp raw/excel raw/stata/do, dta HINTS Every line is a command, unless diversely specified Case sensitive No empty spaces Be clear, be concise Clear your way Creates your global Aesthetics matters! Comments, not poems How many people in the room? We create the do file; save it; provide details; clear; give STATA a drive; open a log file; create global;

19 How to load data into STATA
import delimited command reads .csv and it can automatically detect either a comma or a tab as the delimiter. To import your data, type . import delimited filename In our case type import delimited workfile Then type list If data contain a combination of string and numeric variables, import delimited will determine the correct data type for each variable. insheet command “has been superseded by import delimited. insheet command continues to work but is no longer an official part of Stata from 13 version. Modus operandi is the same of import delimited.

20 How to load data into STATA
Copy and paste Open .dta files Write manually Transform from other statistical software The more automatized the safer you are Do not use typing

21 Language Syntax The basic Stata language is:
[by varlist:] command [varlist] [=exp] [if exp] [in range] [weight] [, options] [..]: optional qualifiers; varlist: is the list of variables we want include in the command; command: is the Stata command exp: is an algebric expression we might want to include range: is the range of an observation we might use (for example to create a new variable) weight: denotes a weighting expression options: is the list of options we want to include (there are proper options for each command) REMEMBER: EACH COMMAND HAS HIS OWN HELP: type help command

22 Inspecting variables Variable Types: Numeric
Stata reads them as number Different types: byte, int, long, float, double (different #’s of decimal points stored) String Stata reads as text String types are str1, str2, str3, etc… (indicates # spots available for a variable’s value) Words can never be in numeric format but numbers can be in string format Some commands are for numeric variables, some only for string, other for both


Download ppt "Stata Basic Course Lab 2."

Similar presentations


Ads by Google