Announcement

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

  • How to make changes in a stata string variable based on a string mata matrix of different dimensions

    Dear all,

    I am not new to STATA but I am very new to mata. I have a problem of understanding how to use information of a matrix that I have created in mata to change some of the observations in a stata variable.

    I have created a mata matrix now of dimensions 9 x 4 containing string elements that I call E. It looks as following (example):
    X1/Y1 X1 Y1 Y1/X1
    X2/Y2 X2 Y2 Y2/X2
    X3/Y3 X3 Y3 Y3/Y3
    X4/Y4 X4 Y4 Y4/X4
    X5/Y5 X5 Y5 Y5/X5
    X6/Y6 X6 Y6 Y6/X6
    X7/Y7 X7 Y7 Y7/X7
    X8/Y8 X8 Y8 Y8/X8
    X9/Y9 X9 Y9 Y9/X8
    Then a have a larger dataset in stata containing 916 variables and 1416 observations.
    I now want to replace some of the instances in one of these variables called co_id, which is the important identifier, based on the conditions set in matrix E (in mata).
    I would like to: replace co_id with the value of E[,1] (i.e col 1) if the value of co_id is found in any of the columns 2-4 in matrix E.

    What I have managed to do so far is just viewing this co_id in mata by:
    st_sview(Q=.,.,"co_id")

    I anticipate that I should do an if else function, but I can't get my head around it.
    Any help would be appreciated,

    Thanks!
    Last edited by Christine Alamaa; 04 Dec 2017, 22:36. Reason: mata to stata conditions in matrices of different dim, string matrix

  • #2
    Do you really need column 4? Seems the same as column 1 except for the last entry. Assuming you already have the matrix E, you could try something like this:
    Code:
    mata:
        st_sview(Q=., ., "co_id")
        for(i=1; i <= 9; i++) {
            pick = select(1::length(Q), Q :== E[i,2] :| Q :== E[i,3])
            if(length(pick) < 1) continue
            Q[pick] = J(length(pick), 1, E[i,1])
        }
    end
    You loop over the nine possible codes (the rows), pick the indices of the data with codes in columns 2 or 3 (I assume 4 doesn't need changing), and then replace those values. The trick is to repeat the replacement code in column 1 as many times as needed.

    Comment


    • #3
      Try to use selectindex() indstead of select() to get the indices directly

      Comment

      Working...
      X