Announcement

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

  • Collapsing multiple variables

    Hello all,

    I have searched on the web about something that I still can't understand and that's why I'm posting here.
    I want to collapse more than 300 variables, and I'd not like to write them all (they are just 5 generic variables). The variables that I'm collapsing are like house1940, house1941... (with a previous forval house`i'). How could I just write inside the collapse: house`i' (instead of writing house1940, house1941, etc.)? Maybe I could create a varlist, but I don't understand how to do it and also I don't know how to put it into the collapse command.

    Thank you.

  • #2
    Why search on the web? The documentation for collapse is there inside Stata.

    collapse understands varlists, and varlists allow wildcards. Consider this:

    Code:
    sysuse auto
    collapse t*, by(foreign)
    The only tricky thing is when you want two or more statistics from each variable. You didn't say that's what you want, so I will let you explain if your question is not what you asked.

    The syntax you cite could at most specify one variable, depending on the value of the macro i. (In principle, the macro could contain a wildcard character, but that didn't seem your implication.)

    Comment


    • #3
      It is not what I'm looking for because I have not defined time series.

      For example, I have variables like: house1940, house1941, house1942...house2000; apart1940, apart1941...apart2000

      So, I would just like to do:

      collapse (sum) house`i' apart`i' , by (city), // and not writing down each variable per year

      instead of doing this:

      collapse (sum) house1940 house1941 house1942 apart1940 apart1941

      Thank you Nick.


      Comment


      • #4
        Not so; what I recommended is precisely what you want.

        Code:
        collapse (sum) house* apart* , by(city)
        Also, your local macro syntax will not work here, unless exceptionally local macro i contains "*", as I explained.

        Comment


        • #5
          Wow, that was very useful. It was exactly what I wanted (what a trick!), without you I would be losing plenty of time searching or writing all of them.

          I didn't understand at all your last sentence. When variable*, it also calls others variables without years included, like houseexp. ("local macro i contrants "*", I didn't get that)

          Thank you so much Nick!

          Comment

          Working...
          X