*! version 2.0.3 15nov2020

Dict

dict -- loop over multiple arguments.

Description

dict offers the tools to define and loop over dictionaries. A dictionary puts together several lists. It indexes their items according to their position on their respective list.
The dict prefix iterates over this index, allowing items of multiple lists to be passed to a loop command at the same time.

Note that the dict prefix can loop over any frame, not just those created by the function dict define.

Dict functions

Define a new dictionary:

dict define newframe, [from(file)] [cols(newvarlist)] [replace]

Display an existing dictionary:

dict list frame [if] [in], [cols(varlist)]

Return the number of observations in an existing dictionary:

dict count frame

Return the index of the observations that match a specified criteria in the dictionary frame:

dict getindex frame [if] [in], [match(type)]

Return the variable values in the observation that matches a specified criteria in the dictionary frame:

dict getvalues frame [if] [in], [match(type)]

Dict prefix

Loop over a dictionary:

dict frame, [code] [arg(char)]: commands referring to %arguments

Refer to a specific index in a dictionary:

dict frame, index(index) [code] [arg(char)]: commands referring to %arguments

Options

dict define

from(file): imports a dictionary from an external file. It accepts both .csv and .dta files.

cols(newvarlist): input the dictionary directly from the console. The names in newvarlist define the columns to be created in the dictionary frame.

- If individual elements should include spaces, enclose them with quotation marks.

- Type end to finish imputing the dictionary.

replace: specifies that it is okay to replace the data in memory, even though the current data have not been saved to disk.

dict list

cols(varlist): specifies the columns of the dictionary frame that should be displayed.

dict getindex

match(type): four matching types. The function defaults to unique if this option is not specified.

- unique: retrieves the index number of the observation that matches with the specified criteria. It returns an error if there are more than one matching observations.

- first: retrieves the index number of the first observation that matches with the specified criteria.

- last: retrieves the index number of the last observation that matches with the specified criteria.

- all: retrieves the index numbers of all the observations that match with the specified criteria.

dict getvalues

match(type): four matching types. The function defaults to unique if this option is not specified.

- unique: retrieves the index number of the observation that matches with the specified criteria. It returns an error if there are more than one matching observations.

- first: retrieves the index number of the first observation that matches with the specified criteria.

- last: retrieves the index number of the last observation that matches with the specified criteria.

dict prefix

index(index): tells the dict prefix not to loop over the entire dictionary but rather to refer only the nth observation of specified dictionary.

code: displays the line code used in each iteration of the loop. Rarely used.

arg(char): changes the character used to call an argument in the command. The default character is %. Rarely used.

Example

Consider the database:

id geo time value

1 AT 2017 0.72
2 AT 2018 0.73
3 AT 2019 0.74
4 BE 2017 0.63
5 BE 2018 0.65
6 BE 2019 0.65
7 DE 2017 0.75
8 DE 2018 0.76
9 DE 2019 0.77
. . . .
45 UK 2019 0.75

And the .csv file:

acronym name

AT Austria
BE Belgium
DE Germany
. .
UK United Kingdom

dict define map, from(countries.csv)
gen country = "", after(geo)
dict map: replace country = "%name" if (geo == "%acronym")

id geo country time value

1 AT Austria 2017 0.72
2 AT Austria 2018 0.73
3 AT Austria 2019 0.74
4 BE Belgium 2017 0.63
5 BE Belgium 2018 0.65
6 BE Belgium 2019 0.65
7 DE Germany 2017 0.75
8 DE Germany 2018 0.76
9 DE Germany 2019 0.77
. . . . .
45 UK United Kingdom 2019 0.75

dict define mylabels, cols(varname label)
id "ID"
geo "Country code"
country "Country name"
time "Year"
value "Employment rates"
end

dict mylabels: label variable %varname "%label"
describe

+
+
+
| Variable | Label |
+
+
+
| id | ID |
| geo | Country code |
| country | Country name |
| time | Year |
| value | Employment rate |
+
+
+

Author

Daniel Alves Fernandes

European University Institute

daniel.fernandes@eui.eu