Announcement

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

  • Saving tabstat results in new variables

    Hello,

    I'm using tabstat to generate weighted standard deviations, by year, for two variables in a panel dataset. What I would like is to add these to my dataset as new variables. I've got some of the way here, but cannot quite get it to work.

    First, I generate the statistics, as follows:
    Code:
    tabstat lpcpi lpcpinf [aw=pop], by(year) stat(sd) columns(statistics) save
    I think this gives me the right output. Doing a little digging, I found code that puts the total standard deviations in a new variable, as follows:


    Code:
            matrix stats=r(StatTotal)
            svmat stats, name(var)   
        svmat stats, name(var)

    But of course this is just the sd for all observations, not each year's sd in an observation pertaining to that year.

    Any ideas?

    Thanks
    Tom

  • #2
    Tom:
    have you taken a look at -statsby-?
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      collapse is another candidate.

      Comment


      • #4
        Thanks both. But both of these are 'destructive' in the sense that they replace the current data with summary stats. I could do this in a pinch and make it work, but ideally I would simply put sd values for year x into rows that pertain to year x - as there is other data I want to work with, and some of it should not be weighted.

        Any further ideas?
        Thanks again.




        Comment


        • #5
          You can merge back with the original dataset.

          As it happens, some commands that would otherwise be useful don't support weights, such as egen.

          Comment


          • #6
            Out of curiosity at this point - I used statsby, then merged in the resulting datasets back into the original. Quick and dirty, but if there's a shortcut, I would be interested.

            Comment


            • #7
              There's a long cut. Rewrite egen to support weights.

              Comment

              Working...
              X