Reputation: 131
I have variables:
set obs 1000
g X= rnormal(0,1)
egen t=fill(1 2)
I need to generate a new variable, that would consist of one value: the first value of X. I tried:
separate X, by(_n <= 1)
and
gen X1 = X if t<=1
But these options give me a vector 100x1 with the first value - the value I need and 99 of empty cells. How can I generate simply a one value variable:1x1?
Upvotes: 0
Views: 5136
Reputation: 19375
you have to write two lines of codes my friend
gen X1 = X if t<=1
replace X1=X1[_n-1] if missing(X1[_n])
and
local my_parameter=X1[1]
and then you happily use your `my_parameter' macro in your arma regressions
. di `my_parameter'
-.44087866
remember, to use a macro (more usually called a parameter in other languages) in a regression in stata you need to embed its name with `'
Upvotes: 1
Reputation: 1338
I don't disagree with the other two helpful answers already posted, but when I read "How can I generate simply a one value variable:1x1?", I cant help but think you are looking for a scalar or a macro.
If that is true they you might be better off with
sum X in 1
di r(mean)
From here, storing this value to use later is trivial:
sca MyVar = r(mean)
From help summarize
, you will see that sum
stores the mean, min and max among many other useful measures.
To see yourself, run return list
after the call to sum
to see what is returned.
By using in 1
you are restricting the summarize
command to only run for the first observation. Naturally then, many of the scalars returned by summarize
will equal the value you desire.
If you wish, you may also precede sum
with quietly
to suppress the output, or add the meanonly
option to calculate only the mean along with suppressing the display.
Upvotes: 1
Reputation:
Perhaps this will point you in a helpful direction
generate X1 = X[1]
The point is that X[1] is the value of X in the first observation. Now having said that, what do you want to do with that value? Your dataset has 1000 observations. Do you want a local or global macro? A scalar? If you intend to use it in a formula applied to all 1000 observations, then perhaps a variable with the same value for every observation will be sufficient.
Upvotes: 0