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!
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;
@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;
Note that there is a DTYEAR format that can by applied directly to the datetime value to display just the year value.
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
@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.
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;
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.
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
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;
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;
@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".
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;
Success, thank you very much! 🙂
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: