SAS dates conversion

Reply
Contributor
Posts: 25

SAS dates conversion

I have dates of events in SAS form, just number of days, but I would like to convert them to DDMMYYY and then extract the year so I can make a variable called "year of event". Is there an easy way to do this? Please provide code if possible.

Super User
Super User
Posts: 7,039

Re: SAS dates conversion

If you have a numeric variable that looks like a date when printed with a date format then you can use the YEAR() function to extract the year.

year_of_event = year(date_of_event) ;

Now if your date variable is instead a character string that just looks like a date then you can first convert it to a date using the proper informat. For example if you values look like 13JUL2014 then you would use code like this:


year_of_event = year(input(date_of_event,date9.);

Contributor
Posts: 25

Re: SAS dates conversion

that was very helpful! Unfortunately, my dates are actually from the beginning of the study period (1998), is there anyway to make the "year" variable count from this date instead of the SAS default date?

Super User
Super User
Posts: 7,039

Re: SAS dates conversion

Subtraction.

Super User
Super User
Posts: 7,039

Re: SAS dates conversion

If the beginning is a constant then just subtract it. If it exists in another variable then reference the other variable.

For example to count beginning with 1 you could use:

study_year = year(event_date) - 1988 + 1 ;

study_year = year(event_data) - year(study_start_date) + 1;

If you want to treat the interval from 01AUG1998 to 01JAN1990 as less than two years then you might want to use the INTCK() function.

study_year = intck('year',study_start_date,event_date,'C') ;

Super User
Super User
Posts: 7,039

Re: SAS dates conversion

Do you mean your dates are the number of days since the beginning of the study? Then add them to the study start date and take the year to get the calendar year.

year = year(study_day + study_start_date -1) ;

Contributor
Posts: 25

Re: SAS dates conversion

I have date of event in numeric form (i.e. 915) and those are days from the study start period 1998. I want to determine what year the event occurred in. Should I add 915 days to the beginning of the study period to get year of event?

Respected Advisor
Posts: 4,173

Re: SAS dates conversion

Answer as already provided by in your other post asking the same question 

Super User
Super User
Posts: 7,039

Re: SAS dates conversion

So if the study start date is a constant then just use a date literal for the calculation.

Assuming study day number 1 is 01JAN1988 then you would use.

year = year(study_day + "01JAN1988"d -1) ;

Ask a Question
Discussion stats
  • 8 replies
  • 337 views
  • 0 likes
  • 3 in conversation