Announcement

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

  • Counter variable contingent on 3 variables and a filter

    Hi all,

    I have a question in regards to implementing a counter variable that is contingent on 3 other variables and a filter. The counter should start at 1 for every new observation in A and increase if the next observation in B has the same value. The count should start from the oldest year and only count if the filter does not equal 1. To illustrate this, please refer to the following data example.

    A B Year Filter
    1 1 94 1
    1 1 93 .
    1 2 96 .
    1 3 97 1
    1 1 95 .
    1 2 99 .
    2 4 91 .
    2 2 93 .
    2 2 92 1
    2 3 95 .
    3 2 90 .
    3 2 90 .

    The count values from top to bottom should be: ., 1, 1, ., 2, 2, 1, 1, 1, 1, 2

    I am using this code but this does not properly take into account the filter.
    sort A B Year, stable
    by A B: gen Counter = _n if Filter != 1

    I would appreciate any kind of help.
    Thank you very much.

  • #2
    It seems that you do care about the original order. Otherwise, the first and the last line of below code could be omitted.
    Code:
    gen originalorder=_n
    bys A B Filter (Year): gen x = _n if Filter==.
    sort originalorder
    Last edited by Romalpa Akzo; 23 Jul 2018, 06:29.

    Comment


    • #3
      Great, thank you.

      Comment


      • #4
        I have a follow up question that is related to a slightly different topic. I have a variable for which I would like to calculate the standard deviation. However, I do not want to calculate one standard deviation value but the standard deviation should get calculated again with every additional observation. To make it easy let's consider the same data example as above. However, now I would like to calculate the standard deviation of the observations of variable B, given that the filter does not equal 1 and in chronological order in terms of years. The standard deviation calculation should start again with every new observation in A. The st. dev. values from top to bottom should be (numbers are approximations): ., 0, 0.58, ., 0, 0.58, 0, 1.41, ., 1, 0, 0


        A B Year Filter
        1 1 94 1
        1 1 93 .
        1 2 96 .
        1 3 97 1
        1 1 95 .
        1 2 99 .
        2 4 91 .
        2 2 93 .
        2 2 92 1
        2 3 95 .
        3 2 90 .
        3 2 90 .

        Again I would appreciate any kind of help to solve this problem. Thank you very much.

        Comment


        • #5
          This requires rangestat, to install do:
          Code:
          ssc install rangestat
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input byte(A B Year Filter)
          1 1 94 1
          1 1 93 .
          1 2 96 .
          1 3 97 1
          1 1 95 .
          1 2 99 .
          2 4 91 .
          2 2 93 .
          2 2 92 1
          2 3 95 .
          3 2 90 .
          3 2 90 .
          end
          gen origorder=_n
          rangestat (sd) B, interval(Year . 0) by(A Filter)
          sort origorder

          In future posts, please make sure to always use dataex to post data examples. This makes it a whole lot easier for people to copy paste your data. For more on how and why of dataex, see the FAQ: https://www.statalist.org/forums/help#stata

          Comment


          • #6
            Hi Jorrit,

            thank you for your input. It already helps a lot. I think there still is a small mistake though because the sd is the same for all observations in the same year. If there are multiple observations in the same year, I would like to take the first observation from top to bottom to calculate the new sd and then the next one. I am trying to figure it out, but maybe you already have an idea on how to fix this issue?

            Thank you very much!

            Comment


            • #7
              Bit confusing, but maybe like this? Note that sort order (which affects the sd you ask for) now depends on A, then year, and the original sort order now. That is, its unclear how you would want the values of B sorted where there are multiple occurrences of one year.


              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input byte(A B Year Filter)
              1 1 94 1
              1 1 93 .
              1 2 96 .
              1 3 97 1
              1 1 95 .
              1 2 99 .
              2 4 91 .
              2 2 93 .
              2 2 93 1
              2 3 93 .
              3 2 93 .
              3 2 90 .
              2 4 93 .
              2 12 93 1
              2 56 93 .
              3 2 93 .
              3 2 90 .
              end
              
              gen origorder=_n
              bysort A (Year origorder): gen n=_n
              rangestat (sd) B, interval(n . 0) by(A Filter)
              sort origorder

              Comment

              Working...
              X