Announcement

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

  • Importing a matrix into stata file: Col numbers

    Dear Statalisters
    I have a matrix with column names equal numbers, when I run svmat matrix, and Stata creates a file with variable names like: X1,..., Xn. How can I keep the numbers from the original matrix as variable names into Stata, something like: var99 var100 var101 instead of X1 X2 X3?
    Thank you and regards
    LR

  • #2
    Check out the help, - svmat [type] A [, names(col|eqcol|matcol|string)]-

    Code:
    . matrix input A = (1,1,1\1,1,1\1,1,1)
    
    . matrix colnames A = 1 2 3
    
    . matlist A
    
                 |         1          2          3 
    -------------+---------------------------------
              r1 |         1                       
              r2 |         1          1            
              r3 |         1          1          1 
    
    
    . svmat A, names(var)
    number of observations will be reset to 3
    Press any key to continue, or Break to abort
    number of observations (_N) was 0, now 3
    
    . list
    
         +--------------------+
         | var1   var2   var3 |
         |--------------------|
      1. |    1      1      1 |
      2. |    1      1      1 |
      3. |    1      1      1 |
         +--------------------+
    
    .

    Comment


    • #3
      Thank you, Joro. The problem is that the numbers that I have are not 1, 2, 3, but other sequences, like, 99, 100, 101, which refer to individual IDs.
      When I use your code, instead of having var99, var100, var 101, Stata returns var1, var2, var3.
      Thank you so much and warm regards,
      LR

      Comment


      • #4
        -svmat- has a -names()- option. -help svmat-. Probably -names(matcol)- comes closest to what you want, and then a group -rename- will take you home.

        Comment


        • #5
          Originally posted by lrkcastro View Post
          The problem is that the numbers that I have are not 1, 2, 3, but other sequences, like, 99, 100, 101, which refer to individual IDs.
          When I use your code, instead of having var99, var100, var 101, Stata returns var1, var2, var3.
          You can rename the matrix's column names to include a string prefix, which will allow you to save arbitrary sequences of numbers in the variable names. See below for the method. (Begin at the "Begin here" comment.)

          .ÿ
          .ÿversionÿ16.1

          .ÿ
          .ÿclearÿ*

          .ÿ
          .ÿmatrixÿinputÿDÿ=ÿ(1,ÿ2,ÿ3)

          .ÿmatrixÿcolnamesÿDÿ=ÿ99ÿ100ÿ101

          .ÿmatrixÿlistÿD

          D[1,3]
          ÿÿÿÿÿ99ÿÿ100ÿÿ101
          r1ÿÿÿÿ1ÿÿÿÿ2ÿÿÿÿ3

          .ÿ
          .ÿ*
          .ÿ*ÿBeginÿhere
          .ÿ*
          .ÿlocalÿcolumn_namesÿ:ÿcolnamesÿD

          .ÿ
          .ÿlocalÿnew_column_names

          .ÿforeachÿcolumn_nameÿofÿlocalÿcolumn_namesÿ{
          ÿÿ2.ÿÿÿÿÿlocalÿnew_column_namesÿ`new_column_names'ÿID`column_name'
          ÿÿ3.ÿ}

          .ÿmatrixÿcolnamesÿDÿ=ÿ`new_column_names'

          .ÿ
          .ÿquietlyÿsvmatÿdoubleÿD,ÿnames(col)

          .ÿ
          .ÿlist,ÿnoobs

          ÿÿ+----------------------+
          ÿÿ|ÿID99ÿÿÿID100ÿÿÿID101ÿ|
          ÿÿ|----------------------|
          ÿÿ|ÿÿÿÿ1ÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿ3ÿ|
          ÿÿ+----------------------+

          .ÿ
          .ÿexit

          endÿofÿdo-file


          .


          In the illustration of the method, I've chosen the prefix ID, but you can choose whatever suits your purpose.

          Comment


          • #6
            What Joseph shows is a very useful technique to know in general. However in this context -svmat- does all of this automatically. Here
            Code:
            . matrix input A = (1,1,1\1,1,1\1,1,1)
            
            . matrix colnames A = 99 100 101
            
            . matlist A
            
                         |        99        100        101 
            -------------+---------------------------------
                      r1 |         1                       
                      r2 |         1          1            
                      r3 |         1          1          1 
            
            . svmat A , names(matcol)
            number of observations will be reset to 3
            Press any key to continue, or Break to abort
            number of observations (_N) was 0, now 3
            
            . list
            
                 +-------------------+
                 | A99   A100   A101 |
                 |-------------------|
              1. |   1      1      1 |
              2. |   1      1      1 |
              3. |   1      1      1 |
                 +-------------------+
            So I do not understand what is the problem?

            Comment


            • #7
              Originally posted by Joro Kolev View Post
              So I do not understand what is the problem?
              You needed to illustrate the last step.

              .ÿtempnameÿD

              .ÿmatrixÿinputÿ`D'ÿ=ÿ(1ÿ2ÿ3)

              .ÿmatrixÿcolnamesÿ`D'ÿ=ÿ99ÿ100ÿ101

              .ÿquietlyÿsvmatÿdoubleÿ`D',ÿnames(matcol)

              .ÿrenpfixÿ`D'ÿIDÿ//ÿÿthis

              .ÿlist,ÿnoobs

              ÿÿ+----------------------+
              ÿÿ|ÿID99ÿÿÿID100ÿÿÿID101ÿ|
              ÿÿ|----------------------|
              ÿÿ|ÿÿÿÿ1ÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿ3ÿ|
              ÿÿ+----------------------+

              .


              I think that it's Rule 6.something in the rename group command, but it's faster for me to do something familiar as in the post above or just use the old renpfix than to pore through the help file to find the new but seldom used syntax.

              Comment


              • #8
                OK, I understand now. Another way to do what Joseph shows with -renpfix- (we are not supposed to use such commands "As of Stata 12.0, renpfix has been superseded by the new syntaxes allowed with rename, which are documented in [D] rename group," whenever we use an out of date command in Stata, we are summoning the Demons of the Old World...) is just to rename the matrix, before we convert it to data:

                Code:
                . matrix input A = (1,1,1\1,1,1\1,1,1)
                
                . matrix colnames A = 99 100 101
                
                . matrix rename A ID
                
                . svmat ID, names(matcol)
                number of observations will be reset to 3
                Press any key to continue, or Break to abort
                number of observations (_N) was 0, now 3
                
                . list, sep(0)
                
                     +----------------------+
                     | ID99   ID100   ID101 |
                     |----------------------|
                  1. |    1       1       1 |
                  2. |    1       1       1 |
                  3. |    1       1       1 |
                     +----------------------+
                Or do with Joseph does, but with the new syntax:

                Code:
                . ren ID* newID*
                
                . list , sep(0)
                
                     +-------------------------------+
                     | newID99   newID100   newID101 |
                     |-------------------------------|
                  1. |       1          1          1 |
                  2. |       1          1          1 |
                  3. |       1          1          1 |
                     +-------------------------------+

                Comment


                • #9
                  Originally posted by Joro Kolev View Post
                  just . . . rename the matrix . . .:

                  Code:
                  matrix rename A ID
                  Nice. Wasn't aware of matrix rename.

                  Comment


                  • #10
                    Thank you, all.

                    Comment

                    Working...
                    X