Announcement

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

  • Adding variable names to name column of matrix

    Dear Statalist-users,

    I would like to save the names of all companies c_* to use them as rownames of matrix mat1. Which works now that I created locals for every year `l'. I don´t know why dropping `rownnames' creats a comformatability error, looping through the years l.

    For the matrix beta I would like to have the rownames of the companies and then the year. The problem is that the year I am currently in is just added to the very last company each year. I tried to use strings or generate a matrix or varialbe to store the rownames, but nothing works.

    Code:
    forvalues l = 1995(1)2015 {
    ...
    
    foreach companies of varlist c_* {
    
    ...
    
    
    local rownames_`l' `rownnames_`l'' `companies'
    
    matrix rownames mat1 = `rownames_`l''
    
    matrix rownames beta_`l' = "`rownames_`l''_`l'"
    Thank you very much

  • #2
    Fabian,

    It's really, really hard to tell what's working and what's now working or why from your explanation and code. Knowing a bit more about the data structure might help too. Do you want the matrix to have row names like...

    Stata1995
    Stata1996
    .
    .
    .
    Stata2015
    SAS1995
    SAS1996
    .
    .
    .
    SAS2015

    I can imagine a data structure where it wouldn't be too hard to get that but I have no idea if that's the data structure you're working with. It's hard to help you move forward without a better sense for your data and what exactly is going wrong and where.

    Lance

    Comment


    • #3
      Hi Lance,

      I have variables named c_1, c_2, ... for which I run regressions. For the years "l" from 1995 until 2015 I get beta values.

      Now I want to store those values, which works fine. But I have troubles naming the rows of my matrix correctly.

      What I want is:

      c_1_1995
      c_2_1995
      ...
      c_1_1996
      ...

      What the code does:

      c_1
      ...
      c_last_1995
      c_2
      ...
      c_last_1996

      Code:
       matrix rownames beta_`l' = "`rownames_`l''_`l'"

      If I just use

      Code:
       matrix rownames beta_`l' = `rownames_`l''_`l'
      and get rid of the "" - I get.

      c_1 c_2 c_..._1995
      c_1 c_2 c_..._1995
      ...
      c_1 c_2 c_..._1996
      ...






      The first part of the question was: Why

      Code:
       local rownames `rownnames' `companies'  local drop rownames
      won´t work. Stata gives back a conformatability error each time. I thought the local rownames stores the variable names of companies (c_1 c_2 ... ) in rownames and after dropping rownames (because the amount of companies in the year-sample changes) can be used again.

      By adding _`l' it worked, but I don´t understand why different locals were needed to do the job.

      Thank you
      Last edited by Fabian Fuchs; 09 Nov 2016, 02:32.

      Comment


      • #4
        I tried to get a solution with another local for the years

        Code:
          
        local rowname_`l' `rowname_`l'' `companies'
        local year `year'  `l'
        
        matrix rownames beta_`l' = "`rowname_`l''_`year'"
        what I get now is

        c_1 c_2 .... 1995 1995 1995 ....
        c_1 c_2 .... 1995 1995 1995 ....
        c_1 c_2 .... 1995 1995 1995 ....
        ...
        c_1 c_2 .... 1996 1996 1996 ....
        c_1 c_2 .... 1996 1996 1996 ....

        The problem seems to be that the local rowname looks like this

        c_1 c_2 ...

        and year

        1995 1995 ...

        putting only one of the two in as rownames of the matrix beta_1995, Stata recognizes to use it like

        c_1
        c_2

        or

        1995
        1995

        but combining those two, looks like the this c_1 c_2 .... 1995 1995 1995 ....

        Thanks
        Last edited by Fabian Fuchs; 09 Nov 2016, 03:17.

        Comment


        • #5
          How about sharing a snippet of data (see dataex on SSC)? I'm afraid I won't have any other ideas without seeing exactly what you're working with.

          Comment

          Working...
          X