DATA Step, Macro, Functions and more

Working with Dates

Reply
Regular Contributor
Posts: 194

Working with Dates

I need to determine if each person received a shot by the time they're  certain age and also if they've received it by the time they enter a program (specified calendar date)

What do you suggest I do?
I've programs left by someone else which I've tried modifiying and they used months.

The birthdate and shot dates are 2 digit month, 2 digit day, and 2 digit year. So, we can convert to a sasdate using  birthdate=mdy(month, day, year) and shotdate=mdy(month, day, year).

Question 1:

How do I determine if they've received a shot by up to 10 days before age 4 years?  Using the old code which uses months,  i can multiply 48*30.4375 to get age 4 years.  Does that give me how old the child is in sasdate in months? If so, how do I get a sasdate for the age in months including a 10 day grace period?  Going even further, how do I get a sasdate in age in years?  is 48*30.4375 a sasdate for age 48 months and for 4 years or is it for only 48 months and there's a different sasdate for 4 years?

Question 2:

If I want to know  if they've received the shot not only by age 4 but also before a certain date after that, say by the time they enter a program

Do I convert the date they enter the program into a sasdate too ((enterdate=mdy(10,10,2010) for example.).  And, if the sasdate number they received the shot is greater than the sasdate for age 4 years (48*30.4375) and less than the program entry date, they've fulfilled the requirement?

So, it'll be:  if   48*30.4375 < shotdate <enterdate then fulfilledrequirement=1;

Thanks very much!

Super User
Posts: 19,878

Re: Working with Dates

2 digit years? Do you only have people born after 2000, otherwise that could be an issue.

I wouldn't recommend using the month/day approximations when you're checking for a 10 day interval.

SAS Dates are really numbers, the number of days since January 1, 1960 and you can subtract/add as required.

So create 3 dates using mdy function as indicated. Then use the intnx functions to increment dates and compare dates. Example for A is below.

shot_date_sas

birth_date_sas

program_start_sas

Then you can simplify the calculation as follows. You may need to modify the <= signs to get the comparisons you want.

*Calculate age at which a person turns 4;

age4_sas=intnx('year', birth_date_sas, 4, 's');

*calculate if shot by enter date;

if age4_sas-shot_date_sas>=10 then shot_by_4=1;

*if shot 10 days before 4 and program requirement;

if shot_by_4=1 and shot_date_sas<=program_start_sas then fulfilled_requirement=1;

Regular Contributor
Posts: 194

Re: Working with Dates

Yes, birthdates are after 2000.  Thank you very much Smiley Happy

What does the 's' mean?    I looked it up, and ti sames it is alignment but I don't understand: SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Super User
Posts: 19,878

Re: Working with Dates

It increments the date 4 years in the future, but at the same date. You can use end/beginning to get the start/end of the year if you needed that type of calculation.

So Jan15, 2000 with the formula above would be Jan 15, 2004, so year is incremented by 4 and day/month stay the same.  You use this formula rather than adding 365 days because of leap years.  It "corrects" for Leap years, but you should verify the logic is what you want.

Regular Contributor
Posts: 194

Re: Working with Dates

Thank you!

Respected Advisor
Posts: 4,173

Re: Working with Dates

For Q1: Look up function YRDIF(). And for the grace period simply deduct 10 days from toda().

  age=YRDIF(DoB,today(),'age');

It's a good idea to always use SAS dates. Using SAS dates you can take advantage of all the calendar functions like intnx(), intck(), yrdif() and so on.

Regular Contributor
Posts: 194

Re: Working with Dates

Thank you!

Ask a Question
Discussion stats
  • 6 replies
  • 285 views
  • 6 likes
  • 3 in conversation