Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating seasons of the year by using gen command

    Hi, all.

    I imported a dataset from excel to stata with data from seven years. I am trying to create a variable called wet season by year but I'm not having any luck. I am using the following command

    gen wet-season-2002 = if Date >= mdy(7,31,year(2002)) | date < mdy(10,31,year(2002)) Attached is the excel file I am trying to run on stata.

    Thanks in advance.
    Attached Files

  • #2
    Well, there are a few things wrong with your syntax. You can't have the - character in a variable name. And your expression year(2002) is probably not what you want, because it actually evaluates to 1965! You also can't have an if clause after the =. Finally, while it is perfectly legal to use | between logical expressions, it means "or," whereas I'm pretty sure the condition you want is "and", which would be represented by & in Stata syntax.

    If you read the Forum FAQ you will see that the use of attachments is discouraged. Not everybody who might help you uses Excel, and even among those who do, some are reluctant to download attachments of any kind from a stranger. I'm among the latter group, so I don't know what your data set looks like. Even if I were to open the Excel file, that still wouldn't tell me what your Stata data set looks like -- and that's what matters for writing Stata code. So avoid Excel attachments here in the future.

    I'm going to assume that somewhere in your Stata variable there is a date variable called Date. And you want to create a new variable, let's call it wet_season (_ is allowed in variable names, - isn't) which indicates whether or not that date falls between July 31 and October 31 of that same year. If that's what you want:

    Code:
    gen wet_season = inrange(Date, mdy(8, 1, year(Date)), mdy(10, 30, year(Date)))
    Note: The -inrange()- function uses >= and <=, whereas your original code referred to > July 31 and < October 31, so I have adjusted the bounds of the wet season accordingly. But I wonder if that's really what you meant.

    With that behind us, let me explain why year(2002) = 1965. Stata's -year()- function takes a Stata internal format date variable as its argument. Stata internal format date variables are represented as the number of days from January 1, 1960. Now, it turns out that 2002 days from January 1, 1960 is January 25, 1965. And the year of that date, of course, is 1965.
    Last edited by Clyde Schechter; 14 Apr 2019, 18:28.

    Comment

    Working...
    X