Announcement

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

  • Associated variables

    Hi,

    I'm new to STATA - apologies if this query is too simple.

    I have a large dataset in which each individual has numerous blood results, such as creatinine. I can tell the sequence of the creatinine values by a) the fact they are numbered eg "creat1" "creat2" and b) an associated time variable ("hrs_trop1_creat1") which tells me how many days before or after the first troponin value, that creatinine value was recorded.

    I need to rename my creatinine values, such that creat0 is 0 hours from the first troponin, creat60 at 60 hours from first troponin, creat-24 at 24hours before the first troponin. Then I could get rid of the associated time variable.

    Could someone suggest how I could do this please? I can't work out how to link the creatinine result with the corresponding time.

    Thanks so much.

  • #2
    Jemima:
    welcome to this forum.
    As per FAQ, please provide an excerpt/example with your data via -dataex-. Thanks.
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      Thankyou for replying.

      Unfortunately I can't as the data is kept on an ICARE high performance environment and I'm not allowed to share it in any way. Which does make life tricky!

      Comment


      • #4
        the FAQ explains what to do in this situation

        Comment


        • #5
          This would seem to demonstrate code that does what you want. Note that "-" is not a valid character in a Stata variable name, so I substitute "_", and note also that I don't care to spend the time typing long variable names.
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float(id cr1 cr_h1 cr2 cr_h2 cr3 cr_h3)
          1001 100 -24 200  0 300 60
          1002  42   0  66 10   .  .
          end
          list, clean
          reshape long cr cr_h, i(id) j(j)
          drop if missing(cr_h)
          tostring cr_h, generate(sfx)
          replace sfx = subinstr(sfx,"-","_",1)
          list, clean
          drop j cr_h
          reshape wide cr, i(id) j(sfx) string
          list, clean
          Code:
          . * Example generated by -dataex-. For more info, type help dataex
          . clear
          
          . input float(id cr1 cr_h1 cr2 cr_h2 cr3 cr_h3)
          
                      id        cr1      cr_h1        cr2      cr_h2        cr3      cr_h3
            1. 1001 100 -24 200  0 300 60
            2. 1002  42   0  66 10   .  .
            3. end
          
          . list, clean
          
                   id   cr1   cr_h1   cr2   cr_h2   cr3   cr_h3  
            1.   1001   100     -24   200       0   300      60  
            2.   1002    42       0    66      10     .       .  
          
          . reshape long cr cr_h, i(id) j(j)
          (j = 1 2 3)
          
          Data                               Wide   ->   Long
          -----------------------------------------------------------------------------
          Number of observations                2   ->   6           
          Number of variables                   7   ->   4           
          j variable (3 values)                     ->   j
          xij variables:
                                      cr1 cr2 cr3   ->   cr
                                cr_h1 cr_h2 cr_h3   ->   cr_h
          -----------------------------------------------------------------------------
          
          . drop if missing(cr_h)
          (1 observation deleted)
          
          . tostring cr_h, generate(sfx)
          sfx generated as str3
          
          . replace sfx = subinstr(sfx,"-","_",1)
          (1 real change made)
          
          . list, clean
          
                   id   j    cr   cr_h   sfx  
            1.   1001   1   100    -24   _24  
            2.   1001   2   200      0     0  
            3.   1001   3   300     60    60  
            4.   1002   1    42      0     0  
            5.   1002   2    66     10    10  
          
          . drop j cr_h
          
          . reshape wide cr, i(id) j(sfx) string
          (j = 0 10 60 _24)
          
          Data                               Long   ->   Wide
          -----------------------------------------------------------------------------
          Number of observations                5   ->   2           
          Number of variables                   3   ->   5           
          j variable (4 values)               sfx   ->   (dropped)
          xij variables:
                                               cr   ->   cr0 cr10 ... cr_24
          -----------------------------------------------------------------------------
          
          . list, clean
          
                   id   cr0   cr10   cr60   cr_24  
            1.   1001   200      .    300     100  
            2.   1002    42     66      .       .  
          
          .
          Now, the next time you post, instead of leaving it to the reader to guess what your data is like and spend the time making up some invented data, you can yourself invent data similar to yours and present it in a usable form. In particular this is easy if you have access to Stata outside the environment, since you can (as I did) create data using Stata's Data Editor and then use the dataex command to present it in a usable format.

          The more you help other members understand your problem, the more likely other members are to help you solve your problem.

          Comment


          • #6
            Jemima:
            as Richard helpfully reminded, FAQ 12.2 recommends, in case of confidential datasets, to post via -dataex- an excerpt/example of a fake dataset that replicates the problem you are complaining about.
            For instance, you could keep the name of the variables you need to be advised about providing fake values for each observation.
            This way you would give intrtested listers an idea of what's the issue with your data without breaching any confidential agreement that you may have.
            Kind regards,
            Carlo
            (Stata 18.0 SE)

            Comment


            • #7
              Thank you all.

              Comment

              Working...
              X