Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: data extraction by date

From   David Kantor <>
Subject   Re: st: data extraction by date
Date   Tue, 02 Dec 2003 11:53:25 -0500

At 10:49 AM 12/2/2003 -0500, Mike Carney wrote:
I have a series of stock returns sorted by Stata's standard date form (that is, when 0 = 1Jan 1960). Is it possible for me to create a data set with 1 for all dates ranging from 1 jan to 10 jan (including all years) and zero otherwise. I've tried

. gen var2 = var1 if doy(1/4), month(1)

but it doesn't seem to work. Any help would be appreciated immensely!!!

A couple of people have already replied while I was writing this, but here is my advice anyway.

A few things about your question and your alleged Stata command don't make sense.

First, I believe that you want to create a variable -- not a dataset -- valued 1 for all dates ranging from ranges 1 jan to 10 jan. (If you want a dataset, you can subsequently filter on the variable.)

Secondly, the argument to the doy function is a constant expression, so there is no real sense to using it that way. (Also, that constant expression is 1/4 which rounds to 0 because doy likes integer arguments.)

Third, where is your date variable? (Is it var1? If so, then, if the rest of your formula were okay, the values would be var1 in the targeted cases -- not 1.)

Fourth, what is the supposed significance of -, month(1)- ? I should note that -generate- does not allow any options as far as I know.

Fifth, if you write,
gen var2 = some_expression if some_other_expression

then the cases where some_other_expression is false will receive a missing value -- not zero.

Here is what I would do. Suppose that your date variable is called date. Then code as follows.

gen int doy = doy(date)
gen var2 = doy >=1 & doy <=10

You can subsequently drop doy. You could have, alternatively done, in one command, ...
gen var2 = doy(date) >=1 & doy(date) <=10

and one can argue about which is better.

I hope this helps.
-- David

David Kantor
Institute for Policy Studies
Johns Hopkins University

* For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index