Announcement

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

  • Replace/dropping observations in variable conditional on observation in other variable

    Hi, I am trying to replace/ drop an observation conditional on another variable. I want to drop every 5th row of observations in FLOWMutual/FLOWLret. As it is a panel data set with observations across time it caused overlap between periods of the change function I used. I've tried multiple codes using drop / replace etc but I can't for the life of me figure out how to fix it, even though it seems as such a simple task. Does anyone know how to tackle this or to perhaps modify the % change function below to not get the observations for the overlapping time periods? Thanks in advance!

    FLOWMutual / FLOWLret function:

    gen FLOW = (tna_latest - tna_latest[_n-1])/tna_latest[_n-1]*100



    I have tried the codes below to no success:

    replace FLOWMutual = . if turn_ratio = .
    drop FLOWmutual if Date = 31dec2019
    foreach x of varlist FLOWmutual turn_ratio{replace `FLOWMutual' = 0 if (`turn_ratio' = .)}

    Click image for larger version

Name:	Scherm*afbeelding 2024-11-15 om 21.07.35.png
Views:	1
Size:	76.9 KB
ID:	1767710

  • #2
    without telling us more, all i can say is that this part of the code is wrong:
    Code:
    if turn_ratio = .
    should be
    Code:
    if turn_ratio == .

    Comment


    • #3
      Your post is very confusing. On the one hand you talk in the title and opening sentence of dropping observations. But your code doesn't even go in that direction, instead trying to overwrite the values of some variables with missing values.

      If you truly want to drop every fifth observation in the data set, it is quite simple:
      Code:
      drop if mod(_n, 5) == 0
      If you actually want to overwrite the values of FLOWmutual with missing values when turn_ratio is missing, then it's
      Code:
      replace FLOWmutual = . if missing(turn_ratio)
      which is actually equivalent to your -replace FLOWMutual = . if turn_ratio = .- except that you need to use the == after turn_ratio. That command should have run without error. Since you site it as among the things you tried without success, I'm inferring that replacing FLOWmutual with missing values when turn_ratio has a missing value isn't what you are trying to get.

      I can only guess what you intended with -drop FLOWmutual if Date = 31dec2019- since there is, according to your example, no variable Date. Assuming that in your real data set such a variable exists, however, this command would be a pair of syntax errors. On the one hand you can't -drop- a variable -if- some condition. You can drop observations conditionally with an -if- clause. But variables are either dropped as a whole or not. It is not possible to drop only part of a variable in Stata. Perhaps again you are confusing -drop-ping with replacing the value with a missing value. In that case, the correct syntax is -replace FLOWmutual if Date == td(31dec2019)-. But that doesn't have any apparent relationship to the stated goal of doing something in every fifth observation. Perhaps if the Date variable had been shown, the connection would be apparent.

      I don't know if what I've said here is enough to help you. It really is unclear what you are trying to do. If you need additional help, please post back with a clear explanation of what you want. In fact, it is often more helpful to mock up something that looks like the results you want and show that, than it is to explain in words. Also, please be sure, in future posts, to show example data in the helpful way: with the -dataex- command. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

      More generally, please read the Forum FAQ for excellent advice about how to post here in ways that maximize your chance of getting a timely and helpful response.

      Added: Crossed with #2.

      Comment

      Working...
      X