Announcement

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

  • loop with "recursive" variable definition

    Hi everyone,

    I'm quite new to Stata so apologies if this question is "basic".
    I have a panel with the following variables: (firm) ID, Industry Code, Year, and Income.
    I would like to generate a variable called Risk which would be the standard deviation of Income for all firms within the same industry and for all previous years.
    E.g. say the panel starts with year t, I want the standard deviation for all firms with the same value of Industry Code in year t; for year t+1 I want the standard deviation for year t AND t+1 etc.
    I guess I need some kind of foreach loop but I'm really stuck here.
    Thank you

    Valentina

  • #2
    Your explanation is a bit unclear. On the one hand you say "all previous years" yet your next paragraph suggests that you want to include the current year as well. The code below includes the current year. Change 0 to -1 if you want only previous years.

    Code:
    rangestat (sd) Income, by(IndustryCode) interval(Year . 0)
    Note: Requires the -rangestat- command, by Robert Picard, Nick Cox & Roberto Ferrer, available from SSC.

    Comment


    • #3
      Thank you, you are right and actually I should have written "for current and all previous years".
      I didn't know about rangestat but actually I'm a bit confused about the "interval" part of the code. I understand the upper bound but for the lower bound the documentation says: "If low is specified using a system missing value, low is set to missing for all observations". I cannot square this description with the command I want to execute..?

      Comment


      • #4
        Yes, and right after that it says:

        rangestat applies the same rules as inrange() for missing bounds: if the lower bound is missing, observations will match up to and including the value of high.
        In other words, -interval(Year . 0)- means everything less than or equal to Year is a match.

        The treatment of missing value in the -interval()- option follows the way it works in Stata's built-in -inrange()- function. See -help inrange()- for details.

        Comment


        • #5
          thanks that helped!

          Comment

          Working...
          X