BookmarkSubscribeRSS Feed
NOA
Calcite | Level 5 NOA
Calcite | Level 5

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.


8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You could try: year(*)

where the * is replaced by your date variable name.

Reeza
Super User

Year= year(datepart(variable));

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

format variable dtyear.;

BrunoMueller
SAS Super FREQ

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;
NOA
Calcite | Level 5 NOA
Calcite | Level 5

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?

BrunoMueller
SAS Super FREQ

Just in case the two lines

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

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

Reeza
Super User

Post your code

NOA
Calcite | Level 5 NOA
Calcite | Level 5

I got it to work. Thank you!

Reeza
Super User

mark as answered please Smiley Happy

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5276 views
  • 4 likes
  • 4 in conversation