Announcement

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

  • Question about stsplit

    Hi all,

    I would like to write a command to stsplit a dataset thus:

    Code:
    stsplit newvar, at(datelist)
    instead of

    Code:
    stsplit newvar, at(numlist)
    The reason is that I have a large number of dates to stsplit on, and it is an extra step to convert dates into a number before typing them into code. The following does not work, unfortunately

    Code:
    stsplit newvar, at(d(31Jan2017))
    Is there a workaround that I can use? Alternatively, is there a quick way I can calculate the number represented by a date to help my efficiency?

    Thanks

    MM

  • #2
    I think this will work.
    Code:
    stsplit newvar, at(`=d(31Jan2017)')
    because that syntax evaluates the expression to the right of the equal sign and then substitutes the numeric result.

    Or something like this, if your dates follow a pattern
    Code:
    . local dates
    
    . forvalue y = 2017/2019 {
      2.     local dates `dates' `=d(31Jan`y')'
      3. }
    
    . macro list _dates
    _dates:         20850 21215 21580
    
    . foreach d in `dates' {
      2.     display %td `d'
      3. }
    31jan2017
    31jan2018
    31jan2019
    
    .
    Or something like this, for an arbitrary list of dates
    Code:
    . local dates 13jan2017 23may2018 9sep2019
    
    . local list
    
    . foreach day of local dates {
      2.     local list `list' `=d(`day')'
      3. }
    
    . macro list _list
    _list:          20832 21327 21801
    
    . // stsplit newvar, at(`list')
    Last edited by William Lisowski; 30 Nov 2021, 16:03.

    Comment


    • #3
      Thanks so much William, these are all good suggestions. The last is elegant!

      Comment

      Working...
      X