Announcement

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

  • Drop observations within a certain variable

    Hello everyone,

    I have a panel data of N countries and X years. I want to drop only the observations for variable Var1 in year 1997 for all countries.
    Using
    - drop Var1 -> i can drop the whole variable for all years and countries
    - drop if year==1997 -> drop all observations for all variables if year is 1997

    I tried "drop Var1 if year==1997" and got an error: "=exp not allowed".
    Any suggestion or workaround?

    Thanks!

  • #2
    Dear Stefan

    The "drop" command tells Stata to delete a variable (column) or cases (rows) from the dataset.
    It cannot be used to set observations on a variable for a subset of cases to missing

    I understand that you
    - want to keep the variable 'var1' in your dataset
    - want to keep the observations from 1997 in your dataset

    I suggest you try

    . replace Var1=. if year==1997

    This sets the values of Var1 to missing (.) for the year 1997

    Comment


    • #3
      Works perfectly. Thank you!

      Comment


      • #4
        Dear Statalist members,

        I have generated time series variable Trade1_var1. Values 1 or -1 indicates that buy/sell signal has been initiated.

        My next step is to generate Trade_2_var1. It should take value 1 or -1 if Trade1_var1=1/-1. But within the next 10 days all subsequent signals should be replaced with =.

        In other words after a first buy/sell signal one should wait for the next 10 days and ignore any signals within this period.


        Any thought how to impelment it?

        Click image for larger version

Name:	Each 10 days.GIF
Views:	1
Size:	5.1 KB
ID:	1315330


        Thank you!
        Last edited by Olena Onishchenko; 04 Nov 2015, 03:30.

        Comment


        • #5
          Hello Every one,

          I have a panel data set from 1998 to 2014 for all the variables. I want to drop two years data set from my data set. How can i do that. I tried to apply some commands but the are not working as stata is showing error.

          Thanks.

          Comment


          • #6
            Originally posted by Rafia Zaheer View Post
            Hello Every one,

            I have a panel data set from 1998 to 2014 for all the variables. I want to drop two years data set from my data set. How can i do that. I tried to apply some commands but the are not working as stata is showing error.

            Thanks.
            Suppose that you want to drop data in 1998 and 1999. Please try:
            HTML Code:
            drop if year == 1998 | year == 1999
            Ho-Chuan (River) Huang
            Stata 19.0, MP(4)

            Comment


            • #7
              Right. Thanku

              Comment


              • #8
                Hello Every one,

                I have a panel data set. I have some data in yearly form while other n daily basis. i want to match frequency of yearly variable with variables having daily basis data. Can any boy tell me the command where yearly values get repeats and came in daily form?.

                Thanks.

                Comment


                • #9
                  Rafia: Your question has nothing to do with the title of the thread. Please start a new thread. The question would benefit from a data example.

                  Comment


                  • #10
                    Hello Everyone,

                    I want to drop observations starting with 3, the variable is type int, I used drop if Variable ==3*, it didn't work (invalid syntax). Any suggestions?

                    Thanks!

                    Comment


                    • #11
                      All variables have to be one word/continuous...

                      Comment


                      • #12
                        Med Dri:

                        If your variable is string then

                        Code:
                        drop if substr(var, 1, 1) == "3"
                        and if numeric then

                        Code:
                        drop if substr(string(var), 1, 1) == "3"
                        On "Med Dri": Is that your given and family name, as we ask here?

                        Comment


                        • #13
                          Hello Statalist,

                          I'm trying to reduce my dataset by one of my variables to certain company_ids for a series of 207 events.
                          I reduce the dataset to one event date, (each event date has 20 post event days on which i measure abnormal returns) and sort on days and abnormal returns. I only want to retain the company_ids which fall in the top and bottom deciles on the event day, and then total the abnormal returns for these deciles/portfolios on each subsequent day up to 20. I have the following code so far:
                          Code:
                          keep if event_date==32797 
                           
                          *sort the days 0-20 and abnormal return from highest to lowest*
                          
                          gsort +days -abnormal_return
                          
                          *take deciles, and keep only 1st and 10th (Loser and Winner portfolios respectively)
                          
                          xtile percentiles = abnormal_return if days==0, nquantiles(10)
                          
                          drop if percentiles==2
                          drop if percentiles==3
                          drop if percentiles==4
                          drop if percentiles==5
                          drop if percentiles==6
                          drop if percentiles==7
                          drop if percentiles==8
                          drop if percentiles==9
                          
                          *Save portfolios*
                          
                          save event1
                          
                          *Restrict to top decile portfolio*
                          list company_id if percentiles==10
                          *input the listed company_ids*
                          use event1 if (company_id==10 | company_id==14 | company_id==20 | company_id==25 | company_id==57 | company_id==58 | company_id==81 | company_id==95 | company_id==106 | company_id==107 | company_id==114 | company_id==120 | company_id==122)
                          
                          capture total abnormal_return if days==0
                          estimates store CAR01W
                          capture total abnormal_return if days==1
                          estimates store CAR11W
                          capture total abnormal_return if days==2
                          estimates store CAR21W
                          capture total abnormal_return if days==3
                          estimates store CAR31W
                          .... and so on to days==20
                          Is there code to simplify this, so i don't need to list the companies which fall into the top and bottom deciles for each event?

                          Thanks

                          Comment


                          • #14
                            Code:
                            keep if event_date==32797    
                            
                            *sort the days 0-20 and abnormal return from highest to lowest*  
                            gsort +days -abnormal_return  
                            *take deciles, and keep only 1st and 10th (Loser and Winner portfolios respectively)  
                            xtile percentiles = abnormal_return if days==0, nquantiles(10)  
                            keep if inlist(percentiles, 1, 10)  
                            
                            *Save portfolios*  
                            save event1  
                            *Restrict to top decile portfolio*
                            
                            list company_id if percentiles==10
                            
                            *input the listed company_ids*
                            use event1 if inlist(company_id,10,14,20,25,57,58,81,95,106,107,114,120,122)  
                            
                            forval d = 0/20 {      
                                capture {          
                                    total abnormal_return if days==`d'          
                                    local D : di %02.0f `d'          
                                    estimates store CAR`D'1W    
                               }
                            }


                            Labelling 00, 01, ..., 09, 10, 11, .... often looks better (and sorts better when relevant).

                            Comment


                            • #15
                              Thank you Nick, that's much more streamlined. Is there a way to circumnavigate inputting the listed company IDs into
                              Code:
                               
                               use event1 if inlist(company_id,10,14,20,25,57,58,81,95,106,107,114,120,122)
                              Or would this have to be done manually for each portfolio of each event?

                              Comment

                              Working...
                              X