Announcement

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

  • How to automatically generate code

    Hello folks, I am new to Stata and could use a bit of guidance as Google has not helped!

    I have a data set of about 100 tables and need to run the same commands on every table. I would like to know if I can get Stata to generate code from code, for example using SQL for a relational database to generate "tab tableA tableB, row column" for every table where tableB is always the same I would use something like this to generate 100 lines of code by just writing one line of code:

    select ||'tab '|| table_name ||' tableB, row column' from user tables;

    and this would produce code which I can then run, e.g.

    tab tableA tableB, row column
    tab tableB tableB, row column
    tab tableC tableB, row column
    tab tableD tableB, row column
    etc etc


    Hopefully that made some vague sense?! If something similar is possible in Stata could somebody please point me in the right direction.

    Many thanks,
    Ian.

  • #2
    Are you looking for something like
    Code:
    foreach table of varlist table? {
        if "`table'" == "tableB" continue
        tabulate `table' tableB, row column
    }

    Comment


    • #3
      Oh, I missed that you're tabulating tableB with itself. You can skip the if "`table'" == "tableB" continue line.

      (Do you need that first concatenation operator in your example SQL statement?)

      Comment


      • #4
        Good spot, that first concatonator was a typo! I will have a play and see if I can get something working with your foreach suggestion, thanks

        Comment


        • #5
          I'm not sure I follow what you're trying to do here, but is there a reason why the tab2 command with the firstonly option wouldn't work?

          Comment

          Working...
          X