- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data _null_;
dtm = '16DEC2020:00:00:00.000'dt;
date = datepart(dtm);
putlog date yymmdd10.;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data _null_;
dtm = '16DEC2020:00:00:00.000'dt;
date = datepart(dtm);
putlog date yymmdd10.;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.