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

  • longitudinal dataset (stacked) counting months since

    Hello, I am hoping to find help with what is probably a simple problem for coders.
    I have a data set that shows a month by month account of a single "person" (ID), a date variable showing each month he is in the data set and a variable that indicates if he is in his trade on that date.
    I am trying to count the number of months since last in trade for that specific person (ID) when that person is not in trade. Last in trade on months he is IN trade should be 0.
    An example can be seen below where I manually counted these months.

    Thank you,

    Jean-Michel Galarneau
    Attached Files

  • #2
    Welcome to Statalist.

    I believe the following untested code will point you in a helpful direction.
    generate date_mon = mofd(Date)
    format date_mon %tm
    sort ID Date
    generate wanted = .
    by ID (Date): replace wanted = 0 if trade==1
    by ID (Date): replace wanted = wanted[_n-1]+(date_month-date_month[_n-1]) if trade==0
    Allow me to offer some advice to improve future posts. I very much wanted to test the code I wrote, but Stata will not read in your screenshot of your example data, and "I'm eager to type data from a picture into Stata" said nobody, ever.

    Please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.


    • #3
      That works well.

      Thank you for your help.