Announcement

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

  • Tag time-invariant & time-varying variables in panel

    Hi Statalist

    This probably has a very simple solution but I am struggling to figure it out.

    For a panel dataset, we can readily identify time-invariant & time-varying variables by -xtsum- and the time-invariant variables are those with a Within standard deviation of 0.

    But is there a way to tag/identify those variables that are time-varying or invariant in such a way this identification can be used for later procedures? For example, suppose I want to -reshape wide- and instead of typing out -reshape wide <time-varying1> <time-varying2> <time-varying3>, i(id) j(time)- (there are >100 variables altogether) I can simply use the variables already tagged as time-varying.

    The closest information I found is https://www.stata.com/support/faqs/d...ions-in-group/ but my question differs in that I am not looking to list observations within a column but to identify the whole column.

    Thanks.

  • #2
    findname (Stata Journal) offers a hook for finding variables that are constant; and the others are the complementary subset:

    Code:
    findname, all(@==@[1]) not
    because if all values are the same then all values are equal to any particular value (and all datasets you care about have a first observation).


    If that is not what you want, then the answer is some easy variation on the FAQ you cite.

    Comment


    • #3
      Many thanks Nick. Very useful to know about the -findname-

      Can I please ask if it is possible to 'trick' -findname- to do something akin to the following (my data is a panel)?
      Code:
      . bysort id (year): findname, all(@ == @[1]) not
      I know -findname- cannot be used with -by- or -bysort- but might there be a way to mimic that command? Thanks.

      Comment


      • #4
        No. You have to write your own loop. Also, watch out: you are in danger of losing any panel identifiers.

        Here by construction useless is useless and the loop finds it.


        Code:
        webuse grunfeld , clear 
        
        clonevar useless = 42 
        
        gen work = . 
        
        unab all : * 
        local needed company year work 
        local all : list all - needed 
        
        
        quietly foreach v of local all { 
            bysort company (`v') : replace work = `v'[1] == `v'[_N] 
            su work, meanonly 
            if r(min) == 1 & r(max) == 1 local todrop `todrop' `v' 
        } 
        
        drop work 
        di "`todrop'"


        Comment


        • #5
          Thanks Nick once more for the worked example using the -grunfeld- data.

          Sorry I had a hard time following each step in your loop. For example, I couldn't get past
          Code:
          . clonevar useless = 42 
          42 invalid name
          r(198);
          But using your code as a template, I muddled along to come up with
          Code:
          . webuse grunfeld , clear 
          
          foreach v in company year invest mvalue kstock time {
              quietly xtsum(`v')
              if r(sd_w) == 0 local timeinvariant `v'
          }
          
          display "`timeinvariant'"
          Hopefully this is consistent with what you were conveying in your example.


          Comment


          • #6
            Sorry, but you're right.

            Code:
            clonevar useless = 42
            should have been

            Code:
            general useless = 42
            I originally had something slightly different, changed it for trivial reasons and forgot to check that it still worked.




            Comment


            • #7
              Not a problem Nick. Thanks very much for your help!

              Comment


              • #8
                Insufficient coffee: #6 should have been

                Code:
                 
                 generate useless = 42

                Comment


                • #9
                  Yes, I figured it out. Thanks Nick. I mean the -generate- command and not your insufficient intake of coffee

                  Comment


                  • #10
                    Hi Statalisters,

                    I have been working on a similar loop but for all variables in the dataset to find all time invariant variables.

                    Code:
                    foreach var of varlist _all {
                        quietly xtsum(`var')
                        if r(sd_w) == 0 local timeinvariant `var'
                    }
                    
                    display "`timeinvariant'"
                    However, the display command only displays the last variable in the dataset that is time invariant. Is there any way I could modify this to display all of the time invariant variables and not just the final variable?

                    Thanks

                    Comment

                    Working...
                    X