Help using Base SAS procedures

DATETIME22.3 to year

Reply
Occasional Contributor NOA
Occasional Contributor
Posts: 8

DATETIME22.3 to year

I am obviously a self taught SAS user and a newbie. Any sort of help is appreciated.

I have a variable called EVENTDATE and according to PROC CONTENTS the format for this variable is DATETIME22.3. I would like to run an analysis using only the year part --not interested about the month, day or time. Can anyone tell me the sas code so I can extract the year so I can run a basic analysis?

Thank you.


Super User
Super User
Posts: 7,401

Re: DATETIME22.3 to year

You could try: year(*)

where the * is replaced by your date variable name.

Super User
Posts: 17,831

Re: DATETIME22.3 to year

Year= year(datepart(variable));

Or depending on the proc you can simply apply a format to your variable of

format variable dtyear.;

SAS Super FREQ
Posts: 683

Re: DATETIME22.3 to year

There are various ways, using DATEPART and YEAR function or DTYEAR format, see below

data have;
  eventDate = datetime();
 
format eventDate datetime22.3;

  eventYear = year( datepart(eventDate) );
  eventYear_c = put(eventDate,
dtyear4.);
run;
Occasional Contributor NOA
Occasional Contributor
Posts: 8

Re: DATETIME22.3 to year

Mr. Bruno Muller thank you. It worked but for some reason the only year the comes out is 2014. I can see in the LOG that my EVENTDATE are all from today.

When i looked in the SQL server database I am using, the dates ranged from 2008-2014. Any idea what might be going on?

SAS Super FREQ
Posts: 683

Re: DATETIME22.3 to year

Just in case the two lines

eventDate = datetime();
format eventDate datetime22.3;

are only for testing purposes, they are not needed in your code

Super User
Posts: 17,831

Re: DATETIME22.3 to year

Post your code

Occasional Contributor NOA
Occasional Contributor
Posts: 8

Re: DATETIME22.3 to year

I got it to work. Thank you!

Super User
Posts: 17,831

Re: DATETIME22.3 to year

mark as answered please Smiley Happy

Ask a Question
Discussion stats
  • 8 replies
  • 2200 views
  • 4 likes
  • 4 in conversation