BookmarkSubscribeRSS Feed
Mdormond
Calcite | Level 5

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.

8 REPLIES 8
Tom
Super User Tom
Super User

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.);

Mdormond
Calcite | Level 5

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?

Tom
Super User Tom
Super User

Subtraction.

Tom
Super User Tom
Super User

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') ;

Tom
Super User Tom
Super User

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) ;

Mdormond
Calcite | Level 5

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?

Patrick
Opal | Level 21

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

Tom
Super User Tom
Super User

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) ;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 821 views
  • 0 likes
  • 3 in conversation