Announcement

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

  • Replacing variable entries based on entries of other variables.

    Hello,

    I have 3 variables here:

    c1 c2 c3
    40 40 .
    5 20 5

    I want to replace these entries as the share of the total of the three variables, however if for example in the first row above the third variable is missing, I want the share to be taken of the total of the first two variables only.

    For example the two '40' entries above would become 50 and 50 (in % form), and the three entries in the row below would become 16.6, 66, 16.6.

    And if for example two of the three variables were missing, the entry would automatically become 100.

    How can I implement something like this on a large scale?

    Thanks,
    Jad

  • #2
    Code:
    clear
    input c1 c2 c3
    40 40 .
    5 20 5
    . 666 .
    end
    
    egen total = rowtotal(c?)
    
    forval j = 1/3 { 
       gen p`j' = 100 * c`j'/total 
    }
    
    l 
    
         +--------------------------------------------------------+
         | c1    c2   c3   total         p1         p2         p3 |
         |--------------------------------------------------------|
      1. | 40    40    .      80         50         50          . |
      2. |  5    20    5      30   16.66667   66.66666   16.66667 |
      3. |  .   666    .     666          .        100          . |
         +--------------------------------------------------------+
    You mean 16.7!

    Comment


    • #3
      Thanks!

      Comment

      Working...
      X