Announcement

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

  • Extraction of diagonal observations from a rectangular Matrix

    Hello to everybody,

    Here is a small toy sample of my dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str1 region str7 sector float(A_v B_v C_v D_v E_v vector)
    "A" "sector1"   1  100 116 50 1001  1
    "A" "sector2"   2  101 117 51 1002  2
    "A" "sector3"   3 1005 118 52 1003  3
    "B" "sector1" 101    4 119 53 1004  4
    "B" "sector2" 102    5 120 54 1005  5
    "B" "sector3" 103    6 121 55 1006  6
    "C" "sector1" 104  107   7 56 1007  7
    "C" "sector2" 105  108   8 57 1008  8
    "C" "sector3" 106  109   9 58 1009  9
    "D" "sector1" 107  110 123 10 1010 10
    "D" "sector2" 108  111 124 11 1011 11
    "D" "sector3" 109  112 125 12 2000 12
    "E" "sector1" 110  113 126 60   13 13
    "E" "sector2" 111  114 127 61   14 14
    "E" "sector3" 121  115 128 62   15 15
    end
    I am trying to generate a new variable whose obs are the "diagonal" obs when the region `x’ matches with the correlated same-letter variables. (i.e. region A matches with A_v etc.)
    Such that:






    Consider all the obs outside the orange boxes as random values.

    In order to solve it, I tried many paths but essentially the idea was to tell STATA, thought a loop, that: if the local - equal to the levelsof “region” - matches with the first letter in the variables then the new variable “vector" should contain the matching obs.

    But it didn’t really work and I am sure there is a better way to tackle the problem!
    Thank you
    Riccardo

  • #2
    I just realised the image coundn't be displayed.

    Click image for larger version

Name:	WhatsApp Image 2022-03-24 at 19.31.48.jpeg
Views:	1
Size:	136.1 KB
ID:	1656174

    Comment


    • #3
      Code:
      levelsof region
      local regions = r(levels)
      gen diag = .
      foreach region of local regions {
          replace diag =  `region'_v if region == "`region'"
      }
      list, sepby(region)
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment

      Working...
      X