Announcement

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

  • How to retrieve a specific value of a variable and calculate it with other values of variables?

    Hi Statalists,

    I'm currently want to let stata calculate P1 for each id and here is a formula: P1 = A11 * D1 + ... + A18 * D4iii
    (A11 is the first value of A1 which equals 0.345865; A12 is the second value of A1 which equals 0.326931 ; A13 is the third one and so on)

    For example, if I want to get the P1 for first person, here is the calculation:
    0.345865 (A11) * 1.566901 (D1 for ID == 1) + 0.326931 (A12) * 0.233873 (D2i for ID == 1) ..... + 0.321616 (A18) * 1.384699 (D4iii)

    I find it laborious and I wonder if there is any more convenient way to calculate P1 for other IDs automatically?

    If I also want to calculate P2 for first person after calculating P1, P2 = A21 * D1 + .... + A28 * D4iii, do I need to use some syntax like for loop?
    id D1 D2i D2ii D3i D3ii D4i D4ii D4iii A1 A2
    1 1.566901 0.233873 0.286958 0.556992 1.113236 1.567941 1.490475 1.384699 0.345865 -0.07561
    2 -0.39339 -0.64672 0.07849 -0.88745 -0.2885 -0.57468 -0.70157 -0.59344 0.326931 0.470004
    3 0.390256 1.464312 1.263042 0.840091 0.97695 -0.14107 -0.23965 -0.59344 0.320997 0.476968
    4 -0.69505 -1.16996 -1.51789 -1.27133 -1.12446 -1.06964 -1.0088 -0.92313 0.367527 0.146237
    5 -0.86872 0.118493 -0.1106 0.761692 -0.67723 0.217444 0.459544 0.725319 0.376543 0.223233
    6 0.387949 -0.32069
    7 0.373703 -0.36884
    8 0.321616 -0.48557

  • #2
    I think this code does what you want.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte d float(d1 d2i d2ii d3i d3ii d4i d4ii d4iii a1 a2)
    1 1.566901  .233873  .286958  .556992 1.113236 1.567941 1.490475 1.384699 .345865 -.07561
    2  -.39339  -.64672   .07849  -.88745   -.2885  -.57468  -.70157  -.59344 .326931 .470004
    3  .390256 1.464312 1.263042  .840091   .97695  -.14107  -.23965  -.59344 .320997 .476968
    4  -.69505 -1.16996 -1.51789 -1.27133 -1.12446 -1.06964  -1.0088  -.92313 .367527 .146237
    5  -.86872  .118493   -.1106  .761692  -.67723  .217444  .459544  .725319 .376543 .223233
    6        .        .        .        .        .        .        .        . .387949 -.32069
    7        .        .        .        .        .        .        .        . .373703 -.36884
    8        .        .        .        .        .        .        .        . .321616 -.48557
    end
    
    local vars d1 d2i d2ii d3i d3ii d4i d4ii d4iii
    generate p1 = 0
    generate p2 = 0
    forvalues v=1/8 {
        local var : word `v' of `vars'
        quietly replace p1 = p1 + a1[`v']*`var'
        quietly replace p2 = p2 + a2[`v']*`var'
    }
    list d p1 p2, clean
    Code:
    . list d p1 p2, clean
    
           d          p1          p2  
      1.   1    2.945018   -1.266657  
      2.   2   -1.433077    .3002576  
      3.   3    1.360612    2.023885  
      4.   4   -3.089634   -.4948973  
      5.   5    .2170774   -.5625926  
      6.   6           .           .  
      7.   7           .           .  
      8.   8           .           .
    Please note the use of the dataex command to provide example data in an immediately usable format.

    Comment


    • #3
      Thanks William! It works perfectly.
      I'm not familiar with this command. Would you mind telling me more details about what's the function of "word" and what does a1[`v']*`var' mean? Why a1 is attached to [`v']?

      Comment


      • #4
        See the output of
        Code:
        help local
        in the section "Macro functions for parsing" for an explanation of the word function, and see the output of
        Code:
        help subscript
        for an explanation of the use of subscripts to select the value of a variable from a particular observation.

        Comment

        Working...
        X