BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASdevAnneMarie
Barite | Level 11

Hello Experts,

 

I would like to get the Year with Datepart function in proc sql, but my code doesn't work.

Could you help me, please :

proc sql;
	create table TEST as 
		select count(distinct(ev.d_effet)) as nombre_versement_15ans from psaeu11.db_evenement ev,
			psaeu11.dp_classe_evt classe
		where ev.no_police = 'AFFGV'
			and classe.b_ea =1  and classe.b_rachat = 1 
			and ev.is_classe_evt=classe.is_classe_evt and 
			year(datepart(y,ev.d_effet))>=year(datepart(y,today()))-15;
quit;

Thank you !

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The DATEPART() function just converts number of seconds (DATETIME value) into number of days (DATE value). Basically it is just dividing by 24*60*60.

 

If you want the YEAR from a DATE value you can use the YEAR() function.  

So if you want the YEAR from a DATETIME value you can just nest the two function calls.

 

select YEAR(DATEPART(datetime_var)) as year_var
...

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

The DATEPART() function has only one argument. You are using DATEPART() with two arguments, that won't work. DATEPART() also only works on data/time values, do your variables contain date/time values?

--
Paige Miller
Tom
Super User Tom
Super User

The DATEPART() function just converts number of seconds (DATETIME value) into number of days (DATE value). Basically it is just dividing by 24*60*60.

 

If you want the YEAR from a DATE value you can use the YEAR() function.  

So if you want the YEAR from a DATETIME value you can just nest the two function calls.

 

select YEAR(DATEPART(datetime_var)) as year_var
...
SASdevAnneMarie
Barite | Level 11
Thank you, Tom !

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 3713 views
  • 2 likes
  • 3 in conversation