Announcement

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

  • keep command

    hellow all
    i am facing a problem with "keep" command. i got a data which contains 5 to 6 variables,
    now i want to keep those data which fill up the two conditions at a time.( like varible1==1 and variable2==1)
    please some one help me with necessary command. thamks to all
    Regards
    Raeed

  • #2
    Raeedur:
    the following example might be helpful:

    Code:
    . set obs 10
    
    . g variable1=1 in 1/5
    
    . replace variable1=2 if variable1==.
    
    . g variable2=1 in 2/7
    
    . replace variable2=2 if variable2==.
    
    . g Flag=1 if variable1==1 & variable2==1
    
    . replace Flag=0 if Flag==.
    
    . keep if Flag==1
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3

      male female age occupation
      1 0
      1 0
      1 0
      1 0
      0 1
      0 1
      0 1
      0 1


      dear Sir
      if i wana keep those which male==1 no mater what is in female
      and also those which female=1 no mate what is in female
      i have tried this command (keep if male=="1" female==1) but it says invalide "femail" .
      thanks

      Comment


      • #4
        You have to add | or &
        Code:
        keep if male == 1 & female == 1
        for Keep if the Observation is male and female
        or
        Code:
        keep if male == 1 | female == 1
        for Keep if the Observation is female == 1 OR male == 1

        Comment


        • #5
          thanks dear JAY

          Comment


          • #6
            if i have duplicate data and i need to drop one of them .. criteria of drop is (male=1 and female= blank) what should i do?

            Comment


            • #7
              Assuming that the variable female is a numeric 'blank' - more commonly known as 'missing' - is coded in Stata as '.'

              so
              Code:
              drop if male==1 & female==.
              In Stata, missing values are positive infinity, so you could also tell Stata to only keep cases with a valid value for female by:
              Code:
              keep if female<.
              ​
              Maybe you have already done this, but if not I would advise ensuring all cases that meet the condition 'male==1 & female==.' are indeed duplicates
              I would first run a code to identify duplicates and then use a drop command to drop the cases.
              You can identify duplicates using Stata's 'duplicates' command. Type 'help duplicates' in the command window to get information on the syntax and some examples.

              Comment


              • #8
                Evelyn
                thanks for your information.. i can't find out any way to drop this problems. i transfer that data to foxpro file use pack command to delete it but i need a sata command that can help me out...
                lets see if your provided command can solve my problem
                thanks again

                Comment


                • #9
                  Raeedur:
                  are you sure that all your variables are in numeric format?
                  Kind regards,
                  Carlo
                  (Stata 19.0)

                  Comment

                  Working...
                  X