Announcement

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

  • Forval

    Hi,
    I have this little problem with the following code:

    clear all
    forval i=1/1{
    use 201`i'-1.dta
    merge m:m Semestre Carnet using 201`i+1'-1.dta
    }

    I work with two datas 2011-1.dta and 2012-1.dta, but the fourth line of my code dont work. Is there any way to do that in Stata?

    Thanks in advance.


  • #2
    No surprise there. -merge m:m- is a trap for the unwary; it just produces data salad. What it does is almost never what people have in mind when they think this is what they should use. (And what it does is sufficiently obscure that in 25 years of using Stata I have only encountered 2 situations where it could have been used--and even then there were better ways to do the same thing.)

    To provide a correction, however, you need to explain what you want to do that you thought -merge m:m- would do for you. So you need to show examples of the two data sets and explain which observations in each you want to match up with which observations in the other. Be sure to use the -dataex- command to show your example data. If you are running version 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.

    Comment


    • #3
      Hi Clyde,
      Thanks for your reply.
      Here is an example for 2011-1.dta:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str87 Semestre str8 Carnet str10(fechaPago FechaVcto) str8 Numcuota
      "2010-II" "20104501" "02/01/2011" "15/12/2010" "4"
      "2010-II" "20104501" "02/01/2011" "15/12/2010" "4"
      "2010-II" "20081238" "02/01/2011" "15/12/2010" "5"
      "2010-II" "20081238" "02/01/2011" "15/12/2010" "5"
      "2010-II" "20052130" "03/01/2011" "31/08/2010" "1"
      "2010-II" "20052130" "03/01/2011" "31/08/2010" "1"
      "2010-II" "20093509" "03/01/2011" "15/12/2010" "1"
      "2010-II" "20093509" "03/01/2011" "15/12/2010" "1"
      "2010-II" "20093055" "03/01/2011" "02/11/2010" "4"
      "2010-II" "20093055" "03/01/2011" "02/11/2010" "4"
      "2010-II" "20094041" "03/01/2011" "30/11/2010" "4"
      "2010-II" "20094041" "03/01/2011" "30/11/2010" "4"
      "2010-II" "20081417" "03/01/2011" "15/12/2010" "5"
      "2010-II" "20081417" "03/01/2011" "15/12/2010" "5"
      "2010-II" "20052089" "03/01/2011" "15/12/2010" "5"
      "2010-II" "20052089" "03/01/2011" "15/12/2010" "5"
      "2010-II" "20051177" "03/01/2011" "15/12/2010" "5"
      "2010-II" "20051177" "03/01/2011" "15/12/2010" "5"
      "2010-II" "20071241" "03/01/2011" "15/12/2010" "5"
      "2010-II" "20071241" "03/01/2011" "15/12/2010" "5"
      "2010-II" "20102067" "03/01/2011" "01/12/2010" "5"
      "2010-II" "20102067" "03/01/2011" "01/12/2010" "5"
      end
      Here you have for 2012-1.dta

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str87 Semestre str8 Carnet str10(fechaPago FechaVcto) str8 Numcuota
      "2011-II" "46009346" "02/01/2012" "05/08/2011" "1"
      "2011-II" "46009346" "02/01/2012" "05/08/2011" "1"
      "2011-II" "20099018" "02/01/2012" "15/12/2011" "5"
      "2011-II" "20099018" "02/01/2012" "15/12/2011" "5"
      "2011-II" "20073232" "02/01/2012" "15/12/2011" "5"
      "2011-II" "20073232" "02/01/2012" "15/12/2011" "5"
      "2011-II" "20083118" "02/01/2012" "14/10/2011" "3"
      "2011-II" "20083118" "02/01/2012" "14/10/2011" "3"
      "2011-II" "20083118" "02/01/2012" "15/11/2011" "4"
      "2011-II" "20083118" "02/01/2012" "15/11/2011" "4"
      "2011-II" "20083118" "02/01/2012" "15/12/2011" "5"
      "2011-II" "20083118" "02/01/2012" "15/12/2011" "5"
      "2011-II" "20093224" "02/01/2012" "15/12/2011" "5"
      "2011-II" "20093224" "02/01/2012" "15/12/2011" "5"
      "2011-II" "20106151" "02/01/2012" "15/12/2011" "5"
      "2011-II" "20106151" "02/01/2012" "15/12/2011" "5"
      "2011-II" "20104247" "02/01/2012" "31/10/2011" "3"
      "2011-II" "20104247" "02/01/2012" "31/10/2011" "3"
      "2011-II" "20104247" "02/01/2012" "30/11/2011" "4"
      "2011-II" "20104247" "02/01/2012" "30/11/2011" "4"
      "2011-II" "20085099" "02/01/2012" "30/11/2011" "4"
      "2011-II" "20085099" "02/01/2012" "30/11/2011" "4
      end
      To put you in context, these two datas contain all the payments that students of a "X" university have made during the corresponding semester. For example, 2011-1.dta shows all payments made during the first half of 2011 and so on. What I want to do is track the payments of some students who are of interest for my study during the 2 semesters following their admission. This is because some are not up to date on their payments. The indentifier for this students is the variable "Carnet", which is repeated with the number of college fees that student pay during the semester, so, I thought m: m could be useful to merge this two datasets because CARNET is repeated many times in both datas.

      Thanks in advance.

      Comment


      • #4
        Well, your explanation doesn't sound like a merge at all. It sounds like you want an -append-. Try
        Code:
        use dataset1
        append using dataset2
        and see if that gives you what you want. If it doesn't, posts back and show what you want the result of combining these two data sets to look like.

        By the way, no loop needed for this. Just the simple code shown immediately above.

        Comment


        • #5
          Well, I think my explanation is not clarifying my purpose. First, I have data that contains information about all freshman from 2011 to 2018, this univeristy allows freshman in the two semestres of the year. So I have 16 semestres with around 9000 freshaman in total. Here is an example of this data for the first semestre of 2011:
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str8 Carnet str53 nombre str7 Semestre long semestre2
          "71462562" "ABAD BURGOS, KEVIN OLIVER"               "2011-I" 1
          "20093497" "ABAD VILLEGAS, LIZVANY LISSETH"          "2011-I" 1
          "74777725" "ACUÑA CUEVA, JOSE ALEJANDRO"            "2011-I" 1
          "70616546" "ADRIANZEN RAMIREZ, ALESSANDRA"           "2011-I" 1
          "70353603" "AGUILAR CALLE, OSCAR ALFREDO"            "2011-I" 1
          "75194326" "AGUILAR FLORES, ILLEANA LIZETH"          "2011-I" 1
          "74167684" "AGUILAR REA, KIARA YESE"                 "2011-I" 1
          "73145881" "AGUILERA PINTA, SMITH ALBERTO"           "2011-I" 1
          "75171440" "AGUINAGA GUEVARA, JULIO OSWALDO"         "2011-I" 1
          "71330612" "AGUIRRE VARGAS, AMET"                    "2011-I" 1
          "20093304" "AGUIRRE VILELA, CHRISTIAN GIANPIERRE"    "2011-I" 1
          "76237440" "AGURTO ZEGARRA, GRECIA PAOLA"            "2011-I" 1
          "48021961" "ALAYO LOPEZ, RENZO MANUEL"               "2011-I" 1
          "72632001" "ALBAN CARLIN, GEANCARLOS"                "2011-I" 1
          "72486549" "ALBAN MEJIA, LUIS EDUARDO"               "2011-I" 1
          "72218842" "ALBAN RUIZ, CARLOS MANUEL"               "2011-I" 1
          "70081236" "ALBURQUEQUE LOPEZ, MARITA JULIANA"       "2011-I" 1
          "71761825" "ALCANTARA CALDERON, CHRISTIAN JOSE"      "2011-I" 1
          "71475008" "ALCAS ANDRADE, ALEXANDER"                "2011-I" 1
          "73261849" "ALDANA MORE, LOURDES YAKORI"             "2011-I" 1
          "70340990" "ALFARO CHUMACERO, RONALD JOEL"           "2011-I" 1
          "73118359" "ALFARO HERRERA, CARLOS ALFREDO"          "2011-I" 1
          end
          label values semestre2 semestre2
          label def semestre2 1 "2011-I", modify
          With a loop code, I separate the aforementioned dataset in sixteen new datas for each semestre. For each of this data, like I mentioned in the other post, I want to track the payments of the freshman during the 2 semesters following their admission. I think append is the wrong way to work with this data because in each payment data I have thousands of payments that not only includes college fees of the new students of their semesters but also old students who have recently paid some debt. My objective is not copy, paste and run 16 times the code in the dofile, so I want to use a loop code. Again, I use merge m:m because the same carnet is repeated many times in master and using data.

          Comment


          • #6
            Excuse me, Clyde, for reply again. Here I present you a code:

            Code:
            clear all
            forval i=1/1{
            use socioeco-`i'.dta
            merge 1:m Semestre Carnet using 201`i'-1.dta
            drop _m==3
            save $sav/pays2011.dta
            clear all
            use pays2011.dta
            merge m:m Semestre Carnet using 201`i'-2.dta
            drop_m==3
            merge m:m Semestre Carnet using 201`i+1'-1.dta
            }
            Like I mentioned in the last reply, I have 16 datasets of freshers in sixteen semestres. For example, in the above code I want merge variable of fresher for the first semester of 2011 with their payments of this semester, so in the first merge code I use 1:m because the fresher´s indentifier (Carnet) and variable Semestre (semester in spanish) repeat one time in for each observation, and m because this two variables repated many times in the using data. Next, I generate a new dataset and merge with m:m because in using and master the observations repeated many time like I said before, but this time I use payments for the second semester of the 2011 (because maybe the fresher is still in debt with the university). The last merge code I follow the same logic but with the first semester of the following year, in this case is 2012.

            Comment


            • #7
              Here is example data for one student for two semesters for 2011.

              What is it that you want this student's data to look like after the process you describe?
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input float(student year semester) str7 charge float payment
              101 2011 1 "books"    200
              101 2011 1 "room"    1000
              101 2011 1 "tuition" 2000
              101 2011 2 "books"    250
              101 2011 2 "tuition" 1500
              101 2011 2 "fees"     250
              101 2011 2 "other"    100
              end

              Comment

              Working...
              X