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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

data _null_;

   dtm = '16DEC2020:00:00:00.000'dt;

   date = datepart(dtm);

   putlog date yymmdd10.;

run;

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

data _null_;

   dtm = '16DEC2020:00:00:00.000'dt;

   date = datepart(dtm);

   putlog date yymmdd10.;

run;

KlausBücher
Fluorite | Level 6

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;

PaigeMiller
Diamond | Level 26

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
Tom
Super User Tom
Super User

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.

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
  • 4 replies
  • 1568 views
  • 1 like
  • 5 in conversation