Announcement

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

  • Row and column names of a matrix

    I was wondering how to extract row and column names from a matrix. It appeared that it is somethink like this in case I want the second label:
    Code:
    di "`: word 2 of `: rownames D''"
    Just for my understanding, could someone explain what exactly happens here?

    An additional example is this
    Code:
    tokenize "`: rownames D'"
    di "`1' `2' `5'"
    Is this the best way?

    Thanks.

  • #2
    Both of those codes work. I don't know that there is much reason to choose one over the other. I suppose that if you use -tokenize-, then you cannot use it again for some other similar task until you are done with the `1', `2', etc. that it creates. But in practice that is probably seldom a problem.

    I'm going to start by assuming that you know what local macros are and what the notation `whatever' means. If you don't, read the macro chapter (18.3) in the User's Guide [U] volume of the PDF manuals that come with your Stata installation.

    With that off the table, `: rownames D' is a macro extended function. These are mostly used in writing programs. They enable the programmer to capture lots of "system" information. There are many of them. For a comprehensive list run -help macro- and click on the :extended_fcn link near the top. Anyway, the macro extended function :rownames (all macro extended functions begin with a colon [:]) takes a matrix name as its argument and it returns a local macro containing the names of the rows of that matrix. If you wanted to refer to that result again later, you could save it in a local macro:

    Code:
    local my_matrix_rownames : rownames D
    ... // OTHER CODE HERE
    
    .....`my_matrix_rownames'...
    The above code would save the row names of matrix D in local macro my_matrix_rownames, and then, in the last row, those row names would be substituted for `my_matrix_rownames' in that place. But if you only intend to refer to the result once, it is sometimes more convenient not to bother storing the results but just, in the immediate place you want the results, to specify `: rownames D'.

    -tokenize- is a programmer's command. It simply takes a string and breaks it into tokens (strings of characters delimited by whitespace), placing the successive tokens into local macros called 1 2 3, etc. In the earliest versions of Stata, before the -syntax- command was created, -tokenize- was a much used command, being the way to parse command lines and get the names of variables in varlists or argumentlists, etc. Today it is less often used.

    Comment


    • #3
      A minor twist on Clyde's excellent explanation is that parse was really the forerunner of syntax (in Stata 5 and earlier).

      tokenize I think is not so ancient and it never has been a general syntax parsing command.

      Comment


      • #4
        Nick is right, of course. I mis-remembered. The common use of -tokenize- in the past was before -foreach- was introduced. In the pre-foreach era, you would get, say, a list of variable names in a local macro. Then you would -tokenize- that macro and loop over `1', `2', etc. So instead of modern:

        Code:
        ds variables_and_wild_cards
        local vlist `r(varlist)'
        foreach v of local vlist { // OR foreach v of varlist `varlist' {
           whatever `v' ...
        }
        we had:
        Code:
        unab vlist: variables_and_wild_cards
        tokenize varlist
        while "`1'" != "" {
            whatever `v'...
            macro shift
        }
        -macro shift-, also now little used, would take the series of local macros 1 2 3, etc. and drop the contents of local macro 1, and then shift the contents of the other macros one number down.

        Note that all of these older constructs are still available in current Stata, though more modern constructs have largely displaced them in practice.

        Comment


        • #5
          Thank you Clyde (and Nick), very clear explanations. As for whatever, this is fill in any statement, right?

          Comment


          • #6
            Yes, by whatever, I mean this could be any Stata command involving `v'.

            Comment

            Working...
            X