Announcement

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

  • Creating variable with values extracted from a matrix

    Hi,

    I am currently working with a database where I have about 200,000 obs.

    Each observation belongs to a group identified by the variable "x_id". For each observation, I also know its status (belongs to the control or treatment group), and the date on which it took place.

    Using the digdis package, I am currently using the following code to extract the MAD for obs. linked to the control group and at a specific date:

    Code:
    digdis x if statut ==0 & date==0, position(1) by(x_id)
    matrix list r(mad)
    I get the following matrix:

    Click image for larger version

Name:	2023-03-23 16_56_07-2 - Stata_MP 15.1 - C__Users_mjouvin_Dropbox_Traceability in cocoa_transactions_.png
Views:	1
Size:	21.4 KB
ID:	1706829


    My variable "x_id" takes values from 0 to 208. I would like to create a variable that would take the value displayed in the matrix for (i.e.) 12, which is 3.3590708, when the variable x_id==12, and so on...

    Any idea that might help ?

  • #2
    maybe this is a start. if you want them all, I'd dump the status condition.

    not sure if xsvmat allows for format definitions so you don't have to destring.

    ssc install xsvmat

    Code:
    clear
    cd S:\STATA
    use auto, clear
    
    g x_id = trunk
    digdis mpg  , position(1) by(x_id)
    matrix list r(mad)
    matrix MAD = r(mad)'
    matrix list MAD
    xsvmat MAD , rowname(x_id) colname(MAD) saving(MAD.dta, replace)
    use MAD, clear
    destring x_id, force replace
    save MAD, replace
    use auto, clear
    g x_id = trunk
    joinby x_id using MAD

    Comment


    • #3
      Thanks a lot, it did the job !

      Comment


      • #4
        I'm sure there's a simpler way, but I leave it to you to find it. As you can, see the trick is that your join is a colname.

        Comment


        • #5
          Also note that xsvmat allows you to save the file in a frame, so you can avoid the opening and closing.

          Comment

          Working...
          X