## Re: calculate a change within a specific interval date.

Solved
Frequent Contributor
Posts: 84

# Re: calculate a change within a specific interval date.

Dear SAS Help,

In the data below, I want to calculate cd4 changes within a specific interval date '12-month' from the first test.

Any help, I much appreciate. Thanks in advance.

Ps

Obs id CD4COUNT  CD4DATE   cd4  cd4date   last_cd4 cd4date 12month   cd4

1 20     332   29MAY2006 332  29MAY2006      6 03NOV2009 29MAY2007    326

2 20     267   12JUN2006 332  29MAY2006      6 03NOV2009 29MAY2007    326

3 20     207   05DEC2006 332  29MAY2006      6 03NOV2009 29MAY2007    326

4 20      71   20NOV2007 332  29MAY2006      6 03NOV2009 29MAY2007    326

5 20      15   24JUN2008 332  29MAY2006      6 03NOV2009 29MAY2007    326

6 20       8   07AUG2008 332  29MAY2006      6 03NOV2009 29MAY2007    326

7 20       3   02JUN2009 332  29MAY2006      6 03NOV2009 29MAY2007    326

8 20       6   03NOV2009 332  29MAY2006      6 03NOV2009 29MAY2007    326

9 40      20   30JAN2003 20  30JAN2003     326 14DEC2010 30JAN2004   -306

10 40      10   13MAR2003 20  30JAN2003     326 14DEC2010 30JAN2004   -306

11 40     300   08MAY2003 20  30JAN2003     326 14DEC2010 30JAN2004   -306

12 40     239  19JUN2003  20  30JAN2003     326 14DEC2010 30JAN2004   -306

Accepted Solutions
Solution
‎09-30-2012 08:47 PM
Posts: 5,540

## Re: calculate a change within a specific interval date.

Hi! Try this way :

proc sort data=have; by id CD4DATE; run;

data want;
do until(last.id);
set have; by id;
if first.id then do;
firstDate = CD4DATE;
firstCD4 = CD4COUNT;
end;
else do;
delay = abs(intck("DAY", firstDate, CD4DATE) - 365);
minDelay = min(delay, minDelay);
if minDelay = delay then do;
yearCD4 = CD4COUNT;
lastDate = CD4DATE;
end;
end;
end;
format firstDate lastDate date9.;
keep id firstDate firstCD4 yearCD4 lastDate;
run;

proc print; run;

PG

PG

All Replies
PROC Star
Posts: 8,167

## Re: calculate a change within a specific interval date.

Based on your example, what do you want the resulting dataset to look like?

Frequent Contributor
Posts: 84

## Re: calculate a change within a specific interval date.

Dear Sir,

I am relatively new with longitudinal analysis, but this is part of study. I want the resulting in long format as I will study change in cd4 in relationship with initial antiretroviral class.

Sincerely,

Ps

PROC Star
Posts: 8,167

## Re: calculate a change within a specific interval date.

Understood, but for any of us to help, we have to see want you want to achieve.  It would also be useful to see what you have already tried to accompomplish what you want.

Frequent Contributor
Posts: 84

## Re: calculate a change within a specific interval date.

Dear Sir,

For instance, id 20, I want to calculate a difference between cd4=332 (first date) and cd4=71 (20Nov2007) in which this date is the nearest one year after the first test.

Also, id 40, a difference between cd4=20 (30Jan2003) and cd4=239 (19Jun2003) that the date is nearest one year after the first test.

Thanks.

Ps

Solution
‎09-30-2012 08:47 PM
Posts: 5,540

## Re: calculate a change within a specific interval date.

Hi! Try this way :

proc sort data=have; by id CD4DATE; run;

data want;
do until(last.id);
set have; by id;
if first.id then do;
firstDate = CD4DATE;
firstCD4 = CD4COUNT;
end;
else do;
delay = abs(intck("DAY", firstDate, CD4DATE) - 365);
minDelay = min(delay, minDelay);
if minDelay = delay then do;
yearCD4 = CD4COUNT;
lastDate = CD4DATE;
end;
end;
end;
format firstDate lastDate date9.;
keep id firstDate firstCD4 yearCD4 lastDate;
run;

proc print; run;

PG

PG
Frequent Contributor
Posts: 84

## Re: calculate a change within a specific interval date.

Dear Sir,

I greatly appreciate and thank you very much for your time in helping me this task. I ran the analysis and checked results that are exactly I am looking for.

Again thanks,

Sincerely,

Ps

Super Contributor
Posts: 1,636

## Re: calculate a change within a specific interval date.

Please mark your question as answered and gave PG and Art credits for helping you. If you don't know how to mark your question as answered please ask. Thank you!

Frequent Contributor
Posts: 84

## Re: calculate a change within a specific interval date.

Dear Linlin,

I appreciate your friendly reminding me. Please tell me as I am relative new for the community. I am not sure where I can give a mark.

I look forward to hearing from you soon.

Ps

Frequent Contributor
Posts: 84

## Re: calculate a change within a specific interval date.

Hi Linlin,

I just clicked correct answer. Am I doing the right way?

Ps

Super Contributor
Posts: 1,636

## Re: calculate a change within a specific interval date.

Yes!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 10 replies
• 457 views
• 9 likes
• 4 in conversation