[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: RE: RE: how to split a variable?
Using -destring- has one advantage here: if there are
non-numeric characters in the variable, -destring-
Otherwise if you know that there are no non-numeric
characters, you can do it directly:
gen house_no = real(substr(var,1,4))
gen indiv_no = real(substr(var,5,2))
You can test in advance that -var- is all numeric in content:
assert real(var) < .
If any value of -var- contains non-numeric characters,
the brute force application of -real()- will yield missing.
Conversely, all is well if all the results of -real()-
are non-missing numeric values.
> try this (I'll assume your variable is called "var"):
> gen house_no = substr(var,1,4)
> gen indiv_no = substr(var,5,2)
> destring house_no, replace
> destring gen indiv_no, replace
> You can have a look at other string functions in the [D]
> manual under "functions" or just by typing "help string functions"
> I have a string variable with 6 characters wide. The first
> four characters are
> household numbbers, and the last two are the individual
> members in a household.
> I want two variables, namely, the household and the
> individual. Eventually, I
> want to make them numerical. So, being string variable isn't
* For searches and help try: