Announcement

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

  • How to create a dummy variable from two variables

    Hi!
    I have a dataset with two variables for test scores: scores in 2002 and scores in 2013. I want need to merge the two variables into a dummy variable so to include the test scores of the two years.
    So the new variable "time" should look like:
    time= 0 if test_scores_2002
    time= 1 if test_scores_2013

    So far I have not been able to find a command to generate the dummy variable from two continuous variables, and would appreciate any help!
    thank you!

  • #2
    It is not clear now (at least to me) what you want to do.

    Dummy variables are 0/1 variables. Do you really want a dummy variable, or you want a variable score, which is equal to test score in the respective year?

    Comment


    • #3
      yes, my problem is that I don't have a dummy year variable in the dataset, but distinct variables for the two years considered. f.e. I have a variable for test scores in 2002 and one for test scores in 2013. I want to evaluate the effect of a treatment on the students scores, and I was thinking of using the difference-in-differences method. I have a treatment dummy variable, dividing the observations in students receiving the program and students not receiving the program, but I don't have a time variable. Therefore I thought of generating a time variable by merging the two variables of test scores in 2002 and 2013. In this way I would be able to compare the test scores in 2013 of the treated group of students with their scores in 2002 (when the treatment was not implemented) and the scores in 2013 of a not treated group of students with their respective scores in 2002.
      I hope this makes it clearer,
      thank you!

      Comment


      • #4
        It's still not clear what you want. But I'll guess that you have data that looks something like this:
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(student_num score_2002 score_2013)
         1  96.82182  120.2323
         2  97.64053  81.48185
         3  97.78952    50.041
         4  59.90504  71.68887
         5 117.62665 64.797356
         6 138.66441  99.22935
         7  72.86063  96.64683
         8  54.30534 145.46921
         9  72.78477  95.81928
        10  150.3101  98.59268
        end
        and you want to have

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float student_num int year float score
         1 2002  96.82182
         1 2013  120.2323
         2 2002  97.64053
         2 2013  81.48185
         3 2002  97.78952
         3 2013    50.041
         4 2002  59.90504
         4 2013  71.68887
         5 2002 117.62665
         5 2013 64.797356
         6 2002 138.66441
         6 2013  99.22935
         7 2002  72.86063
         7 2013  96.64683
         8 2002  54.30534
         8 2013 145.46921
         9 2002  72.78477
         9 2013  95.81928
        10 2002  150.3101
        10 2013  98.59268
        end
        This is what the -reshape- command does. Do read -help reshape- and click the link near the top of that page to the full explanation in the PDF documentation. It takes some practice to get comfortable with using -reshape- and develop an intuitive grasp of what goes in the -i()- and -j()- parts. But in this case the command would be
        Code:
        reshape long score, i(student_num) j(year)
        In the future, bear in mind that it is often difficult to explain in words what needs to be done. It is typically very helpful to

        a) Show an example of the data you are starting out with (using the -dataex- command, as I have done here.)
        b) Work out by hand what the results you want from that example would look like and show that (also using -dataex-).
        c) Provide a brief explanation of the steps you used to work out the b) example from the a) example, step by step.

        Certainly, if what I have shown is not what you want, please follow these steps when posting back. If it is what you want, bear these steps in mind for the future, so you can get helpful responses in a more timely manner.

        In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it 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.

        When asking for help with code, always show example data. When showing example data, always use -dataex-.


        Comment

        Working...
        X