Announcement

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

  • loop for data sets

    Dear Users,
    I have 3 datasets:
    datacomp_112022, dataplant_112022, and datainds_112022.
    All the datasets have the same variables but they have different years (this is not an issue). I would like to run this code on all these data sets using a loop. If this is possible, could any one help please?
    Thanks

    The code is (I got this code in one of my previous questions on Statalist):
    Code:
     
     bysort comp year: gen which = sum(code_a != code_a[_n-1])  forval j = 1/3 {      egen code_a`j' = mean(cond(which == `j', code_a, .)), by(comp year) }

  • #2
    Give two dataexes of these datasets. And please, reformat your code such that it's more legible

    Comment


    • #3
      Not sure what you are asking, but a guess is:

      Code:
      foreach p in comp plant inds{
          use data`p'_112022, clear
          bysort comp year: gen which = sum(code_a != code_a[_n-1])
          forval j = 1/3{
              egen code_a`j' = mean(cond(which == `j', code_a, .)), by(comp year)
          }
         save data`p'_112022, replace
      }
      If you mean that you want to first combine the datasets before running the code, see either

      Code:
      help append
      or

      Code:
      help merge
      For any specific code suggestion, show a data example of at least 2 of the datasets using dataex.

      Comment


      • #4
        Dear Jared:
        I cant provide data example due to legal reasons. I am only creating examples of my own which are done in excel.

        Dear Andrew:
        Thanks for the code. It is exactly what I wanted!

        Comment


        • #5
          Yeah then create a simulated example and then include them as dataexes. There's nothing that prevents you from including a data example that illustrates the problem. So long as we can recreate the error, the contents of the file likely don't matter

          Comment


          • #6
            Jared Greathouse is right. Indeed we address this point directly in the FAQ Advice.

            If your dataset is confidential, then provide a fake example instead.
            So, please note that for future threads.

            Comment


            • #7
              Sure! Thanks, Nick and Jared.

              Comment

              Working...
              X