Announcement

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

  • Generate creating new variable with 0 observations

    I'm pretty new to Stata, so thanks in advance for your help! I'm trying to create a new variable that combines two existing variables, which should be very easy: generate newvar = v1 + v2. However, when I do this, my newvar has 0 observations (they're all being converted to missing values). Why might this be happening?

  • #2
    I'm quite sure it means that in every observation in your data set at least one of the two variables has a missing value.

    Now, that may indicate that your data set is incorrect. If so, you need to revisit how it was created, or, if somebody else created it, have them do that. Or, if it is correct that one of them is always missing, then it makes no sense to add them to create a new variable. What you probably want in that case is to have the new variable take on the value of v1 if it is non-missing, and v2 otherwise. A quick way to do that would be:
    Code:
    assert missing(v1, v2)
    gen wanted = min(v1, v2)
    Correction: I shouldn't have said I'm sure. There are other possibilities, but they are rather unlikely. If this isn't the answer, post back with example data using the -dataex- command, and also show the exact command you used to create the variable. If you are running version 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.

    Last edited by Clyde Schechter; 04 Nov 2022, 15:47.

    Comment


    • #3
      Yep, you're right! I'm using a dataset someone else created and shouldn't have assumed they'd cleaned it properly. Thank you for answering my very basic question.

      Comment


      • #4
        ..and shouldn't have assumed they'd cleaned it properly
        Yeah, you should never assume that any data set has been cleaned properly--even if you cleaned it yourself. Data management doesn't get much attention in statistical education, but, frankly, it's far more complicated and wide-ranging in the problems it presents than data analysis. When you've seen one regression, you've pretty much seen them all. But every data set presents new, idiosyncratic challenges. And, of course, if the data is wrong, analyzing it is just a fool's errand.

        Comment


        • #5
          Your code worked for solving the issue! Thank you so much.

          Comment

          Working...
          X