Announcement

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

  • Frequency of answers on an unbalanced panel data

    Hi all,

    I am working on an unbalanced panel data and I would like to create a variable called "numanswer" that gives the number of answers per ids.

    Here is an example of what would look like the variable "numanswer" on an unbalanced panel data.

    input byte id int year byte wage numanswer
    1 2012 35 4
    1 2013 30 4
    1 2014 30 4
    1 2015 34 4
    2 2013 35 2
    2 2014 50 2
    3 2012 50 3
    3 2014 40 3
    3 2015 40 3
    4 2015 30 1
    5 2013 40 3
    5 2014 40 3
    5 2015 50 3
    end

    Does someone know how to create such a variable ?

    Thanks a lot !

    Regards,

    Al

  • #2
    Al:
    what follows can do the trick:
    Code:
    bysort id: egen flag=count(wage) if wage!=.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      As count() counts non-missing values of its argument, this version is likely to be more convenient


      Code:
       
       bysort id: egen flag=count(wage)

      Comment


      • #4
        Nick is obviously correct.
        Being recently bitten by miscoded missing values, I see them lurking everywhere!
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Dear Carlo, Nick

          Thank you very much for your answer !

          Kind regards,

          Al

          Comment


          • #6
            This works perfectly fine. Thank you very much ! Is it actually possible, for each id, to only keep the first value for the flag variable and replace the others by "." as in the following example ?

            input byte id int year byte wage float flag
            1 2012 35 4
            1 2013 30 .
            1 2014 30 .
            1 2015 34 .
            2 2013 35 2
            2 2014 50 .
            3 2012 50 3
            3 2014 40 .
            3 2015 40 .
            4 2015 30 1
            5 2013 40 3
            5 2014 40 .
            5 2015 50 .
            end


            Thanks again !

            Regards,

            Al

            Comment


            • #7
              Code:
              bysort id (year) : gen flag = _N if _n == 1
              Also, look at the tag() function in the help for egen.

              Comment


              • #8
                Thanks a lot !

                Comment

                Working...
                X