More compressed recoding of variables: inlist and inrange

The functions inlist and inrange can be used to set up compressed code expressions for coding variables, so that scripts are more clear and less resource-demanding to run.

These functions are ideal if you want to create extensive code conditions, usually in combination with generate and replace expressions, e.g. if you want to make a rough grouping of municipalities, where you need to list larger sets of municipality codes.

The inlist function

When coding/recoding variables based on long if conditions, the function inlist can be useful to use. An example is if you want to create a variable that takes the value 1 for people who live in a sample of 100 municipalities. This would have given a generate or replace command with a very long if expression. When using inlist, this can be made more compressed by listing all the municipality numbers separated by commas inside function brackets.

The logical function inlist is set to 1 (“true”) if the value in the first argument is found among the remaining arguments.

The arguments to the function can be both variables and values ​​(all types).

Examples:

  • generate var = 1 if inlist(marital_state, 1, 3, 5)
    (inlist function = 1 (“true”) if marital_state = 1, 3 or 5)
  • generate var = 1 if inlist('1', regstat09, regstat10, regstat11)
    (inlist function = 1 (“true”) if at least one of the regstat variables = ‘1’)

The examples above correspond to these expressions:

  • generate var = 1 if marital_state == 1 | marital_state == 3 | marital_state == 5
  • generate var = 1 if regstat09 == '1' | regstat10 == '1' | regstat11 == '1'

The inrange function

In addition to inlist, there is another logical function that can make it easier to create conditions based on intervals: inrange. If conditions based on intervals rarely become particularly long, since you usually only need to specify a minimum and a maximum value. But if you operate with several intervals in one and the same if-expression, inrange can be useful. In any case, the code syntax becomes more compressed compared to regular if statements when using this function.

The logical function inrange is set to 1 (“true”) if the value in the first argument is higher than or equal to the value in the second argument and lower than or equal to the value in the third argument.

The arguments to the function can be both variables and values ​​(all types).

Example:

  • generate var = 1 if inrange(wealth, 500000, 1000000)
    (inrange function = 1 (“true”) if 500000 <= wealth <= 1000000)

The example above corresponds to this expression:

  • generate var = 1 if wealth >= 500000 & wealth <= 1000000

Click here for a practical example of using inlist and inrange.