Announcement

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

  • Sum of segmented responses

    Hi, I want to sum the segmented responses within each pid(id) separately.

    I am writing a paper and the variable is poverty duration, but since the poverty duration per person may be discontinuous, I want to get the segmented poverty duration, not the total poverty duration of a person.


    Thank you.


    .
    Click image for larger version

Name:	2024-03-18 오후 6.32.43.png
Views:	1
Size:	299.0 KB
ID:	1747033

  • #2
    Code:
    gen `c(obs_t)' obs_no = _n
    by pid (obs_no), sort: gen spell = sum(poverty != povery[_n-1])
    by pid spell (obs_no), sort: gen wanted = _N if poverty
    Note: because example data was not provided in a usable form, this code is not tested. It may contain typos or other errors. If you are not able to adapt this code to your actual data, when posting back, pleas be sure to use the -dataex- command so that usable example data is available for troubleshooting. If you are running version 18, 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.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Code:
      gen `c(obs_t)' obs_no = _n
      by pid (obs_no), sort: gen spell = sum(poverty != povery[_n-1])
      by pid spell (obs_no), sort: gen wanted = _N if poverty
      Note: because example data was not provided in a usable form, this code is not tested. It may contain typos or other errors. If you are not able to adapt this code to your actual data, when posting back, pleas be sure to use the -dataex- command so that usable example data is available for troubleshooting. If you are running version 18, 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.
      I've spent days trying to figure it out...
      Thank you so much for your help.

      You have been a great help in making my thesis go smoothly!!!
      Thanks again!

      Comment

      Working...
      X