Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: data management - days into weeks


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: Re: data management - days into weeks
Date   Wed, 7 Mar 2012 10:40:21 +0900

Maria Cecilia Vieira da Silva wrote:

In my data set I have "day", a variable that indicates the day of treatment of a
patient. Example:
 
Patient=1
 
Day=5
Outcome x1=6
Outcome x2=0.5
 
Day=10
Outcome x1=5
Outcome x2=0.6
 
I would like to create a variable "week", where week=1 if day<=7; week=2 if
7<day<=14 and so on.
Any suggestion about how to create "week" without my having to write the
conditions for each week of the year up to week 52?
The date functions do not help me because my data set does not contain 'dates'.

--------------------------------------------------------------------------------

Are you looking for something like the following?

generate int Week = ceil(Day / 7)

Its use is illustrated below so that you can check to make sure that it does
what you want.  It won't restart at Week 1 if your patient follow-up interval
exceeds a year (52 weeks), so you'll need to consider that.

Joseph Coveney

. input str6 patient_nr int Day double(outcome_1 outcome_2)

     patient~r       Day   outcome_1   outcome_2
  1. "01-001"  5 6 0.5
  2. "01-001" 10 5 0.6
  3. "01-001" 14 0 0.1
  4. "01-001" 7  1 0.3
  5. "01-001" 15 3 0.1
  6. end

. 
. sort patient_nr Day

. 
. generate int Week = ceil(Day / 7)

. 
. list, noobs sepby(patient_nr Week) abbreviate(20)

  +-------------------------------------------------+
  | patient_nr   Day   outcome_1   outcome_2   Week |
  |-------------------------------------------------|
  |     01-001     5           6          .5      1 |
  |     01-001     7           1          .3      1 |
  |-------------------------------------------------|
  |     01-001    10           5          .6      2 |
  |     01-001    14           0          .1      2 |
  |-------------------------------------------------|
  |     01-001    15           3          .1      3 |
  +-------------------------------------------------+


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index