Announcement

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

  • loop using dates

    My task is to produce many reports of each month using an historic dataset(the dataset of each month are appended in this biggest dataset). I also have to rename each of these reports in this way : report_march.dta , report_april,dta... etc

    In the biggest data set I have the date variable in string format, for example "31/03/2016":

    Big dataset :

    clear all
    input str10 date str10 value
    "29/02/2016" "312"
    "30/04/2016" "312"
    "30/06/2016" "44"
    "30/11/2015" "67"
    "31/01/2016" "7"
    "31/03/2016" "5"
    "31/05/2016" "21"
    "31/07/2016" "3"
    "31/08/2016" "4"
    "31/10/2015" "5"
    "31/07/2016" "11"
    "31/08/2016" "87"
    "30/06/2016" "89"
    "30/11/2015" "14"
    "31/01/2016" "15"
    "31/03/2016" "3"
    "31/05/2016" "17"
    "31/07/2016" "18"
    "31/08/2016" "56"
    "31/10/2015" "7"
    "31/12/2015" "3"
    "31/12/2015" "2345"
    "29/02/2016" "23"
    "30/04/2016" "8"
    "30/06/2016" "89"
    "30/11/2015" "5"
    "31/01/2016" "27"
    "31/07/2016" "12"
    "31/08/2016" "29"
    "30/04/2016" "34"
    "30/06/2016" "4"
    end
    keep if date=="31/03/2016"
    save report_march.dta,replace


    I want a loop that take in account the month as date ("31/03/2016" , "30/04/2016", etc) and the name of the month("march", "april",etc)


    Thank you for your help.












  • #2
    Working with dates as strings in Stata is, at best, awkward, and often altogether futile. So the first step is to create a Stata internal format (numeric) date variable. Then we can extract the month from that, obtain the name of that month, and save the report accordingly. The code below will loop over all twelve months of the year and create a report file for each:
    Code:
    // CREATE STATA INTERNAL FORMAT DATE
    gen sif_date = daily(date, "DMY")
    format sif_date %td
    
    // EXTRACT THE MONTH OF THE YEAR
    gen month = month(sif_date)
    
    // LOOP OVER MONTHS AND CREATE REPORTS
    forvalues m = 1/12 {
        preserve
        keep if month == `m'
        local filename: word `m' of `c(Months)'
        local filename = lower("`filename'")
        save report_`filename', replace
        restore
    }
    I don't understand the command -keep if date == "31/03/2016"- in your original post: it save only data for a single day, not the entire month of March.

    If you are going to be a regular user of Stata, you need to be familiar with Stata date time variables. Start with -help datetime- and follow the link to the corresponding section of the online User's Manual. It's a fairly heavy read, but the material is indispensible if you do work with date variables.

    One other thing that strikes me as peculiar: are you quite sure you do not have data from any other years in your data set? If you do, then your report_march.dta file will contain the data for March from all years from which there is March data. That may be what you want, but it seems odd.

    Comment


    • #3
      Thank you very much Clyde. I was avoiding to use date time format for no especific reason, maybe I thought it will be easier to manage the data like that. About the other issue you point out, I think my explanation of the problem was too bare so it caused a little confusion in relationship of the years. Again, thanks a lot for your help.

      Comment

      Working...
      X