Announcement

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

  • Replace/drop all variables with negative values

    Hi,

    I am having difficulties of finding an efficient and less time-consuming way of replacing negative values from my variable list. What I have initially done is use the replace function but this was for a few variables. Now, I'm interested in dropping all negative values within my dataset i.e. 278 variables and 2,000+ observations. Could anyone advice me on what's the best way to do this?

    I wasn't able to find an answer to this online, I came across the following commands recently but they do not work as my variables are not labelled as var1-varx...:
    egen rmin=rowmin(var1 - var50)
    drop if rmin<0

    Many thanks for your help!

    Rocio

  • #2
    Hi Rocio,

    You don't drop a value, you drop a variable (column) or a observation (row). You could do something like:

    Code:
    drop if var1 == . *This will drop all observations which var1 == . This does mean that if an obs was missing for var1 but had data for var2, the data of var2 for this observation will be lost (again, you drop an observation).
    
    drop var1 *this will drop var1 for all variables. var1 will not exist anymore on your dataset.
    You can use the command ds to list all variables in your dataset and select from those put in the code you provided. Note that the code you provided will drop all observations (rows) for which the minimun value in the row is <0.

    You might want to recode all negative values to missing instead of dropping the observations that contain them. In this case, a code as this would work:

    Code:
    local x var1 var2
    foreach x in `x'{
        replace `x' = . if `x' < 0
    }
    Replace var1 var2 with the list of variables you want to apply this. Again, ds is your friend:

    Code:
    ds

    Comment


    • #3
      Welcome to Statalist, Rocio.

      See the output of help varlist to see other ways of constructing variable lists in Stata. One of them should work for you.

      Comment


      • #4
        Part of the confusion is what you mean by drop is not clear. Do you mean eliminate observations with any negative values for any of the variables, or do you mean replace negative values with missing? In general, Stata will work just find with missing and will simply ignore observations with missing values so you often do not need to drop observations.
        If you look a little you will find folks have noted on Statalist how to create a macro that includes the names of all the variables in the dataset. Then you simply loop over that macro.

        Comment


        • #5
          Hello Experts,

          I want to replace the negative values for my variable with missing or a similar positive value. I have tried mvdecode, replace, and abs but nothing is working. Below are the codes I used and an example of data.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input long marriage_history byte husband_cohabit_first_month double husband_cohabit_start_recentSIF
          2 -88  1.751328e+12
          2 -88 1.7961696e+12
          2 -88  9.440928e+11
          2 -88 1.7592768e+12
          2 -88  1.627776e+12
          2 -88 1.1597472e+12
          2 -88 1.4832288e+12
          2 -88    1.7172e+12
          2 -88 1.1228544e+12
          2 -88 1.6145568e+12
          2 -88  1.199232e+12
          2 -88  1.230768e+12
          2 -88 1.0730016e+12
          2 -88  1.748736e+12
          2 -88 1.7672256e+12
          2 -88 1.3885344e+12
          2 -88  1.262304e+12
          2 -88   1.59624e+12
          2 -88 1.8251136e+12
          2 -88 1.5120864e+12
          2 -88 1.7672256e+12
          2 -88 1.1544768e+12
          2 -88 1.6909344e+12
          2 -88 1.7566848e+12
          2 -88 1.3254624e+12
          2 -88  1.654128e+12
          2 -88             .
          2 -88 1.1360736e+12
          2 -88  9.913536e+11
          2 -88 1.4201568e+12
          2 -88 1.1333952e+12
          2 -88 1.6461792e+12
          2 -88  1.675296e+12
          2 -88 1.7908992e+12
          2 -88 1.7961696e+12
          2 -88  1.322784e+12
          2 -88  1.504224e+12
          2 -88 1.3885344e+12
          2 -88 1.8039456e+12
          2 -88 1.4859072e+12
          2 -88 1.5989184e+12
          2 -88 1.5779232e+12
          end
          format %tc husband_cohabit_start_recentSIF
          label values marriage_history lived_list
          label def lived_list 2 "2. more_than_once", modify
          label values husband_cohabit_first_month month_list
          label def month_list -88 "-88. Do not know", modify
          Code:
          . mvdecode husband_cohabit_start_firstSIF, mv(99)
          
          . replace husband_cohabit_start_firstSIF=. if husband_cohabit_start_firstSIF<0
          (0 real changes made)
          
          . 
          . replace husband_cohabit_start_firstSIF= abs(husband_cohabit_start_firstSIF)
          (0 real changes made)
          I will request your guidance on this.

          Thank you
          Deepali
          Deepali Godha

          Comment


          • #6
            you have not followed the instructions in the -mvdecode- help file correctly; what goes in the "mv()" parentheses is the number you want to change to missing (presumably the "-88"'s - however, I can't be sure because the variable in your command is not the name of a variable in your data example - which also explains why one can't check what is going on with your -replace- commands

            Comment


            • #7
              Sorry Rich, that was an error on my part. Thank you
              Deepali Godha

              Comment

              Working...
              X