How to calculate intervals between observations

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

How to calculate intervals between observations

Dear SAS Community,

I'm trying to calculate time intervals for multiple observations per subject. My dataset records survey results for lifetime smoking behavior by age.  Here is an example of what my data looks like:

Person_ID     Age   

1                    18     * Individual started smoking

1                    25     * Changed smoking behavior

1                    40     * Stopped smoking

1                    45     * Current age

Individuals have different age values depending on their lifetime smoking behavior.  For some individuals, age of quitting and current age are the same.

I would like to generate a new variable, Time, for duration of smoking behavior in years:

Person_ID     Age     Time   

1                    18     7

1                    25     15

1                    40     5     

1                    45     0

I've considered using Intck to calculate these intervals but I'm not sure how to compare between observations for the entire dataset.

Can anyone please suggest how I can generate my new Time variable? Thank you very much.    


Accepted Solutions
Solution
‎06-19-2013 08:47 AM
Super User
Posts: 5,082

Re: How to calculate intervals between observations

A few similar approaches are possible.  This way is probably the easiest to understand:

data want;

   set have;

   by person_id;

   if last.person_id then time=0;

   else do;

      next_record = _n_ + 1;

      set have (keep=age rename=(age=next_age)) point=next_record;

      time = next_age - age;

   end;

   drop next_record next_age;

run;

Good luck.

View solution in original post


All Replies
Super User
Posts: 5,256

Re: How to calculate intervals between observations

The calculation itself seems simple, just a subtraction between ages?

One way to do this is to resort the table no person-ID and descending Age.

Then use a new retained variable to store the previous observations age 8for the same individual (for this, use first. and last. processing).

Other favor the lag() function, which also can be used.

Data never sleeps
Contributor
Posts: 29

Re: How to calculate intervals between observations

TJ: Hope it Helps...


data one;
input Person_ID Age ;
observ =_N_;
cards;
1 18    * Individual started smoking*/
1 25     * Changed smoking behavior
1 40     * Stopped smoking
1 45     * Current age
;run;

proc sort data=one; by descending observ; run;

data two;
     set one;
     x=lag(Age);
run;

proc sort data=two; by observ; run;
data time(drop = observ x) ;
set two;
time = x-age;
run;

Solution
‎06-19-2013 08:47 AM
Super User
Posts: 5,082

Re: How to calculate intervals between observations

A few similar approaches are possible.  This way is probably the easiest to understand:

data want;

   set have;

   by person_id;

   if last.person_id then time=0;

   else do;

      next_record = _n_ + 1;

      set have (keep=age rename=(age=next_age)) point=next_record;

      time = next_age - age;

   end;

   drop next_record next_age;

run;

Good luck.

Contributor
Posts: 44

Re: How to calculate intervals between observations

Thank you very much! Your code works great and it's something I'll keep in my back pocket for the next time. 

Contributor
Posts: 44

Re: How to calculate intervals between observations

Thank you for your suggestions!

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 226 views
  • 6 likes
  • 4 in conversation