Announcement

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

  • Selectively list date variable as matrix row name using macro

    Greetings, I'm hoping for some aid here. Having searched the forum for a solution, I've yet to find one that works. Here is the code I'm working with
    Code:
    clear
    input str20 DATESTR double SALES GDP byte SELECT
    "2019m1" 1243 209 0
    "2019m2" 889 209 0
    "2019m3" 1220 210 0
    "2019m4" 1594 211 0
    "2019m5" 1458 212 0
    "2019m6" 1178 213 0
    "2019m7" 1452 214 0
    "2019m8" 1453 214 1
    "2019m9" 1281 215 1
    "2019m10" 1444 216 1
    "2019m11" 1185 216 1
    "2019m12" 1194 217 1
    "2020m1" 1255 216 1
    "2020m2" 1029 216 1
    "2020m3" 1290 215 1
    "2020m4" 674 208 1
    "2020m5" 1014 201 1
    "2020m6" 1757 195 1
    "2020m7" 1826 200 1
    "2020m8" 1563 206 1
    "2020m9" 1483 211 1
    "2020m10" 1388 213 1
    "2020m11" 1288 214 0
    "2020m12" 1321 215 0
    "2021m1" 1301 217 0
    "2021m2" 1211 219 0
    "2021m3" 1698 220 0
    "2021m4" 2027 223 0
    "2021m5" 1715 225 0
    "2021m6" 1746 227 0
    "2021m7" 1790 229 0
    "2021m8" 1803 230 0
    "2021m9" 1535 232 0
    "2021m10" 1503 235 0
    "2021m11" 1549 237 0
    "2021m12" 1401 240 0
    "2022m1" 1350 241 0
    "2022m2" 1238 243 0
    "2022m3" 1809 244 0
    "2022m4" 1929 246 0
    "2022m5" 1612 247 0
    "2022m6" 1690 249 0
    end
    gen date=monthly(DATESTR,"YM")
    tsset date, m
    mkmat SALES GDP if SELECT==1, matrix(goodmatrix) rown(date)
    The final command here creates a matrix with the specified columns, however the row names return in their numerical form. Is there a way I can specify the row names be listed as written?

    NOTE: I must use the time variable 'date' variable to accomplish this, I cannot use 'DATESTR'.

  • #2
    Code:
    mkmat SALES GDP if SELECT==1, matrix(goodmatrix) rown(DATESTR)
    will produce the matrix you say you want. "I must use the time variable 'date' variable to accomplish this, I cannot use 'DATESTR'." Why??

    Last edited by Clyde Schechter; 26 Sep 2022, 18:37.

    Comment


    • #3
      Hi Clyde! So, this snippet is part of a more extensive eclass program I'm writing, where the available input is gathered from 'capture tsset'; within the program, 'DATESTR' won't be available. I've tried some iterations of 'tostring' prior to 'mkmat', but the variable still isn't presented as a string in the matrix.

      Comment


      • #4
        Eric, could you explain what issue you're having with doing something like the following?

        Code:
        tempvar datestr
        tostring date, gen(`datestr') format(%tm)
        mkmat SALES GDP if SELECT==1, matrix(goodmatrix) rown(`datestr')
        drop `datestr'
        Last edited by Hemanshu Kumar; 27 Sep 2022, 04:55.

        Comment


        • #5
          Greetings Hemanshu, and in response to your input, the 'tostring' command produces (and also does not work with 'rep'):
          Code:
          . tostring date, gen(`datestr') format(%tm)
          date cannot be converted reversibly; no generate.
          There isn't a 'tostring' alternative I've come across, and having program users submit a string version of their time series variable just for labeling purposes isn't particularly tenable. I currently collect the time variable from `timevar', through the following process, any ideas on how to transfer this info to my needs in the first post?
          Code:
          capture tsset
          if _rc!=0 {
              di as err "This is the incorrect response, may God have mercy on your soul."
              exit 250
          }
          else {
              loc panvar "`r(panelvar)'"
              loc timevar "`r(timevar)'"
              loc tform "`r(unit1)'"
              loc tunit "`r(unit)'"
              if "`tform'"=="" {
                  di as err "Time delta not supported. Please review time variable input/format."
                  exit 451
          }
          Ignore the error 250 jibe, I'll surely replace that line if the program ends up publishable :D

          Comment


          • #6
            Hi group, and much appreciation for your posts here. I eventually came to the somewhat simple answer to the problem posed above, by reverse generating a string variable using the proper time format. The row names now appear as strings when the matrix is displayed or printed. The simplified code is:
            Code:
            capture tsset
            loc timevar "`r(timevar)'"
            qui gen x=string(`timevar',"%tm")
            mkmat SALES GDP if testsamp==1, matrix(A) rown(x)

            Comment


            • #7
              Eric Makela sorry I lost track of this thread. I'm glad you figured out a solution using the string() function. That said, the tostring command I had suggested would have also worked fine, just with the inclusion of the force option.

              Comment

              Working...
              X