I have a numeric date with time and I need to convert just the datepart into yyyy-mm-dd format.
data (numeric):
16DEC2020:00:00:00.000
want (numeric):
2020-12-16
data _null_;
dtm = '16DEC2020:00:00:00.000'dt;
date = datepart(dtm);
putlog date yymmdd10.;
run;
data _null_;
dtm = '16DEC2020:00:00:00.000'dt;
date = datepart(dtm);
putlog date yymmdd10.;
run;
If you don't want to use the Macro Language:
data test;
format data datetime20.;
data = "16dec2020:0:0:0"dt;
want = put(datepart(data), yymmdd10.);
run;
The difference between the two solutions above is that the method from @Shmuel leaves the date as numeric, which then enables you to perform arithmetic and logical operations on it.
The solution from @KlausBücher creates a character string which looks like a date to humans, but SAS will never consider this a date and so you can't perform arithmetic or logical operations on it (well, actually you can perform character string logical operations on it).
So I strongly recommend keeping the date numeric.
Your question is strangely worded so it is not clear what you are asking for. In SAS a FORMAT is just instructions for how to display the values. There are only two types of variables, fixed length character strings and floating point numbers.
If you just want to display the DATETIME values in Y-M-D order then use the E8601DN10. format.
want=data;
format want b8601dn10.;
If you want to first convert the datetime values (number of seconds) to date values (number of days) then use the DATEPART() function. Then for the newly created date values you can use either the YYMMDD10. format or the E8601DA10. format.
want = datepart(data);
format want yymmdd10.;
If you really wanted to create a character string then use the PUT() function with the appropriate format to convert the numeric values into strings.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.