Announcement

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

  • dropping a variable base of the condition of another variable

    Hi ,
    I want to drop the variable if the variable there is no value in the column variable or the result is 0 when sum of all values of the variable. I want to drop "sch_pri_no_bicyle" that has no value or as sum_sch_pri_no_bicyle is equal to Zero.
    drop sch_pri_no_bicyle if sum_sch_pri_no_bicyle==0
    But, it doesn't work.
    
    Please here is sample data set

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int(sch_pri_no_bicyle sch_mid_no_bicyle sch_hig_no_bicyle) float(sum_sch_pri_no_bicyle sum_sch_mid_no_bicyle sum_sch_hig_no_bicyle)
    . 35 35 0 190 725
    . 20 20 0 190 725
    . 30 30 0 190 725
    . 20 20 0 190 725
    .  .  . 0 190 725
    end
    ------------------ copy up to and including the previous line ------------------

    

  • #2
    Your code doesn't work for me without a fix to remove characters not visible above. That said findname (Stata Journal) can do for you:


    Code:
    . findname, all(missing(@))
    sch_pri_no~e
    
    .
    . findname, all(@==0)
    sum_sch_pr~e
    and after each run of findname you can


    Code:
    drop `r(varlist)'

    . search findname, sj

    Search of official help files, FAQs, Examples, SJs, and STBs

    SJ-15-2 dm0048_3 . . . . . . . . . . . . . . . . Software update for findname
    (help findname if installed) . . . . . . . . . . . . . . . N. J. Cox
    Q2/15 SJ 15(2):605--606
    updated to be able to find strL variables

    SJ-12-1 dm0048_2 . . . . . . . . . . . . . . . . Software update for findname
    (help findname if installed) . . . . . . . . . . . . . . . N. J. Cox
    Q1/12 SJ 12(1):167
    correction for handling embedded double quote characters

    SJ-10-4 dm0048_1 . . . . . . . . . . . . . . . . Software update for findname
    (help findname if installed) . . . . . . . . . . . . . . . N. J. Cox
    Q4/10 SJ 10(4):691
    update for not option

    SJ-10-2 dm0048 . . . . . . . . . . . . . . Speaking Stata: Finding variables
    (help findname if installed) . . . . . . . . . . . . . . . N. J. Cox
    Q2/10 SJ 10(2):281--296
    produces a list of variable names showing which variables
    have specific properties, such as being of string type, or
    having value labels attached, or having a date format

    (end of search)

    Note that I didn't take you literally. A variable with values 1 -1 will have sum zero but seems likely to be informative.

    Comment


    • #3
      I would like to do something similar. I am dealing with a wide data that I would like to filter in a certain way.
      I have the following variables:
      hh_name_1, age_calc_1, hh_relationship_1. These set of variables repeat for 12 times.
      I would like to drop the 3 variables (hh_name_1, age_calc_1, hh_relationship_1) if the hh_relationship_1 is not equal to 2 or 4.
      I can't use the drop if option because it will remove the entire observation and I want to keep the rest of my variables.
      Do you have any suggestions how I can do so?

      Thank you.

      Comment


      • #4
        In Stata, you can drop entire observations, or you can drop entire variables, but you cannot drop variables for only certain observations, and you cannot drop observations for only certain variables. You can, however, make the value of a variable "missing" depending on some logical condition, and thereby exclude observations with those variable values from entering into analyses. Or, you can identify observations with particular values and include or exclude those from a command.

        The second and better approach would look like this:
        You don't say why you want to "filter" observations, but I'm guessing you just want analyze only observations with the desired values
        Code:
        Whatever_Stata_Command if if inlist(hh_relationship_1, 2,4) // only the desired ones
        If this doesn't make sense in your situation or otherwise won't work for you, post again and explain.

        Comment

        Working...
        X