[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: Constructe a string variable
Suppose I have a string variable with 3 position on each observations.
Each of these observations contains some values range from 1-999.
(though all entries are numerical, but variable X is of string format)
My purpose is to reconstruct this variable to be the following formate:
i.e. I want to replace the left postition for each observation with "0" if
I have tried the following method:
gen str1 X1=substr(X, 1, 1)
gen str2 X2=substr(X, 2, 1)
gen str3 X3=substr(X, 3, 1)
my idea is that if I can divide original variable(X) into 3 pieces, and then I
can replace the missing( or " ") into 0, and then re-combine the variable.
However, in the above data example.
the X1 (=substr(X,1,1) is always equal to:
(the corerct one should be:
That means, the function of -string- or -substr- in Stata will always return
the first values of that observations no matter its' real position.
I am just wondering whether anyone knows an easier function or command to deal
* For searches and help try: