Microdata.no with support for loops and bindings
Long scripts with repeating structure can be simplified and systematized/generalized through the use of new functionality for bindings and loops. This is now possible through the use of the commands let and for.
Bindings
Bindings are script-local, named entities that allow you to generalize/automate scripts. Bindings are typically defined at the top of a script through the let
command, and can point to a numeric value, a string (text), a symbol, a binding, or combinations of these.
A symbol is a specific object that you will later refer to, usually a variable. It does not necessarily have to be an existing variable, but can be inserted into an expression where the variable is to be generated. Symbols should not be specified with quotes like strings.
A typical use case for bindings is when referring to years or dates in connection with importing variables, adjusting variables, running statistics etc. Then you can create a binding to a given year where it is relevant, and if you find out that you want to run for another year, you just adjust the value of the binding instead of all places in the script where the year is located. In the script, you use the $ sign followed by the name of the binding where you want to point to the binding.
Examples of defining bindings:
Use help let
for the full syntax description for bindings.
You can also use functions that operate only on bindings, also called procedures, in this way:
Use the command help-procedure
to read about what other procedures are available.
It is possible to assemble and combine several bindings into one expression by using the ++
symbols. This corresponds to concatenating values/text/symbols into one combined expression. Note that numbers combined with text become text. Example:
Example of using bindings:
In the example above, the procedure date_fmt()
is used. This is a very useful procedure that generates standard target dates for import commands (in the form YYYY-MM-DD), and can be used to make the script even more automated and streamlined. Example:
The commands above generate the following date values:
- 2021-01-01
- 2021-10-01
- 2021-10-20
Examples of how to generalize dates in an import expression:
equals
It may be wise to think carefully when using inline bindings, as the code can become harder to read with excessive use. Any error messages can also be harder to deal with. A rule of thumb is to define a fully concatenated binding through let
if it is used in several places.
Note that ++
has lower precedence than the mathematical operators +
and -
. This means that the expression
equals
Loops
Loops take the step further by allowing you to automate the use of repetitive command lines with the same structure. For example, if you want to import a variable several times with different target times. Then you can use the command for
in combination with in
to define how the loop should be iterated. You can either define a range using a colon (:
), or you can list the values to be iterated over. It is possible to use both integers, text and symbols as iterators, and it is possible to iterate over more than one iterator in a loop. Iterators can be seen as a binding that generates a set of values that go from the first to the last value in a loop. Note that iterator bindings are local so you can use the same names in different loops. Also, bindings defined inside a loop are local.
Example of using loops:
A typical analysis example where a set of variables measured over several years is imported, for a random selection of resident individuals in a given age group:
Another example where three datasets with the same structure are created, retrieving progression data measured over the year 2018 for job seekers, disabled people, and social assistance recipients, respectively. This is used to find the number of days in the different statuses (during 2018). The datasets are linked with the main dataset “bosatte” (“residents”) for each step:
Another example where cross-sectional data on working hours for all employees measured over all months in 2018 is retrieved:
You can find the complete introduction script here (can be pasted into the script window in microdata.no and run).
All information on this page can also be found in the user manual.