I have a string variable in Stata with entries like the following
var1 12.34 var2 23.45 var3 34.56
var2 54.32 var3 67.32
var1 32.32 var3 23.45
I want to create three variables with the above string variable by extracting only the numbers (variable names var1, var2 and var3). However, not all entries have all three variables. So, if I simply extract the numbers by parsing, entry 2 will have var2's value under var1's value and var3's value under var2's value.
Is there a way to make all the entries uniform such that if var2 is missing, the entry needs to be replaced with "var2 0" in between var1 and var3 and if var1 is missing, the entry needs to be replaced with "var1 0" before var2.
I am ignoring the question as inserting words (really two words) isn't needed at all to parse your input as you wish. Indeed, inserting words would be awkward as you need to find which words are present before you can determine which to insert (and where to insert them).
Here's one way to do it. You will need to change 3 wired into this example code if you really have more variables than 3 packed inside your data variable.
Here is another way to do it that doesn't wire in the number of variables or what they are named.