Announcement

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

  • Running a loop through variables and through files

    Can anyone think a way in which I can run a loop through values and through files?

    My case:

    I have quarterly surveys in a folder.
    I need to create a birth cohort variable in each survey as: year-age.

    The main problems are two:

    1. I can not come up with a way in which in each file a different 'year' is used (i.e in 2007 survey 'year' should be 2007 in 2008 survey 'year' should be 2008)
    2. Even having a way to do that, I need a way in which in all the four quarters of the same year the variable 'year' does not change.

    I do not have a variable in the survey which indicates the year in which it was conducted (which simplifies considerably the problem)

    Thanks in advance

  • #2
    What's the structure of file names? Something must indicate the temporal dimension of the surveys (or not?).
    You should:

    1. Read the FAQ carefully.

    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

    Comment


    • #3
      Regarding to the filenames, it does indicate the time. The names are e.g. per20071 per20072 per20073 per20074 per20081... per20134

      Comment


      • #4
        Yes, if the file name's contain the "year" and the "quarter", you can exploit that in a loop.

        Comment


        • #5
          Then use the relevant parts of the filenames to construct your loop. See -fs- by Nick Cox from SSC which is a convenience command to list file names in a directory. See also -help string functions- to manipulate strings. The last character of your file name indicates the quarter and the four before that the year. That should be enough, if I understand the problem correctly. (I'd like to give an example but I'm writing from a clunky mobile device.)
          You should:

          1. Read the FAQ carefully.

          2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

          3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

          4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

          Comment


          • #6
            If you know the years then a simple -forvalues - should be enough.
            You should:

            1. Read the FAQ carefully.

            2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

            3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

            4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

            Comment


            • #7
              Thank you very much, I solved it in this way:


              (Part code, not for running) local files : dir "`c(pwd)'" files "*.dta"
              fs *.dta

              foreach file in `files' {
              use `file', clear
              local file_name = "`file'"
              drop year cohort
              gen year=substr("`file_name'", 9 , 4 )
              destring(year),replace

              capture confirm variable edad
              if !_rc {
              gen cohort=year-edad
              }
              else {
              gen cohort=year-p03
              }
              local i=`i'+1
              cd ".."
              save, replace
              }
              (End of part code)

              Probably not the most efficient way, but it worked.
              Thanks again.
              Last edited by Alberto Basantes; 03 May 2014, 19:34.

              Comment

              Working...
              X