Announcement

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

  • Renaming Matrix Rows With Spaces

    Suppose I wish to rename the rows of a matrix.
    Code:
    clear *
    cls
    matrix A = (2,1\3,2)
    
    
    cls
    loc rn
    foreach i in "City God Temple" Songjiang { //
        local rn `rn' `:display "`i''
        
        di "`rn'"
    }
    
    
    matrix rownames A = "`rn'"
    
    mat l A
    In this case, I want row 1 to be named "City God Temple" and row 2 to be "Songjiang". How would I do this? Instead, at present, it concatenates them together.

    EDIT: Okay so I have the answer (dug up from some code in my program), but I don't get why it's correct.
    Code:
    clear *
    cls
    u "http://fmwww.bc.edu/repec/bocode/s/scul_Reunification.dta", clear
    
    matrix A = (2,1\3,2)
    
    
    levelsof country if inlist(code,20,21), l(labs)
    
    mat rownames A = `labs'
    Why does this work? Is it the backticks that levelsof defines?
    Last edited by Jared Greathouse; 30 Dec 2022, 17:57.

  • #2
    Code:
    . loc rn
    
    . foreach i in "City God Temple" Songjiang { //
      2.     local rn `" `rn' "`i'" "'
      3.    
    .     macro list _rn
      4. }
    _rn:             "City God Temple"
    _rn:             "City God Temple" "Songjiang"
    
    .
    .
    . matrix rownames A = `rn'
    
    .
    . mat l A
    
    A[2,2]
                  c1  c2
    City God T~e   2   1
       Songjiang   3   2
    
    .
    When defining a local or global macro, if Stata encounters a double quote as the first character, it will expect a matching double quote as the final character, and will remove both. So sometimes you have to add surrounding compound double quotation marks to be sacrificed to preserve the wanted double quotation marks.

    The following examples demonstrate various applications of this rule.
    Code:
    . local gnxl a b c
    
    . macro list _gnxl
    _gnxl:          a b c
    
    . local gnxl "a b c"
    
    . macro list _gnxl
    _gnxl:          a b c
    
    . local gnxl a "b two" c
    
    . macro list _gnxl
    _gnxl:          a "b two" c
    
    . local gnxl "a one" b "c three"
    
    . macro list _gnxl
    _gnxl:          a one" b "c three
    
    . local gnxl "a one" b c
    invalid syntax
    r(198);
    
    . local gnxl ""a one" b c"
    
    . macro list _gnxl
    _gnxl:          "a one" b c
    Last edited by William Lisowski; 30 Dec 2022, 18:25.

    Comment


    • #3
      I did not understand what is the problem you are dealing with:

      Code:
      . clear *
      
      . matrix A = (2,1\3,2)
      
      . matlist A
      
                   |        c1         c2 
      -------------+----------------------
                r1 |         2          1 
                r2 |         3          2 
      
      . matrix rownames A = "City God Temple" Songjiang
      
      . matlist A
      
                   |        c1         c2 
      -------------+----------------------
      City God T~e |         2          1 
         Songjiang |         3          2 
      
      .
      If you put the row name in quotation marks, it is not concatenated, it is shown as you typed it.

      Comment


      • #4
        Joro Kolev You're right, but, the issue is that I'm not typing in the matrix names- for a program, I needed to programmatically name them via the list I built in the first post. So, while it works when one types it out, the critical thing was for Stata to rename it via code, instead of me typing it out. And thank you!! William Lisowski The issue is that the original context in which I'd applied the code block was with numbers, and therefore I had no need for the additional quotes. Okay, so this makes much more sense now!

        Comment

        Working...
        X