Announcement

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

  • Adding Dummy Variables

    Hi All,

    So I am new to using Stata. I have a preset excel file and wanted to create dummy variables using if else statements for the year column. So for eg, if the year is 1980, all observations having Year=1980 should have a 1 in the 1980 column, otherwise 0. But everytime I try creating such a statement I get an error "no; data in memory would be lost"

    Requesting your help for the same.

    Thanks in Advance.

  • #2
    That is not an error message you would get when creating a variable (dummy or otherwise), so you are not telling us exactly what you did. As a consequence, we cannot tell you what went wrong.

    However, in general, you hardly ever make indicator (dummy) variables in Stata anymore. Instead, we keep the year variable as is, and enter it in our model as a factor variable. See help fvvarlist
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      I first used the import excel command to read in the file and then was trying the following command " gen 1980 if year==1980 {generate '1'} else { generate '0'}
      Do let me know if there is an easier way to do this.

      Comment


      • #4
        You should read the help file of a command when you are about to use it, say -help generate-.

        Otherwise what you want to do is done best for one year (or few years)

        Code:
        gen dummy1980 = year==1980
        Or more economically on the writing if you have to do for many years

        Code:
        tab year, gen(dummy)

        Comment


        • #5
          On similar lines, I was wanting to insert dummy variables, where if the city is located in a certain group of states (eg: CA, AL, TX) it is marked as 1 or otherwise marked as 0. I tried putting all the abbreviations for it, but it says the code is too long. How do you create a dummy variable that satisfies a group of terms?

          Comment


          • #6
            For this one you need to read the help of -dataex-, and provide a minimal example of your data, and how you want your result to look like.

            Your explanation in #5 is too abstract for me to imagine how code doing it would look like.

            Comment


            • #7
              STATEPOST Expenditure
              CA 500
              TX 100
              Suppose this an example of a few data points. Now i want it to basically create a dummy variable that takes the value 1 if Statepost is CA or TX, else it is 0. In reality I have about 11 such Stateposts for which I want it to take a value of 1, else be 0.

              Comment


              • #8
                gen dummy = inlist(statepost,"CA","TX")

                Comment


                • #9
                  Since they are string variable, should I first convert them to numeric? I am getting a type mismatch in the current format?

                  Comment


                  • #10
                    I told you to use -dataex- and provide an example of your data that we can work with.

                    I have no idea why you are getting a "type mismatch". I am not getting it in my Stata 15:

                    Code:
                    . clear
                    
                    . *(2 variables, 2 observations pasted into data editor)
                    
                    . gen dummy = inlist(statepost,"CA","TX")
                    
                    . list, clean
                    
                           statep~t   expend~e   dummy  
                      1.         CA        500       1  
                      2.         TX        100       1

                    Comment


                    • #11
                      Click image for larger version

Name:	Data Example.PNG
Views:	1
Size:	96.1 KB
ID:	1479419
                      Here is an example of the data. The statepost tab can be seen too.

                      Comment


                      • #12
                        Sorry but it says the data is too large for dataex to work. Also I just noticed that the Statepost codes somehow have a space after them. So they read "CA " and "TX " not "CA" and "TX". Was wondering if that causes any issues leading to type mismatch errors.

                        Comment


                        • #13
                          This is why I am telling you that you should use -dataex-, not just to make your life miserable. These things, like where there is white space and where there is not, can be only seen if I see exactly the data you have.

                          And you are not using -dataex- properly. It is supposed to extract a subsample of your data (I think 200 observation max, but even that many are not necessary).

                          try

                          Code:
                          replace STATEPOST = trim(STATEPOST)
                          and then do what I showed you above with the inlist().

                          Comment


                          • #14
                            I fixed the error about the spaces by using a revised dataset. As for the above code, it is giving me a type mismatch when I try to paste the "replace" line itself. So i think the issue is in the STATEPOST column. It is already in the numeric form, so I do not see why it is throwing up such an error.

                            Sorry for not being able to use dataex, I am completely new to stata and just started using it today. So still figuring out the basics itself.

                            Comment


                            • #15
                              This is an awesome example of goodwill, yet the fail to follow the clear advice (please, do read the FAQ) about sharing data (dataex can easily provide a fraction of the data) entails overwork and unnecessary work.
                              Best regards,

                              Marcos

                              Comment

                              Working...
                              X