Announcement

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

  • List of values of variable by patient-ID

    Dear stata-community,
    I would like to have a list of how many patients that has a certain age, but only counting each patient-ID once.

    So I need a list something like
    age_2012 age_2019
    19 freq 2 18 freq 1
    28 freq 1 26 freq 2
    32 freq 4 39 freq 4

    The reason I need for both 2012 and 2019 is that some of the patients from 2012 has left, and new patients have come into my data-material.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte pasID str2(age_2012 age_2019)
    1 "32" "39"
    1 "32" "39"
    2 "28" "."
    2 "28" "."
    2 "28" "."
    3 "19" "26"
    3 "19" "26"
    3 "19" "26"
    3 "19" "26"
    4 "32" "39"
    4 "32" "39"
    5 "19" "26"
    6 "32" "39"
    6 "32" "39"
    7 "32" "39"
    8 "."  "18"
    8 "."  "18"
    end
    I also have the variables "birthyear" and "year".

    Thank you, Cathrine
    Last edited by Cathrine Christiansen; 12 Jan 2022, 03:15.

  • #2
    See

    Code:
    help contract
    help egen

    Code:
    preserve
    egen tag= tag(pasID)
    contract age_2012 age_2019 if tag
    l
    restore
    Res.:

    Code:
    . l
    
         +-----------------------------+
         | age_2012   age_2019   _freq |
         |-----------------------------|
      1. |        .         18       1 |
      2. |       19         26       2 |
      3. |       28          .       1 |
      4. |       32         39       4 |
         +-----------------------------+
    Last edited by Andrew Musau; 12 Jan 2022, 03:08.

    Comment


    • #3
      Note also

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte pasID str2(age_2012 age_2019)
      1 "32" "39"
      1 "32" "39"
      2 "28" "."
      2 "28" "."
      2 "28" "."
      3 "19" "26"
      3 "19" "26"
      3 "19" "26"
      3 "19" "26"
      4 "32" "39"
      4 "32" "39"
      5 "19" "26"
      6 "32" "39"
      6 "32" "39"
      7 "32" "39"
      8 "."  "18"
      8 "."  "18"
      end
      
      groups age_*
      
        +---------------------------------------+
        | age_2012   age_2019   Freq.   Percent |
        |---------------------------------------|
        |        .         18       2     11.76 |
        |       19         26       5     29.41 |
        |       28          .       3     17.65 |
        |       32         39       7     41.18 |
        +---------------------------------------+
      Naturally you can apply tag() first as well.

      groups is now from the Stata Journal and must be installed before it can be used. As I used a simple English word for the command name, the best search is an otherwise unpredictable


      Code:
      . search st0496, entry
      
      Search of official help files, FAQs, Examples, and Stata Journals
      
      SJ-18-1 st0496_1  . . . . . . . . . . . . . . . . . Software update for groups
              (help groups if installed)  . . . . . . . . . . . . . . . .  N. J. Cox
              Q1/18   SJ 18(1):291
              groups exited with an error message if weights were specified;
              this has been corrected
      
      SJ-17-3 st0496  . . . . .  Speaking Stata: Tables as lists: The groups command
              (help groups if installed)  . . . . . . . . . . . . . . . .  N. J. Cox
              Q3/17   SJ 17(3):760--773
              presents command for listing group frequencies and percents and
              cumulations thereof; for various subsetting and ordering by
              frequencies, percents, and so on; for reordering of columns;
              and for saving tabulated data to new datasets
      For a fairly quick overview see https://www.statalist.org/forums/for...updated-on-ssc

      Comment


      • #4
        Thank you!

        Comment

        Working...
        X