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

Hello,

 

I am trying to convert converting date YYYY-MM-DDT00:00:00 to MM-DD_YYYY.  

 

Example 2016-10-16T00:00:00 to 10162016

 

Code used

data date1;

set date;

newdate=input(yymmdd10.);

format newdate mmddyy10.;

run;

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
unison
Lapis Lazuli | Level 10

If you have a true SAS datetime, you can access the date part with datepart()

and then re-format to mmddyyn8:

 

data have;
mydate='10NOV05:03:49:19'dt;
format mydate datetime.;
run;

data want;
set have;
newdate=datepart(mydate);
format newdate mmddyyn8.;
run;

-unison

-unison

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20


data want;
char_date='2016-10-16T00:00:00';
want_date=input(char_date,yymmdd10.);
format want_date mmddyyn8.;
run;
data want;
set have;
want_date=input(char_date,yymmdd10.);
format want_date mmddyyn8.;
run;

 

heatherjae15
Obsidian | Level 7

Thank you for replying to my question.  Unfortunately I am getting . in my new date variable.  Do you have any thoughts as to what I may have done wrong?

 

 

Tom
Super User Tom
Super User

There are many.  You can help narrow the possibilities down by providing more information.  What is the name of the variable you have? Is it numeric or character?  Does if have a format attached to it? If so what format?  Did your test code generate errors or warnings? Show the log. Make sure to copy the text from the log and use the Insert Code button on the forum editor to get a pop-up window to paste the lines of text. This will prevent the forum from reflowing the text as if it was a paragraph.

Tom
Super User Tom
Super User

Do you have a question here? 

Did the code you posted not work? 

Do you want to create a numeric variable with date values (number of days)?  Or another character variable?

Do you want the dates to display with the mixed hyphen and underscore delimiters as in your title?   Or would you prefer to see them with some other delimiter?  The MMDDYYx format lets you pick the delimiter by changing the letter X to one of:  BCDPS or use N and get no delimiter at all.

 

Is your source variable really a character string? Or is it a datetime value with a format that makes appear in that style?  If so then just use the DATEPART() function to get the number of days from the number of seconds the variable currently has.

 

 

ballardw
Super User

@heatherjae15 wrote:

Hello,

 

I am trying to convert converting date YYYY-MM-DDT00:00:00 to MM-DD_YYYY.  

 

Example 2016-10-16T00:00:00 to 10162016

 

Code used

data date1;

set date;

newdate=input(yymmdd10.);

format newdate mmddyy10.;

run;

 

Thank you


Is your variable date character or is it numeric with a format such as E8601T18. applied?

If character convert as @novinosrin shows.

If the variable has that E8601 (or similar ) format then the value is a DATETIME and can get the date portion with the DATEPART function:

data date1;
   set date;
   newdate= datepart(date);
   format newdate mmddyy10.;
run;

you need to provide a character value to the INPUT function, either as a literal value or the name of the variable.

Your data step probably threw at least one error related to the syntax of the input function.

unison
Lapis Lazuli | Level 10

If you have a true SAS datetime, you can access the date part with datepart()

and then re-format to mmddyyn8:

 

data have;
mydate='10NOV05:03:49:19'dt;
format mydate datetime.;
run;

data want;
set have;
newdate=datepart(mydate);
format newdate mmddyyn8.;
run;

-unison

-unison

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
  • 6 replies
  • 7536 views
  • 3 likes
  • 5 in conversation