I have a bunch of values in a dataset that are formulated like 2000-3222
and 10/1-10
.
I would like to split these so that it lists 2000
, 2001
etc. and 10/1
, 10/2
etc., all in their own rows.
Is there any command to do this in Stata or R?
EDIT:
Example data:
input int SRNo str200 SchemeName str30 CTSNo1 str4 CTSNo2
69 "Khimji Nagar SRA Co-op.Housing Society Ltd." "467" ""
70 "Jai Bhavani CHS Ltd. (Proposed)" "7 (Pt.)" ""
71 "Shivshakti SRA CHS Ltd." "364 ‘A’" ""
72 "Shree Ram CHS Ltd. (Prop.)" "96 (Pt.) -99(Pt.)" ""
end
Assuming all values look like your example and you variable(s) is of type string:
This solution is useful if you only need a certain part of the original variable.
EDIT:
Using @Nick's excellent suggestion:
As you can see, this solution will give you two variables for
string1
and another two forstring2
each containing both (separate) parts of the original variable.Based on your example data (in which I added a few more observations to make things more illustrative), you need something along the following lines:
The above code snipped produces the desired results:
EDIT:
The
forvalues
loop in my solution above is not necessary. A different way of doing this, which avoids looping over observations, is the following: