BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Corinthian94
Obsidian | Level 7
Hi all,
 
I am working on manipulating ages based on how long people took between interviews in days, and this is the code I have created for it. I am wondering if there is a way to make this shorter? I was thinking of using a macro but am not sure that is the best way, so wanted to reach out. Appreciate your help with this, as well as any thoughts!
 
if interview_dif ge 25 and interview_dif le 45 then do; *Included this because some people take surveys 30 days after last one, since windows are 30 days apart;
 
if vrid_3mo ne "" then interview_age = ((age*12) + 1);
if vrid_6mo ne "" then interview_age = ((age*12) + 4);
if vrid_9mo ne "" then interview_age = ((age*12) + 7);
 
end;
 
if interview_dif gt 45 and interview_dif le 75 then do;
 
if vrid_3mo ne "" then interview_age = ((age*12) + 2);
if vrid_6mo ne "" then interview_age = ((age*12) + 5);
if vrid_9mo ne "" then interview_age = ((age*12) + 8);
 
end;
 
if interview_dif gt 75 and interview_dif le 105 then do;
 
if vrid_3mo ne "" then interview_age = ((age*12) + 3);
if vrid_6mo ne "" then interview_age = ((age*12) + 6);
if vrid_9mo ne "" then interview_age = ((age*12) + 9);
 
end;
 
if interview_dif gt 105 and interview_dif le 135 then do;
 
if vrid_3mo ne "" then interview_age = ((age*12) + 4);
if vrid_6mo ne "" then interview_age = ((age*12) + 7);
if vrid_9mo ne "" then interview_age = ((age*12) + 10);
 
end;
 
if interview_dif gt 135 and interview_dif le 165 then do; 
 
if vrid_3mo ne "" then interview_age = ((age*12) + 5);
if vrid_6mo ne "" then interview_age = ((age*12) + 8);
if vrid_9mo ne "" then interview_age = ((age*12) + 11);
 
end;
 
if interview_dif gt 165 and interview_dif le 195 then do; 
 
if vrid_3mo ne "" then interview_age = ((age*12) + 6);
if vrid_6mo ne "" then interview_age = ((age*12) + 9);
if vrid_9mo ne "" then interview_age = ((age*12) + 12);
 
end;
 
if interview_dif gt 195 and interview_dif le 225 then do; 
 
if vrid_3mo ne "" then interview_age = ((age*12) + 7);
if vrid_6mo ne "" then interview_age = ((age*12) + 10);
if vrid_9mo ne "" then interview_age = ((age*12) + 13);
 
end;
 
if interview_dif gt 225 and interview_dif le 255 then do; 
 
if vrid_3mo ne "" then interview_age = ((age*12) + 8);
if vrid_6mo ne "" then interview_age = ((age*12) + 11);
if vrid_9mo ne "" then interview_age = ((age*12) + 14);
 
end;
 
if interview_dif gt 255 and interview_dif le 285 then do; 
 
if vrid_3mo ne "" then interview_age = ((age*12) + 9);
if vrid_6mo ne "" then interview_age = ((age*12) + 12);
if vrid_9mo ne "" then interview_age = ((age*12) + 15);
 
end;
 
if interview_dif gt 285 and interview_dif le 315 then do; 
 
if vrid_3mo ne "" then interview_age = ((age*12) + 10);
if vrid_6mo ne "" then interview_age = ((age*12) + 13);
if vrid_9mo ne "" then interview_age = ((age*12) + 16);
 
end;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

INTNX moves by intervals.

INTCK counts interval boundaries crossed.

 

If you want to adjust by months you could use INTCK with MONTH interval.

interview_age = baseline_age*12 + intck('month',baseline_date,interview_date,'cont');

Or perhaps just divide the number of days by the average number of days in a month.

interview_age = baseline_age*12 + round((interview_date-baseline_date)/(365.25/12));

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

I am assuming you have AGE at some baseline date instead of actual DATE OF BIRTH.  Is that correct?

 

Looks like you are storing AGE in months since you multiply it by 12?  Normally people store AGE in years.  How are you storing AGE?

 

Do you have the INTERVIEW_AGE for each follow-up interview?

 

If you  have the BASELINE_AGE and BASELINE_DATE then you could get pretty close the calculating an INTERVIEW_AGE by doing something like this:

 

Convert BASELINE_AGE and BASELINE_DATE to a date, perhaps the beginning of the month that is BASELINE_AGE years before BASELINE_DATE. 

 

Then calculate the interview age from that date to the current interview date.

INTERVIEW_AGE = intck('year',intnx('year',baseline_date,-baseline_age),interview_date);
Corinthian94
Obsidian | Level 7

Hi Tom,

 

Thanks for your response! Yes, we only have age at Baseline, not date of birth. We are capturing age in years but then have to convert to months for our purposes, which is why they are multiplied by 12.

 

We do have interview age for each follow-up interview, but these are sometimes inconsistent due to human error. I have gone through and fixed it the best that I can, by basically making all ages the same across the interviews of each participant and trying to add on months from there.

 

I see what you are saying about turning age and date both into dates! How would you go about changing age to a date that is comparable though? The way I am thinking about it, if I convert age to a date the differences between age and interview_date as they correspond to date would not be consistent. 

 

I am looking into the code you posted as well! Intck is new to me, but seems like it is calculating the difference in years between the two? Or do I have that wrong?

 

Thanks again for your help!

 

Tom
Super User Tom
Super User

INTNX moves by intervals.

INTCK counts interval boundaries crossed.

 

If you want to adjust by months you could use INTCK with MONTH interval.

interview_age = baseline_age*12 + intck('month',baseline_date,interview_date,'cont');

Or perhaps just divide the number of days by the average number of days in a month.

interview_age = baseline_age*12 + round((interview_date-baseline_date)/(365.25/12));
Corinthian94
Obsidian | Level 7

That makes sense, thanks for your help!

ChrisNZ
Tourmaline | Level 20

Something like this?

 

*Test needed because some people take surveys 30 days after last one, since windows are 30 days apart;
if       25 <=INTERVIEW_DIF <=  45 then CORRECTION=1; 
else if  45 < INTERVIEW_DIF <=  75 then CORRECTION=2; 
else if  75 < INTERVIEW_DIF <= 105 then CORRECTION=3; 
etc..

if CORRECTION then do;  
  if      VRID_3MO ne "" then INTERVIEW_AGE = (AGE*12 + CORRECTION    );
  else if VRID_6MO ne "" then INTERVIEW_AGE = (AGE*12 + CORRECTION + 3);
  else if VRID_9MO ne "" then INTERVIEW_AGE = (AGE*12 + CORRECTION + 6);
end;
 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 648 views
  • 1 like
  • 3 in conversation