Announcement

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

  • generate variable


    Dear Stata users,

    I have the following dataset

    name p1 p2 p3 lowp
    a 10 13 30 10
    b 11 8 21 8
    c 13 12 14 12

    I would like to generate a variable "name2" that is equal to the name of the product with the lowest p (indicated by lowp).
    In my example name2 would be equal to:

    name2
    a
    b
    b

    Thank you for your help.
    Really appreciated.

    Regards,
    F

  • #2

    Code:
    input name p1 p2 p3 lowp
    a 10 13 30 10
    b 11 8 21 8
    c 13 12 14 12
    end
    
    gen lowest = p1
    gen name2 = 1
    forval j = 2/3 {
           replace name2 = `j' if p`j' < lowest
           replace lowest = p`j' if p`j' < lowest
    }
    You should think about what you want if there are ties for lowest.
    Last edited by Nick Cox; 26 Sep 2014, 09:37.

    Comment

    Working...
    X