Announcement

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

  • Removing multiple substrings from macros

    I wanna remove multiple subtrings from a macro.
    Code:
    qui ssc inst rcm, replace
    
    
    import delim "https://raw.githubusercontent.com/jehangiramjad/tslib/master/tests/testdata/prop99.csv", clear
    
    loc int_time = 1989
    
    drop if year >= 2001
    
    
    keep if submeasureiddisplayorder==2
    
    rename (locationdesc data_value) (state cigsale)
    
    keep state year cig
    
    egen id = group(state)
    
    order id, b(year)
    
    drop if inlist(id,3,22,38,10,12,2,12,23,31,33,48)
    
    xtset id year, y
    cls
    
    rcm cig, ///
        trunit(5) ///
        trperiod(`int_time') ///
        method(forward) ///
        cr(mbic) frame(cig_wide)
    
    
    local sel = subinstr("`e(predvar_sel)'","cigsale", """,",.)
    
    di "`sel'"
    The final macro looks like
    Code:
    ·43 ·28 ·47 ·1 ·24 ·44 ·4 ·40 ·41 ·6 ·27 ·45 ·25 ·29 ·14 ·30
    and I desire
    Code:
    ,43,28,47,1,24,44,4
    and so on. How might I do this?

    Methodologically, I like the forward selection method, but in my personal opinion it WILDLY overfits the time-series in the pre-intervention period thus resulting in a noisy counterfactual. The author tells me that the method doesn't play well with non-stationary time series data, so I wanna see if I can fix it.


    Nevermind, I got it
    Code:
    local sel = subinstr("`e(predvar_sel)'","cigsale·",","",",.)
    Last edited by Jared Greathouse; 01 Jul 2022, 10:13.

  • #2
    Actually, no I don't.. Consider this simple example
    Code:
    cls
    
    loc string gdpcap·4 gdpcap·445434
    
    
    local sel = subinstr("`string'","gdpcap·" , ",",.)
    
    
    di `sel'
    I want 4,445434

    but instead, there are spaces between them.

    Comment


    • #3
      Code:
      loc string gdpcap·4 gdpcap·445434·43 ·28 ·47 ·1 ·24 ·44 ·4 ·40 ·41 ·6 ·27 ·45 ·25 ·29 ·14 ·30
      local string= subinstr(ustrregexra("`string'", "[^\d]+", ","), ",","", 1)
      Res.:

      Code:
      . di "`string'"
      4,445434,43,28,47,1,24,44,4,40,41,6,27,45,25,29,14,30

      Comment


      • #4
        Yep this works. Thank you so much!

        Comment

        Working...
        X