Announcement

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

  • data manipulation!

    Dear All, I was asked this question (https://bbs.pinggu.org/forum.php?mod...=1#pid57861683). Suppose that we have a dataset like
    Code:
    clear
    input float(id indu)
    1 .9375
    2  .125
    3     1
    4  .375
    5  .625
    end
    The purpose is to construct a new dataset like
    Code:
    clear
    input str4 id float(id1 id2 id3 id4 id5)
    "id1" .9375 .125 .9375 .375 .625
    "id2"  .125 .125  .125 .125 .125
    "id3" .9375 .125     1 .375 .625
    "id4"  .375 .125  .375 .375 .375
    "id5"  .625 .125  .625 .375 .625
    end
    The value for each pair, say, id1 and id2, is the minimum of id=1 (.9375 and id=2 (.125). Any suggestions to go from the data above to the data below? Thanks.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Code:
     forval i=1/5{
     gen id`i'= cond(indu[`i']<=indu[_n], indu[`i'], indu[_n])
     }
    Result:

    Code:
    . l
    
         +-------------------------------------------------+
         | id    indu     id1    id2     id3    id4    id5 |
         |-------------------------------------------------|
      1. |  1   .9375   .9375   .125   .9375   .375   .625 |
      2. |  2    .125    .125   .125    .125   .125   .125 |
      3. |  3       1   .9375   .125       1   .375   .625 |
      4. |  4    .375    .375   .125    .375   .375   .375 |
      5. |  5    .625    .625   .125    .625   .375   .625 |
         +-------------------------------------------------+

    Comment


    • #3
      Dear Andrew, Thanks a lot for this helpful suggestion.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        Consider also as inside the loop in Andrew Musau's helpful post.

        Code:
        gen id`i' = min(indu[`i'], indu)

        Comment

        Working...
        X