Announcement

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

  • replace values if many variables have the same number

    Hi,

    I want to do the following

    gen diabetes=.

    replace diabetes=1 if x==1 | y==1



    But I have 60 variables like x and y and names do not have any sequence. I don't want the argument to be long.

    Solving suggestion with macro and loop is appreciated.


    Thanks


  • #2
    Hi,

    no loops required (and, although my solution includes a local macro, it is not required either): Your condition can also be formulated as "replace the variable if both minimum and maximum of a list of variables equal 1".
    Like this:
    Code:
    clear
    input id x1 x2 x3 y1 y2 y3
    1 1 1 1 1 1 1
    2 1 2 1 1 1 1
    3 1 1 1 1 1 1
    4 2 2 1 1 2 2
    end
    
    local if_varlist x1,x2,x3,y1,y2,y3
    
    generate diabetes=.
    
    replace diabetes=1 if (min(`if_varlist')==1 & max(`if_varlist')==1)
    
    list
    Please note:
    1. The list of variables that goes into the min() and max() functions has to be comma-separated.
    2. I used a data example with made-up data; for future questions, I recommend that you read through the FAQ (also linked at the top of each post), especially on how to ask good-to-answer questions using sample data.
    Regards
    Bela

    Comment


    • #3
      You can cut your two lines to one

      Code:
      gen diabetes = cond(x == 1 |  y == 1, 1, .)
      although almost certainly you will be better off with a (0, 1) indicator

      Code:
      gen diabetes = x == 1 |  y == 1
      That leads directly to the suggestion that egen, anymatch() might match your problem.

      Comment


      • #4
        Sorry if I caused some confusion, but I misread the original question. Instead of any, I somehow thought Moin Ahmed was trying to generate the variable as 1 if all input variables equal 1. My solution accounts for the latter only.

        I second Nick Cox's advice: egen ..., anymatch() should be the easiest way to solve this:
        Code:
        clear
        input id x1 x2 x3 y1 y2 y3
        1 1 1 1 1 1 1
        2 1 2 1 1 1 1
        3 1 1 1 1 1 1
        4 2 2 1 1 2 2
        4 2 2 2 2 2 2
        end
        
        local egen_varlist x1 x2 x3 y1 y2 y3
        
        egen diabetes=anymatch(`egen_varlist') , values(1)
        
        list
        Regards
        Bela

        Comment


        • #5
          Daniel: I am not sure you misread the question at all. I think it's not especially clear what is wanted.

          Comment


          • #6
            Originally posted by Daniel Bela View Post
            Sorry if I caused some confusion, but I misread the original question. Instead of any, I somehow thought Moin Ahmed was trying to generate the variable as 1 if all input variables equal 1. My solution accounts for the latter only.

            I second Nick Cox's advice: egen ..., anymatch() should be the easiest way to solve this:
            Code:
            clear
            input id x1 x2 x3 y1 y2 y3
            1 1 1 1 1 1 1
            2 1 2 1 1 1 1
            3 1 1 1 1 1 1
            4 2 2 1 1 2 2
            4 2 2 2 2 2 2
            end
            
            local egen_varlist x1 x2 x3 y1 y2 y3
            
            egen diabetes=anymatch(`egen_varlist') , values(1)
            
            list
            Regards
            Bela
            This is exactly what I wanted. Thanks a lot.

            Comment


            • #7
              Thank you all!

              Comment

              Working...
              X