Bookmark and Share

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: Fwd: Converting weekly data of form yyyyww


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Fwd: Converting weekly data of form yyyyww
Date   Wed, 25 Jul 2012 16:15:43 +0100

-encode- is indifferent to the numeric or other meaning of the strings it maps to numeric. That's a reason why -destring- is needed.

On your main problem, it seems that most weekly data come with one of two definitions, that weeks start or finish on particular days of the week. This is the key to successful data processing of weekly data. I wrote a Stata Journal Tip on week assumptions in 2010. Google for the reference or for Statalist posts that cite it. I intend to write more on this, but probably not until after the Stata Conference in San Diego starting tomorrow.

Nick

On 25 Jul 2012, at 15:31, Ruth Gilgenbach <[email protected]> wrote:

Thanks for your quick reply. This was helpful--I didn't realize that's
what -encode- does.

I'm sure there's a more elegant solution, but something like:

******************
gen year = substr(dates,1,4)
destring year, replace
gen week = substr(dates,5,2)
destring week, replace
gen delivery =yw(year,week)
format %tw delivery
*******************
works, except for the week 53 problem.

On Wed, Jul 25, 2012 at 8:14 AM, Nick Cox <[email protected]> wrote:

Don't -encode-, -destring-,  or use -real()- directly, which avoids
creation of new variables. For example, -encode- would map "2009", "2010",
"2011" to 1,2,3, not at all what you want.

Mapping anybody's else "weeks" onto Stata's weeks is still going to be a
problem, however.


On 25 Jul 2012, at 13:55, Ruth Gilgenbach <[email protected]> wrote:

I have a data set with variable "dates" which are of the form yyyyww, and I am trying to convert them to a useable format, and am having no
luck. I am running Stata 12.

For example, if I use the following
************************
inp str06 dates
201002
200953
201102
200935
end

gen delivery = weekly(dates,"YW")
*************************

I get the result: (4 missing values generated) rather than the correct
result.

I know that the 53-week year in 2009 is also going to pose a problem,
but this problem persists even in the absence of week 53.

I have also attempted to split the string into components and build
the dates from these, but I receive the same problem, that missing
values are generated rather than the values themselves:

******************
inp str06 dates
201002
200953
201102
200935
end

gen year =substr(dates,1,4)
encode year, generate(nyear)
gen week = substr(dates,5,2)
encode week, generate(nweek)

gen delivery2 = yw(nyear,nweek)
*********************************
*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index