Announcement

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

  • Generating Date

    I have two variables: one is day (i.e. 1,2.......365/366) and other is rainfall. I want to convert that day in to date in DMY. I would appreciate your help. Thank you in advance.

  • #2
    Assuming that you know the year, then that would be

    Code:
    gen dailydate = mdy(1,1,year) + day - 1
    What about leap years?

    Comment


    • #3
      Thank you Nick for your quick help. Actually I have data set like this, I know that the first year is 2006 and then the last is 1987....in between there are 20 years. I know in which year the leap year occurs as well but could not convert v11 to data in DMY.
      v11 v12(rainfall)
      1 0.0
      2 1.2
      3 2.3
      3.3
      365 4.4
      1 3.8
      2 0.0
      3
      365/366 14.5

      Comment


      • #4
        I think the first two lines below will give you the year, then Nick's code gives you the daily date.
        Code:
        generate year = 2006
        replace year = cond(v11==1,year[_n-1]+1,year[_n-1]) if _n>1
        generate dailydate = mdy(1,1,year) + v11 - 1
        format dailydate %td

        Comment


        • #5
          It worked!!! Thank you so much William and Nick. Will be back to you again if anything comes beyond me .

          Comment


          • #6
            I wanted to append all the .dta files so I used the following code ,which worked.

            cd "F:\climate data\New folder"
            local filelist: dir . files "*.dta"

            clear
            gen source = ""
            foreach f of local filelist {
            append using "`f'"

            replace source = "`f'" if mi(source)
            }

            Now within that I want to loop the date code #4, but after appending all the files the v1 there is stacked. I used split v1, limit (2) to split the data after which there are three variables v1, v11 and v12. I used the code destring v11 than tried to run date code #4. I didn't get how to loop all these code together. I would appreciate your help.

            Comment


            • #7
              You led us to believe in #3 that you have a single file with the variable v11. And I see now you say that the data are in backwards order, with 2006 followed by 2005 followed by ... followed by 1987. If that is really true then the code I gave in #4 will produce years starting at 2006 and running to 2025, which is not a good thing. The replace statement should have subtracted 1 from the previous year rather than adding 1 to the previous year.

              Now we learn you have 20 separate data files that you first need to append together. You do not tell us how the files are named, you do not show us what the data looks like in a representative file, you refer to a variable v1 that we do not understand, you split v1 for reasons you don't explain, and then ask how to combine this code with the code provided in #4, which you already say worked for you.

              Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. The more you help others understand your problem, the more likely others are to be able to help you solve your problem. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using CODE delimiters, and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

              Comment


              • #8
                Sorry, for the inconvenience. As I am new to this forum and to stata, it's not intentionally. I have actually run the code that you provided but the year was increasing so I changed it and subtracted 1 instead of adding. The below is the data set after appending, like 1015.dta there are other 20 files.
                source v1
                1015.dta 1 0.0
                1015.dta 2 0.0
                1015.dta 3 0.0
                1015.dta 4 0.0
                1015.dta 5 0.0
                1015.dta 6 0.0
                1015.dta 7 0.0

                In order to run code #4, I use split v1, limit(2) after splitting v1 there are two more variables v11 and v12. Actually, I thought I could loop to other files once I know for one so I posted this, but, now I am unable to produce.

                Comment


                • #9
                  I'm reluctant to make suggestions. I see the following problems.
                  1. It is not clear to me what the files you are looping over are. Do they each contain 20 years worth of rainfall data for different locations, or are you using the loop to combine 20 files each containing one year's worth of rainfall data?
                  2. If each file contains a single year's worth of data, then for your append loop to work, the names of your files will have to be such that dir returns the in the precise order you want. In at least one example in help extended_fcn documentation of dir, the sample filenames are not in any discernible order.
                  3. If each file contains 20 years worth of data for a different location, then perhaps you're going about things the wrong way in trying to incorporate the date code. But all you told us of your problem is "within that I want to loop the date code #4" but you do not tell us in what way doing that does not work. Referring again to the Statalist FAQ Section 12.1 is particularly pertinent:
                    12.1 What to say about your commands and your problem

                    Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
                    ...
                    Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.
                  With all that said, and with the sincere plea that you read the Statalist FAQ and act on it so we can concentrate on solving your problem instead of trying to understand what the problem is, perhaps the following will help. You do not want to assign the date within the loop, instead you want to assign the date outside the loop, restarting the date at 2006 for each new value of source. Something like this may do.
                  Code:
                  cd "F:\climate data\New folder"
                  local filelist: dir . files "*.dta"
                  clear
                  gen source = ""
                  foreach f of local filelist {
                      append using "`f'"
                      replace source = "`f'" if mi(source)
                  }
                  split v1, limit(2)
                  destring v11
                  generate year = 2006
                  // subtract 1 from the year each time the day goes back to 1 AND the source is the same
                  // for the first observation from each new source, the  year will remain 2006
                  //   and the countdown starts over
                  replace year = cond(v11==1,year[_n-1]-1,year[_n-1]) if _n>1 & source[_n]==source[_n-1]
                  generate dailydate = mdy(1,1,year) + v11 - 1
                  format dailydate %td
                  Last edited by William Lisowski; 23 Apr 2017, 13:37.

                  Comment


                  • #10
                    Thanks for your wonderful support.

                    Comment

                    Working...
                    X