# Generating New Variables

In this manual, we will be generating new variables, computing statistics based on gretl output, and performing matrix calculations using gretl’s scripting language. That means we will be generating series, scalars, matrices, lists, and even strings. How does gretl handle these?

Gretl is actually very forgiving in the generation of new results. The ‘mother’ command for doing this is genr. The genr command pretty much does it all. In the appropriate context, series, scalar and matrix are synonyms for this command.

So, to create a new scalar result, say create a constant c that is equal to 3, you could use scalar c = 3 or genr c = 3. The scalar and genr commands let gretl know that you are calculating something and calling it c.

To create a new variable, you can use the series command or genr. Suppose you have a variable in your dataset called food_exp. You want to create a new variable as the natural logarithm of food_exp. This can be done using series or genr (e. g., series l_food_exp = ln(food_exp)). In the context of a genr or series formula, variables must be referenced by their names, not their ID numbers. The formula should be a well-formed combination of variable names, constants, operators and functions. Further details on some aspects of this command can be found in the Gretl Users Guide.

As you have seen, a genr command may yield either a series or a scalar result. For example, the formula x2 = x * 2 naturally yields a series if the variable x is a series and a scalar if x is a scalar. The formulae x = 0 and mx = mean(x) naturally return scalars. The genr command handles both cases seamlessly.

Under some circumstances you may want to have a scalar result expanded into a series or vector. You can do this by using series as an “alias” for the genr command. For example, series x =

In many cases, you can even omit the genr, series, scalar, or matrix statements and gretl will figure out what to compute based on what is on the right-hand side of your equation. This is dangerous though, because you may inadvertently be trying to compute objects with incompatible dimensions or of incompatible types.

In this book, I may at times use genr instead of the preferred series command to create new variables. I am told by members of the gretl team that it is better practice to call things what they are and so series, scalar, and matrix are better than the generic (but equally effective) genr. One of the amazing things about the gretl language is that omitting these commands altogether usually works anyway. Still, I think there are good reasons to get started on the right foot by adopting good programming practices.[3] There are at least three commands that demand the use of genr, rather than series. These involve creating a time index (genr time) and dummy variables (genr unitdum and genr dummy). These cases will be pointed out when we get to them.

## Leave a reply