Announcement

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

  • Create tje function edate in Stata (similar to the one in excel)


    Hello, I would like to create in stata, for example via .ado, a function similar to the one in excel (edate(2021/12/1,1) or edate(2021/12/1,-3)). However, I would like the function to be able to be called in stata as edate(x,y) where "x" is a date and "y" is the number of months in the future. Thanks for the help.
    Last edited by Eduardo Diaz; 16 Mar 2022, 21:04. Reason: excel

  • #2
    If you mean that you would like to have a function that you can use with -gen-, that is not possible. Only StataCorp can create new Stata functions.

    But one can create a program that will do this. The following program is called with two variables: the first is the start date, and the second is the number of months you wish to add (if positive) or subtract (if negative). The option -gen()- must contain the name of a new variable to receive the result.

    Code:
    capture program drop edate
    program define edate
        syntax varlist(min = 2 max=2 numeric), gen(name)
        local start_date: word 1 of `varlist'
        local offset: word 2 of `varlist'
        confirm numeric variable `start_date'
        confirm int variable `offset'
        capture confirm var `gen'
        if c(rc) == 0 {
            display as error "variable `gen' already exists"
            exit 9
        }
        gen int `gen' = dofm(mofd(`start_date') + `offset')
        tempvar day month_length
        gen byte `day' = day(`start_date')
        gen int `month_length' = daysinmonth(`gen')
        replace `gen' = `gen' + min(`day'-1, `month_length'-1)
        exit
    end
    
    //  MAKE A TOY DATA SET TO DEMONSTRATE USE
    clear
    set obs 100
    set seed 1234
    gen start_date = runiformint(20000, 23000)
    format start_date %td
    gen int offset = runiformint(-24, 24)
    
    // DEMONSTRATE USING THE FUNCTION
    edate start_date offset, gen(result)
    format result %td
    Note: I have followed the Excel edate convention that, for example, 1 month after March 31 is April 30.
    Last edited by Clyde Schechter; 16 Mar 2022, 22:01.

    Comment


    • #3
      Thank you Clyde. Also, I was wondering if its posible that your function can be used like: gen scalar=edate(2021/12/01 , 2), where the date could be defined previously.

      Comment


      • #4
        No, it can't be used that way, nor is it possible for a user to write a function for use with -gen-. Only StataCorp can do that because those functions are part of the compiled executable, they are not ado-files.

        Here's a variation of the program in #2 which can be called with a single (string) start date in YMD format, and an integer offset, and returns the result as a string in the same format. If you prefer to get the result as a Stata internal format numeric date, change the line of code before the -exit- command to -return scalar edate = `result'-.

        Code:
        capture program drop scalar_edate
        program define scalar_edate, rclass
            args start_date offset
            local start_date = daily(`"`start_date'"', "YMD")
            confirm integer number `start_date'
            confirm integer number  `offset'
            local result = dofm(mofd(`start_date') + `offset')
            local day = day(`start_date')
            local month_length = daysinmonth(`result')
            local result = `result' + min(`day'-1, `month_length'-1)
            return local edate: display %tdCCYY/NN/DD `result'
            exit
        end
        
        scalar_edate "2021/12/01" 2
        display `"`r(edate)'"'
        
        scalar_edate "2021/08/08" -5
        display `"`r(edate)'"'
        
        scalar_edate "2021/10/31" 1
        display `"`r(edate)'"'
        Last edited by Clyde Schechter; 17 Mar 2022, 10:23.

        Comment


        • #5
          Thanks again for your help, Clyde.

          Comment

          Working...
          X