Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: database manipulation
From
"Dimitriy V. Masterov" <[email protected]>
To
[email protected]
Subject
Re: st: database manipulation
Date
Thu, 7 Oct 2010 17:20:05 -0400
> 1. Is there a way to convert the following date format (1/1/1975) into
> a date form that stata will recognize?
Try this (assuming your original variable is named date)
gen date2=date(date,"MDY")
format date2 %td
> 2. Is it possible to separate a single string field separated by
> commas into 2 separate fields (i.e. Dallas, TX into Dallas in one
> field and TX in another)? What about if a space was between the two
> fields (i.e. Dallas TX into two fields - one with Dallas and the other
> with TX)?
There are 2 ways of getting the state (assuming your city-state
variable is named cs). The second is probably safer since regular
expressions can go wrong.
gen state = regexs(0) if regexm(cs,"[A-Z][A-Z]$") // grab 2
CAPITALIZED letters at the end
gen state2 = substr(cs,-2,.) // grab the last two letters at the end
This is how you get the city.
gen city = regexs(1) if regexm(cs,"(^.*) ([, A-Z][A-Z]$)")
replace city = regexr(city,",$","") // gets rid of any commas on the end
HTH,
DVM
> Thank you in advance.
>
> Mike
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/