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

Hello,

 

I was wondering if anyoen could help me with the datepart function. I have tried a few things I've seen in the message boards, with no success.

 

The variable 'birth_date' is set up like this   09APR62:23:59:59 (it is numeric and the format is DATETIME18.)- this is one example but there are about 10,000 records all with different birth dates

 

In this example I want only the year (62) and to be changed to 1962. I am not sure how to isolate just this number and also add the 19 in front of it for every row.


Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User
The function is DATEPART() not DATETIME().
The DATETIME() function returns the current datetime value from the computer's clock.

View solution in original post

13 REPLIES 13
GraphGuy
Meteorite | Level 14

data foo;
format my_dt datetime.;
my_dt='09APR62:23:59:59'dt;
format my_date date9.;
my_date=datepart(my_dt);
year=put(my_date,year4.);
run;

 

proc print data=foo; run;

 

table.png

Tom
Super User Tom
Super User

@GraphGuy wrote:

data foo;
format my_dt datetime.;
my_dt='09APR62:23:59:59'dt;
format my_date date9.;
my_date=datepart(my_dt);
year=put(my_date,year4.);
run;

 

proc print data=foo; run;

 

table.png


Note that there is a DTYEAR format that can by applied directly to the datetime value to display just the year value.

sashelp123
Calcite | Level 5

thank you that fixed up the first row, but sorry a detail that I failed to mention is that there are thousands of records all with different dates, so the first row was properly changed to 1962, but I need it to part out the date for every row if that's possible

Tom
Super User Tom
Super User

@sashelp123 wrote:

thank you that fixed up the first row, but sorry a detail that I failed to mention is that there are thousands of records all with different dates, so the first row was properly changed to 1962, but I need it to part out the date for every row if that's possible


Show what code you actually ran.

sashelp123
Calcite | Level 5

data foo;

set xxx;

format my_dt datetime.;

my_dt='09APR62:23:59:59'dt;

format my_date date9.;

my_date=datepart(my_dt);

year=put(my_date,year4.);

run;

 

 

 

Tom
Super User Tom
Super User

First thing to note is that there is a bug in (feature of?) the DATETIME format.  A width of 18 should be enough to display date with 9 digits and time with 8 and one for the colon.  But for some reason it instead uses only 7 characters for the date.  Change to using DATETIME19 or longer.

 

If you want the year from a datetime value then you can nest call to DATEPART() function inside the call to YEAR() function.

yob = year(datetime(birth_date));

Another thing to consider is to not use _DATE suffix on the names of variables that contain DATETIME values instead of DATE values.  It will just lead to confusion.  Use DATETIME or TIMESTAMP instead to make it clearer what type of data the variable contains.

sashelp123
Calcite | Level 5

Hi thank you for your reply! I am still learning and I'm not sure how to include your code in a statement, like what the full code would like like to do that? thank you

Tom
Super User Tom
Super User

So assuming you have a dataset named HAVE and a variable named BIRTH_DATE the first thing to check is whether the variables already have the right century or not.  You could try seeing what the distribution of values is by using PROC FREQ.

proc freq data=HAVE;
  tables BIRTH_DATE;
  format BIRTH_DATE dtyear4.;
run;

If you want to make a copy of your data and in the process fix the format attached you can use a simple data step like this.

data WANT;
  set HAVE;
  format BIRTH_DATE datetime19.;
run;

If you want to fix the format attached to your existing dataset you can just use PROC DATASETS to update the dataset without having to make a copy of the data.

proc datasets lib=WORK nolist;
  modify HAVE ;
    format BIRTH_DATE datetime19.;
  run;
quit;

 

sashelp123
Calcite | Level 5

ok thank you! so I did that and changed the format to 19, so now how do I create a variable (year) that is just 19 plus the 2 values for the end year? you mentioned yob=year(datetime(birth_date)), does that go in a data step, or how do I make that change? thanks!

 

Data date_clean;

set want;

yob = year(datetime(birth_date));

run;

 

 

Tom
Super User Tom
Super User

@sashelp123 wrote:

ok thank you! so I did that and changed the format to 19, so now how do I create a variable (year) that is just 19 plus the 2 values for the end year? you mentioned yob=year(datetime(birth_date)), does that go in a data step, or how do I make that change? thanks!

 

Data date_clean;

set want;

yob = year(datetime(birth_date));

run;

 

 


That code will create a new variable named YOB that has the year value from the datetime variable BIRTH_DATE.

Is that what you want?  I don't understand what you mean by "end year".

sashelp123
Calcite | Level 5

oh I'm not sure why I said 'end year' I just meant year. So currently in the date it is just showing for example '62' for the year, but I want it to change to 1962. Also when I put that code into sas I seem to be getting an error message.

 

126 Data date_clean;

127 set want;

128 yob = year(datetime(birth_date));

--------

72

ERROR 72-185: The DATETIME function call has too many arguments.

129 run;

Tom
Super User Tom
Super User
The function is DATEPART() not DATETIME().
The DATETIME() function returns the current datetime value from the computer's clock.
sashelp123
Calcite | Level 5

Success, thank you very much! 🙂

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 13 replies
  • 1522 views
  • 0 likes
  • 3 in conversation