Stata v11 Intro

Embed Size (px)

DESCRIPTION

Stata

Citation preview

  • 5/21/2018 Stata v11 Intro

    1/5

    IDEC8017-14-01

    1

    Crawford School of Economics and Government

    Introduction to Stata (v11.01)

    Q1. How do I get started in Stata?Log on to the computer in the Crawford labs using your student id and password. Then go tothe Start Menu and select

    Start Menu => All Programs => ... =>Stata.

    Q2. What are all these windows?

    Command:For you to type your command and when you hit enter, the command will be

    executed

    Results:Results appear in this window when you type commands in the command window

    Variables:Lists the names and labels of all the variables in the current data file, including anyyou might have created

    Review:Keeps record of all your commands

    Use the Page Up and Page Down keys to cycle through past commands. If you select a

    past command in the Review window, it will appear automatically in the Command

    window. You can edit the command before executing it again.

    Q3. How can arrange the window layout to suit my taste?

    Right-click in the Results Window, in the pop-up menu you will be able to choose yourpreferred font and other preferences

    Save your preference by going to Stata/Preferences/SavePreferences/Name

    Q4. How do I get a data file into Stata?

    It depends on the form of the data file. If it is a formatted Stata data file (file name type *.dta)

    then you could forget the startup routine and just double click the data file.

    If you are already in Stata, go to the menubar and select File => Open..., or alternatively selectthe Open File icon on the taskbar, and then navigate to choose the file you want.

    Or you can type in the Stata Command window:

    use \filename.dta, clear

    This assumes you want to type the whole path for Stata to find the file. It may be easier tomove the file into your H: drive first, and then you can omit the path specification. The option

    clear removes any earlier data that might be in Statas memory.

    There are other commands for opening different types of data files, such as raw text files and

    spreadsheets (from Lotus, Excel, etc.).

    Q5. How do I see my data in Stata?

    If you want a description of the kindsof variables and their labels, type

    describe varlist

    1Note that the Stata version available in the Lab is more updated.

  • 5/21/2018 Stata v11 Intro

    2/5

    IDEC8017-14-01

    2

    where varlistis a list of variable names (if you leave it blank Stata interprets the list as being

    allvariables).

    If you want statistical summaries including means and standard deviations, type

    summarize varlist

    Hint:You only need to type enough of a command name to make the command unique. So

    you could type summ varlistor even su varlist(but s varlistis not good enough).

    If you want to see the numbers themselves, type

    list varlist

    Caution:If there are lots of observations the result could fill many screens. You can cut in ona command by using key combination Ctrl-Break. Next time you could look at selected

    observations (See Q6 below).

    Hint:Another way to see your data is to use the Data Editor, or for more safety the Data

    Browser. These bring up the data in a spreadsheet format. They are started by command orfrom the menubar or the toolbar.

    Q6. How do I transform or create a variable?

    The command has the form

    generate newvar=expression

    where expressionis practically any common mathematical expression.

    Some operators

    RelationalArithmetic Logical (numeric and string)

    -------------------- ------------------ ---------------------

    + addition & and > greater than- subtraction | or < less than

    * multiplication ! not >= > or equal

    / division ~ not ,

  • 5/21/2018 Stata v11 Intro

    3/5

    IDEC8017-14-01

    3

    - Variable list is a list of variables separated by (a) blanks

    - The range may be:

    1 the first observation

    2 the third observation

    -1 the last observation

    -3 the third observation from the last

    1/5 observations from 1 to 5

    If inrange is not specified, the range will be 1/-1 (all observations) by default

    - In ifexpressions, we can use logical operators. Please see the full list of operators

    below.

    - options may vary across commands.

    Q8. How do I select a subset of observations for transformation or analysis?

    If you want to select on the observation index, use the in option. The command

    summ myvar in 1/25

    does the calculation on just the first 25 observations. On the other hand

    gen myvar=hisvar if hisvar, =,

  • 5/21/2018 Stata v11 Intro

    4/5

    IDEC8017-14-01

    4

    will find the commands in the menubar and the toolbar. In a .do file you can turn logging on

    and off with the commands

    log using h:\tut1.log and log close

    There are two types of log files, one which is just plain text and useful for copying into other

    programs (called .log), another which is more highly decorated output but not much use in

    other programs (called .smcl).If the log file already exists, the first command above will have to be modified to be either

    log using h:\tut1.log, append

    or log using h:\tut1.log, replace

    Q12. How can I make notes to myself (comments) in a .do file?

    Any line that begins with an asterisk * is treated as a comment. Stata only echos the line and

    then ignores it.

    Q13. Some lines in my .do file are pretty long. How can I break them?

    Stata normally takes the carriage return at end of a line as the indicator that a command iscomplete and ready to be executed. One trick is to define a new end of command character.

    #delimit ;list onevar

    twovarthreevar;

    #delimit cr

    This tells Stata to keep reading until it reaches a semicolon before the command is complete

    and ready to be executed. All extra spaces and carriage returns are ignored. The feature can be

    turned off by resetting the delimiter character to the carriage return. The above example hasthe same effect as

    list onevar twovar threevar

    Hint: The same trick lets you put several short instructions on the same line of a .do file.

    Q14. How do I find out more about Stata commands?

    Many commands can be specified from the menus Data, Graphics and Statistics.

    There is a very extensive online help system. It distinguishes between help on a command

    name (menu: Help => Stata Command...) and help on a topic (menu: Help => Search...).

    It is often much more efficient to search in the internet to know what to look for in Statabefore resorting to the Help Resources in Stata.

    For example: google stata descriptive statistis beginners, on the first page, two very good

    websites are that we highly recommend you to visit

    http://data.princeton.edu/stata/

    http://www.princeton.edu/~otorres/Excel/excelstata.htm

    Resources for learning Stata

    http://www.stata.com/links/resources-for-learning-stata/

    A must-visit website on visual overview for creating graphs in Stata

    http://www.stata.com/support/faqs/graphics/gph/stata-graphs/

  • 5/21/2018 Stata v11 Intro

    5/5

    IDEC8017-14-01

    5

    Example .do file(assumes you have the data file wages1.dta in a folder h:\tut1)

    *Change the directory. Close any existing log files and start a new log.

    cd h:\tut1

    capture log close

    log using tut1.log, replace

    *Load the data and have a look at it.

    use wages1.dta, clear

    desc

    list wage male in 1/10

    *Do some calculations and make some graphs.

    summ wage if male==0

    summ wage if male==1

    plot wage school

    twoway (scatter wage school)

    regress wage male

    regress wage male school

    *Close the log file.

    log close

    *****************