Announcement

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

  • Problem with count variable generation

    Dear Statalist Users,

    Although I went through many related discussions and explanations about my question, nothing helped me. I will be thankful for your suggestions in the following problem.
    I am working with a Call Details Record (CDR) Dataset. In the data, for each caller id I observe the antenna used to make/receive call. I want to generate the variable for the number of times a certain antenna is used by a caller. What I am looking for is the following:



    Thanks in advance for your cooperation. Bests.


    S.
    Last edited by Sally Hansen; 27 Jan 2019, 16:36.

  • #2
    caller_id time antenna_id count_var
    1 t_11 50 2
    1 t_12 55 1
    1 t_13 50 2
    2 t_21 51 1
    2 t_22 52 1
    2 t_23 53 3
    2 t_24 53 3
    2 t_25 53 3
    3 t_31 55 2
    3 t_32 55 2
    3 t_33 50 1
    3 t_34 51 1
    3 t_35 52 1
    3 t_36 54 3
    3 t_37 54 3
    3 t_38 54 3

    Comment


    • #3
      you can try the following code:
      Code:
      bysort caller_id antenna_id: egen count=count( antenna_id )
      and for preserving original order of data, you can try:
      Code:
      gen id=_n
      bysort caller_id antenna_id: egen count=count( antenna_id )
      sort id
      Last edited by Chen Samulsion; 27 Jan 2019, 17:03.

      Comment


      • #4
        What Chen said.

        I would just clarify that Chen's second piece of code that preserves the sort is unnecessary. One can just use the syntax below.

        According to Nick Cox the following syntax is no longer documented, and it is no longer explained in the manuals that the following syntax is sort preserving:

        Code:
        .  egen count=count( antenna_id ), by(caller_id antenna_id)

        Comment


        • #5
          Joro Kolev Thank you for introducing this wonderful legacy.

          Comment


          • #6
            Dear Chen and Joro, thank you so much for your kind help and suggestions. Both codes (1 and 3) are giving the same outcome and they are exactly doing what I need to do. Bests

            S.

            Comment

            Working...
            X