IS BF 2024 25 Week 1
IS BF 2024 25 Week 1
Myself Appendix
Education & Academic Position
Publications
Current Research
Introduction
Why Stata?
References
Books
Websites
Introducing Stata
Program Layout
Getting started
Loading data in Stata
Main commands
2 of 45
Myself
Education & Academic Position
▶ Education:
▶ B.Sc. in Law (University of Messina)
▶ M.Sc. in Economics (University of York)
▶ Ph.D. in Economics & Institutions (University of Messina)
▶ Ph.D. in Economics (University of Essex)
▶ Current Position
▶ Associate Professor of Economics (University of Messina)
▶ Previous Position
▶ Visiting Lecturer (School of Economics & Finance, Queen Mary
University of London)
3 of 45
Myself
Publications
▶ Main Publications:
▶ Book:
▶ Happiness and the Pursuit of Freedom (with S. Bavetta & P. Navarra),
Cambridge University Press, 2014.
▶ Selected Papers:
▶ Autonomy in Decision-Making and Freshmen’s Performance at
University: Evidence from Italy (with S. D’Arrigo, L. Leonida, E.
Muzzupappa & P. Navarra), Studies in Higher Education, 2024.
▶ Does Economic Liberalization Foster Corporate Investment? Theory and
Evidence from US and Canadian Firms (with A. Iona, L. Leonida, M.
Limosani & P. Navarra), Socio-Economic Planning Science, 2024.
▶ Freedom, Diversity and the Taste for Revolt (with A. Marino & P.
Navarra), Kyklos, 2021.
▶ A tale of soil and seeds: the external environment and entrepreneurial
entry (with D. Baglieri, M. Mudambi & P. Navarra), Small Business
4 of 45 Economics, 2016.
Myself
Current Research
▶ Current Research:
5 of 45
Introduction
▶ The aim of this workshop is to learn how to use Stata to carry out
empirical research.
▶ Notice that through the file the words in orange contain links,
which we will re–direct you to the relevant web page.
6 of 45
Introduction
Why Stata?
10 of 45
Introducing Stata
Program Layout
▶ After opening the software, you may notice that it is divided in five
different windows:
1. Left window (History): Here the list of the past executed commands
is reported (if you need you can click on one of the previous
commands to replicate it).
2. Top-middle window (Results): it displays the results of each
command you executed.
3. Bottom-middle window (Command): here you can write the
command you want to execute
4. Top-tight window (Variables): it contains the list of the variables
included in the dataset that you are currently using;
5. Bottom-right window (Properties): it reports some information
about the file in use (usually not very informative).
11 of 45
Introducing Stata
Program Layout
12 of 45
Introducing Stata
Program Layout
▶ Some buttons at the top of the main page are worthy of attention.
▶ While the first three allow you to open a dataset, to save it and to
print, you may want to consider the following:
▶ Button n. 4: it opens the *.do file editor, i.e. a file where you can
write all your code and run it;
▶ Button n. 5: it opens a Data editor (however, my suggestion is to
arrange your data in Excel);
▶ Button n. 6: it opens a Data browser (you may inspect the data, but
you cannot edit them);
▶ Button n. 7: a break button to stop the execution of your code, if
needed.
13 of 45
Introducing Stata
Program Layout
14 of 45
Introducing Stata
Program Layout
▶ Finally, the menu bar at the top of the main window is very useful.
In particular:
▶ Button 1: It allows you to open and save datasets in Stata format, to
import data from different format to Stata, to export your dataset in
another format, to use example datasets, to change your working
directory;
▶ Button 2: It allows you to manage your data;
▶ Button 3: It allows you to generate different type of graphs (really
many!);
▶ Button 4: it contains all the commands that should be used to carry
out analysis in Stata;
▶ Button 5: it allows you to access help resources, such a Stata manuals
(they are installed along with the software), other resources, the
Statalist forum and Stata journal.
15 of 45
Introducing Stata
Program Layout
16 of 45
Introducing Stata
Tip 1 – Inspecting a Stata command
Although you can use the help tab, you can get information
about any specific command in Stata in other ways. For
instance, if you already know the command that you want to
inspect, simply type in the command window:
help aaa
help regress
▶ Notice that the path of the folder where the dataset is located
must be under quotation.
▶ Instead, if your data file has a different format, the easiest thing to
do is to click on File -> Import. You see that Stata supports
different formats. Simply choose the one you like and follow the
instruction.
19 of 45
Getting started
Loading data in Stata
▶ For example suppose that our data are in Excel. You choose File
-> Import -> Excel spreadsheet. The following window appears:
20 of 45
Getting started
Loading data in Stata
22 of 45
Getting started
Hint 1
Suppose you do not know how to write down the line of code
to import data. You can use the menu bar to import data that
are saved in Excel, for instance.
After you import the data using the menu bar, go to the
Review window and click on the last executed command. In
the Command window, you will see which is the Stata syntax
to import the data. This is an evidence that learning Stata’s
syntax is easy.
Next time you know that you can write directly the line
of code above.
23 of 45
Getting started
Hint 2
There is another way to import data from Excel to Stata. You
can simply copy and paste the data.
Open the Excel file, highlight all the data (do not high-
light empty cells at the end of the data) and copy them using
CTRL+C.
24 of 45
Getting started
▶ We will now review some commands in Stata. They are those that
you will frequently use in your research.
▶ Stata can generate new variables using those contained in your
dataset.
▶ This is done using the command generate – or its abbreviation
gen.
▶ For instance, we want to generate the log of the variable sandp.
26 of 45
Getting started
generate
▶ In Stata:
preserve
keep date microsoft
restore
28 of 45
Getting started
preserve, restore, keep, drop and replace
▶ keep date microsoft allows you to keep only the two variables in
the list.
▶ restore allows you to recall the dataset in the shape it was
previously.
▶ replace allows us to change the content of a variable. For
instance, we generate previously a new variable, named lnsp.
Suppose that for whatsoever reason, we want to multiply that
value by 100. Hence we type:
replace lnsp=100*lnsp
29 of 45
Getting started
sort and order
▶ The command sort allows you to sort your data in an ascending
order.
▶ If you like to sort them in descending order, you can use the more
general gsort, i.e. gsort- date (recall to include “-” if you want
in descending order).
▶ The command order with the option first instead allows you to
move the columns. For instance, the line code order microsoft,
first indicates that the first column of the dataset should contain
the variable microsoft.
▶ If you type order microsoft sandp, first, the first two
variables of the dataset will be microsoft and sandp.
30 of 45
Getting started
if
gen dummy=0
replace dummy=1 if sandp<1000
▶ Notice that if the conditional statement contains an equality, i.e.
replace dummy=1 if sandp==1000 we need to use “==” rather
than simply “=”.
31 of 45
Getting started
& and |
gen dummy2=0
replace dummy2=0 if sandp<1000 & microsoft<25
32 of 45
Getting started
tostring and destring
▶ When you upload data in Stata, the variables are marked in black.
If, instead, they are marked in red, this means that the software
reads the number as a string.
▶ Stata allows you to convert a string to a number, using destring,
and, viceversa, using tostring.
▶ For example, the variable date is a number.
33 of 45
Getting started
tostring and destring
34 of 45
Getting started
substr
▶ Suppose we want to ask the software to extract the first two digits,
using substr:
gen newvbl=substr(date, 1, 2)
▶ In the line above substr is the command to extract the code. The
first number in the brackets, 1 denotes the location in the string
where to start, while 2 indicates the length of the string to extract.
▶ Hence, we ask the software to read the first two elements in the
sting.
35 of 45
Getting started
Working with dates
gen date2=date(date,"DMY")
format date2, %td
38 of 45
Getting started
Installing new commands
▶ Although you can download your data and arrange them in Excel
before importing them in Stata, there is another way to obtain
financial data directly in Stata.
▶ Specifically, we can use the user–written routine getsymbols
▶ Notice that the line ssc install is the standard way to download
an additional command in your machine.
▶ There are some advantages/disadvantages in using it.
40 of 45
Appendix
getsymbols
▶ Advantages/disadvantages of getsymbols:
▶ It allows you to access several sources of data (Yahoo Finance,
Google, Alpha Vantages, Quandl);
▶ Depending on the source you access, you can download data for
stock, currency and cryptocurrencies data;
▶ Alpha Vantages database allows you to download high frequency data,
too (1m, 5m, 15m and 30m)
▶ However, some data cannot be downloaded (for instance, corporate
data from Yahoo finance, although available).
▶ An example about the usage of this routine (as well as other useful
tools for financial analysis) can be found in the presentation
delivered by Alberto Dorantes at the 2019 Stata meeting.
41 of 45
Appendix
getsymbols
▶ However, you should use exactly the same ticker that is used in the
source that you want to access. For instance, if you need data for
S&P500, you should use ˆGSPC if you want to get information
about that market index.
▶ Below I provide an example about the usage of this routine.
42 of 45
Appendix
getsymbols
43 of 45
Appendix
getsymbols
Using that line of code, we could obtain the required data, di-
rectly in Stata. However, we may obtain additional data, we are
not interested in. Some editing is required. Hence:
drop r_*
drop R_*
drop volume_*
renpfix p_adjclose_
rename _GSPC sp500
rename MSFT microsoft
rename ORCL oracle
44 of 45
Appendix
getsymbols
45 of 45