Programming functions 9
For example, you have two datasets in memory. The current frame is named
default and contains 57 observations. The other dataset, we will assume, is stored
in frame xdata. It contains different variables but on the same 57 observations.
The two datasets are in the same order so that observation 1 in default corresponds
to observation 1 in xdata, observation 2 to observation 2, and so on. You can
type
. generate hrlywage = income / frval(xdata, hrswrked, n)
This will divide values of income stored in default by values of hrswrked
stored in xdata.
The first thing to notice is that frval()’s first two arguments are not expressions.
You just type the name of the frame and the name of the variable without embedding
them in quotes. We specified xdata for the frame name and and hrswrked for
the variable name.
The second thing to notice is that the third argument is an expression. To emphasize
that, let’s change the example. Assume that xdata contains 58 instead of 57
observations. Assume that observation 1 in default corresponds to observation
2 in xdata, observation 2 corresponds to observation 3, and so on. There is no
observation in default that corresponds to observation 1 in xdata. In this case,
you type
. generate hrlywage = income / frval(xdata, hrswrked, n+1)
These examples are artificial. You will normally use frval() by creating a
variable in default that contains the corresponding observation numbers in
xdata. If the variable were called xobsno, then in the first example, xobsno
would contain 1, 2, . . . , 57.
In the second example, xobsno would contain 2, 3, . . . , 58.
In another example, xobsno might contain 9, 6, . . . , 32, which is to say, the
numbers 2, 3, . . . , 58, but permuted to reflect the datasets’ jumbled order.
In yet another example, xobsno might contain 9, 6, 9, . . . , 32, which is to
say, observation 1 and 3 in default both correspond to observation 9 in xdata.
xdata in this example might record geographic location and in default, persons
in observations 1 and 3 live in the same locale.
And in a final example, xobsno might contain all the above and missing values
(.). The missing values would indicate observations in default that have no
corresponding observation in xdata. If observations 7 and 11 contained missing,
that means there would be no observations in xdata corresponding to observations
7 and 11 in default. ( frval() has a second syntax that allows you to specify
the value returned when there are no corresponding observations; see below.)
Regardless of the complexity of the example, the value of xobsno in observation
j is the corresponding observation number i in xdata. Regardless of complexity,
to create new variable hrlywage in default, you would type
. generate hrlywage = income / frval(xdata, hrswrked, xobsno)
That leaves only the question of how to generate xobsno in all the above situations,
and it is easy to do. See [D] frlink.