07-13-2014 08:50 AM
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.
07-13-2014 10:51 AM
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.);
07-13-2014 12:19 PM
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?
07-13-2014 12:34 PM
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') ;
07-13-2014 01:05 PM
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) ;
07-13-2014 01:34 PM
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?
07-13-2014 06:26 PM
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) ;