Announcement

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

  • Local macro for prior month/day numeric date for use in file path returns syntax error

    Hello all,

    I am trying to use a local macro using c(current_date) that returns the last numeric day of the prior month for use in file directories/names (e.g.
    Code:
    C:/path/`year'_`lastmonthno'_`lastdayno'_filename
    returns
    Code:
    C:/path/2018_10_31_filename
    ).

    For last numeric day of the prior month I have:

    Code:
    . di %td_D dofm(mofd(td(`c(current_date)')))-1
      31
    This is the value I was expecting. But when I try to make this into a local macro I get an error:

    Code:
    . local lastday= %td_D dofm(mofd(td(`c(current_date)')))-1
    %td_D invalid name
    r(198);
    The same error is happening for a macro for the numeric value of the prior month:


    Code:
    . di %td_N dofm(mofd(td(`c(current_date)')))-1
      10
    but

    Code:
    . local lastmonthno = %td_N dofm(mofd(td(`c(current_date)')))-1
    %td_N invalid name
    r(198);
    Obviously a syntax error on my part, but I cannot determine how to fix it from help datetime. Or if there is a more elegant/efficient way, including ssc or other user programs, I'd appreciate the guidance.

    Thanks in advance,
    Jim

  • #2
    You don't need the format identifiers (%td_...) when storing the dates in the macro. These only affect how things are displayed, not how they are stored internally. Then it should work.

    Comment


    • #3
      Code:
       
        local lastday : display %td_D dofm(mofd(td(`c(current_date)')))-1
      should work. No reason for this to be explained at help datetime as it's really a matter of ways of defining local macros. See help macro and help extended_fcn. I have to sympathise: the documentation for this seems almost to be written as if you know the syntax already and just need to be reminded of what it is.

      Comment


      • #4
        Originally posted by Nick Cox View Post
        Code:
        local lastday : display %td_D dofm(mofd(td(`c(current_date)')))-1
        should work. No reason for this to be explained at help datetime as it's really a matter of ways of defining local macros. See help macro and help extended_fcn. I have to sympathise: the documentation for this seems almost to be written as if you know the syntax already and just need to be reminded of what it is.
        Thank you Mr. Cox, never would have figured that one out on my own. I reacquainted myself with help macro and think I get it now.

        Comment

        Working...
        X