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.
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.);
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?
Subtraction.
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') ;
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) ;
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?
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) ;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.