Announcement

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

  • cumulative numbering within subgroup

    Hello,

    I have data wherein patients have multiple assessment types. I would like to create a new variable that numbers (1,2,3 etc) for the subgroup of Assessment.
    I've tried the bysort command that i've seen in previous posts: bysort Assessment id: gen newvar=(_n)

    but this does not give me the desired outcome.

    This is what I'm trying to achieve:

    id. assessment. newvar
    1. SOC 1
    1. dischargetype1. 2
    1 dischargetype2. 3
    2. SOC 1
    2. dischargetype3. 2
    3. dischargetype1. 1
    3. SOC 2
    3. dischargetype2. 3
    4. SOC 1

    Thank you!

  • #2
    bysort id: gen wanted = _n
    Last edited by Øyvind Snilsberg; 07 Feb 2022, 12:31.

    Comment


    • #3
      While I agree with #2 as quite possibly what you want, is there a time variable that you can make explicit? Otherwise put, what is the rule that allows you to identify 1, 2, 3 and so on?
      Last edited by Nick Cox; 07 Feb 2022, 13:24.

      Comment


      • #4
        Thank you! Yes, the time variable is ‘assessment date’.

        Comment


        • #5
          Good; then safer and more explicit code is more like

          Code:
          bysort id (assessment_date) : gen wanted = _n

          Comment


          • #6
            This worked perfectly, thank you very much!

            Comment

            Working...
            X