Announcement

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

  • Replace tempvarnames by matanames

    Dear all,

    I am writing a small program where I create temporary variables and use them in regression commands. However, when I access the information stored e.g. in r(table), the variable is called as the temporary name, i.e. _00000X.

    Consider the following example:
    tempvar var1 var2
    gen `var1' = ....
    gen `var2' = ....
    reg `var' `var2'
    mat A = r(table)
    mat list A
    How can I automatically change the columnnames of the matrix A so that the first column is called var2 instead of something like _0000X?

    Best,

    Korbinian


  • #2
    You can change the column names of your new matrix using mat colnames

    Comment


    • #3
      Yes, but I want to do it automatically by code:

      Is to find the column of the matrix that is named as
      Code:
      "`var1'"
      and change it's name to
      Code:
      "var1"
      .

      Comment


      • #4
        I don't see that the question can be answered at all easily unless you show more code, specifically your program as at present together with a reproducible example.

        Comment


        • #5
          It is not clear to me what you mean by "automatically", but here is one way

          Code:
          local names var1 var2
          tempvar `names'
          
          generate `var1' ...
          
          regress `var1' `var2'
          
          matrix A = r(table)
          matric colnames A = `names'
          
          matrix list A
          There are several other ways depending on what you want at the end.

          Note that it seems incomplete to have temporary variable names, but a permanent name for the created matrix. In a program this is usually considered bad style (or worse).

          Best
          Daniel

          Comment


          • #6
            Your solution may work for my problem, but it would mean a lot of code in my case. However, my question is much more simple (and a positive answer would be a solution that is much more simple, too).

            Consider the very brief example:
            tempvar var1 var2 var3 var4
            local unknown `var1' `var2' `var3' `var4'
            display "`unknown'"
            Is there any possibility to return the elements of local unknown in that way that Stata gives me
            var1 var2 var3 var4
            instead of something like
            __00000Z __000010 __000011 __000012 ?

            (But explicitly without manually defining a local names via
            local names var1 var2 var3 var4
            , because it's possible elements are unknown at this point.)

            Or to put it differently:
            Stata knows that __00000Z belongs to var1, but does it also know that var1 belongs to __00000Z?
            Last edited by Korbinian Hammer; 08 Jul 2016, 07:21.

            Comment


            • #7
              Your solution may work for my problem, but it would mean a lot of code in my case.
              Why do you think so? What exactly is your case? How is the example (which is supposed to represent the underlying problem) different from your actual problem?

              But explicitly without manually defining a local names [...] because it's possible elements are unknown at this point.
              What do you mean by this? Again, you need to post code much (much) closer to your real problem. Temporary names are just like locals, meaning local. Given only a (permanent) name like __00000Z there is no way to know which temporary name was used, if any, because virtually any valid name could have been used, including literal __00000Z.*

              Stata knows that __00000Z belongs to var1, but does it also know that var1 belongs to __00000Z?
              I do not get this either. Stata knows that `var1' evaluates to __00000Z but it cannot possibly relate var1 to __00000Z or the other way round.

              Best
              Daniel

              EDIT

              * Actually there seems not even to be a way to determine whether
              __00000Z is a variable, a scalar, a matrix or something else (cf. http://www.statalist.org/forums/forum/general-stata-discussion/general/84940-temporary-names-for-a-scalar-dangerous-advice"Dirk Enzmann's concern).
              Last edited by daniel klein; 08 Jul 2016, 07:44.

              Comment


              • #8
                Stata knows that `var1' evaluates to __00000Z but it cannot possibly relate var1 to __00000Z or the other way round.
                That is the answer to my question that has remained (,but I am still wondering that it is not possible to "relate var1 to __00000Z or the other way round").Thank you!

                Comment

                Working...
                X