Solved
Contributor
Posts: 46

# Determining cycle start and end dates

Hello All,

I need so help in generating start and end date of continuous cycles.

I have a treatment  that is adminsistered twice a week. Each 4 weeks periods is considered a cycle. The maximum number of cycle allowed is 7.

I have a start date for the first day of treatement and in some cases an end date but not always.

I need to assign the start and end date of each cycle .

I am using the intnx function

trt01edt=INTNX("day",trtsdt, 28,"B");

trt02edt=INTNX("day",trtsdt, 29,"B");

How can use a loop to  to get the start and end dates calculated auttomatically instead of having to enter each interval?

Thank You.

KC

Accepted Solutions
Solution
‎05-26-2016 08:59 AM
Super User
Posts: 13,563

## Re: Determining cycle start and end dates

See if this comes close:

``````data want;
set have;
array s TrtSt01-TrtSt07;
array e TrtEn01-TrtEn07;
format TrtSt01-TrtSt07 TrtEn01-TrtEn07 mmddyy10.;
do i=1 to dim(s);
s[i] = intnx('day',reference_startdate,28*i -28 +(1*(i>1)));
e[i] = intnx('day',reference_startdate,28*i);
end;
drop i;
run;``````

All Replies
Super User
Posts: 13,563

## Re: Determining cycle start and end dates

To help understand you needs it would help if you could provide some example starting data and what the desired output would be for that data.

If your variable trt01edt is supposed to mean something like treatment of cycle you might consider renaming to to TrtEdt01 as there a serveral processing approaches that are easier to maintain using a numerical suffix indicating sequence.

Also you show a 29 in your second line of code. Is there a particular reason?

Contributor
Posts: 46

## Re: Determining cycle start and end dates

[ Edited ]

This is for a treatment.

I can rename the variables.

Everyone has a start date, Some subject have an end date (treatment ended) while toehr do not becasue they are still on treatment.

The treatment is adminsitered twice a week.

The weekly dates do not matter.

in order to determien the cycle i was doing:

trtst01=reference_startdate;

trten01=intnx("day",reference_startdate,28,"b");

trtst02=intnx("day",reference_startdate,29,"b");

trten02=intnx("day",reference_startdate,56,"b");

etc...

Instead of having to type in every segment i would like to get it done iteratively.

Solution
‎05-26-2016 08:59 AM
Super User
Posts: 13,563

## Re: Determining cycle start and end dates

See if this comes close:

``````data want;
set have;
array s TrtSt01-TrtSt07;
array e TrtEn01-TrtEn07;
format TrtSt01-TrtSt07 TrtEn01-TrtEn07 mmddyy10.;
do i=1 to dim(s);
s[i] = intnx('day',reference_startdate,28*i -28 +(1*(i>1)));
e[i] = intnx('day',reference_startdate,28*i);
end;
drop i;
run;``````
Contributor
Posts: 46

## Re: Determining cycle start and end dates

Thank you Ballard,

I will try it but I was wondering if I wnated to use to retain statement or the lag function to use date that has just been determined

ex trtst02= trten01+1?

Super User
Posts: 13,563

## Re: Determining cycle start and end dates

Retain is for across record values, so I doubt if that would be helpful if I undestrand that you are adding 14 variables to a single record.

Similarly LAG looks for values from a previous records.

You could refernce a previous value of the end but you get into bookkeeping:

do i=1 to 7;

if i= 1 then s[i]=reference_start;

else s[i] = e[i-1];

...

end;

But if your relationships are strictly linear it is good to know how to work with them. There are some common things that happen with loops, one of them is nesting one or more within another and keeping track of lots of boundary exceptions (if i=1 stuff) can get a bit twitchy especially when you have them at both the start and end of a period.

With a fixed interval of 28 I might have gone into MOD function for increments.

Contributor
Posts: 46

## Re: Determining cycle start and end dates

Hello,

Using the sugestion of the array  I was able to determine the start and end date of cycle but these intervals do not exactly correpondd to the dates in my data due to a missed dose issue and also because each adminsistration has a window  of +/- 3 dates.

I have a ref start date, which is the very first time the user took the medecine.I have subsequent adminsistration dates. From these I calculate the relative exposure day.

data have

subjid    ref_stdt             exposdt               expoday   (exposdt-ref_stdt +1)

1           1jan2016           1jan2016                  1

1           1jan2016            5jan2016                 5

2           1feb2016            1 feb2016                1

2           1feb2016            15feb2016             15

2            1feb2016            25feb2016            25

2            1feb2016            28feb2016            28

2            1feb2016            29feb2016            29

3            1feb2016           1feb2016                 1

3            1feb2016           25feb2016             25

3            1feb2016          3march2016           32

3             1feb2016         25march2016         55

3              1feb2016       29march2016          59

subject 1 has only one cycle which starts on 1 jan2016 and ends on J5 jan2016

subject 2 has 2 cycles, cycle 1 starts on 1feb2016 and ends on 28feb2016. cycle 2 starts on 29 feb2016 and has no end date

subject 3 has 3 cycle , cycle 1 starts on 1feb2016 and ends on 25feb2016, cycle 2 start on 3march 2016 and ends on march 25 2016, cycle 3 starts on march 29 2016. Each of these subject ends the cycles on differnet dates, expoday (missed does, travels, etcc..).

I did a transpose to get the day by subject and then according to the days, tried  assign the cycle start and end dates: per the exposure day but this leads to incorrect cycle starts and end dates.

e.g  exposure day 25 in case of subject 2 and 3 corresponds to the end of cycle 1 .

Cycle 2 for subject 2 starts on day 28 while cycle 2 for subject 3 starts on day 32.

subjid    eday1       eday5     eday15    eday25      eday28      eday32    eday55     eday59

1            1               1

2            1                                1                1            1

3            1                                                  1                                   1            1              1

I cannot find a way to to assign the ocrrect dates automatically without a lenght if/else statement.

🔒 This topic is solved and locked.