Announcement

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

  • Number of observations per participant

    Hello, I am very new to Stata and need help with some simple commands.

    I have got data from participants who have been measured repeatedly across different visits to see how variables change with ageing. I would like to calculate how many times each participant has been measured.

    The data are in long form. Is there a way to create a new variable that assigns an observation number (e.g. first observation, second observation, third observation)? And calcuate the mean number of observations per participant?

    Thanks

  • #2
    Code:
    by participant_id (time_variable), sort: gen seq = _n
    by participant_id (time_variable): gen observation_count = _N
    egen participant_tag = tag(participant_id)
    summ observation_count if participant_tag
    As you do not show example data, I have written code for an imaginary data set that I hope resembles yours, but may or may not do so. At the least you will need to replace parts of this code with the actual names of variables in your data. At worst, there will be no such correspondence because your data is differently organized, and we will have both wasted our time.

    In the future, when seeking help with code, to assure that the response you get is helpful, always show example data, and always use the -dataex- command to do that. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thank you, that code works for my data

      Comment

      Working...
      X