SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DeepakSwain
Pyrite | Level 9

Hi there, 

 

I need your kind help to get system date in YYYYMMDDHHMMSS format. 

 

data _null_;
export_date=put(datetime(), datetime20.);
put export_date;
run;

 

Swain
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

If you want an exact match, you can roll out your own format like this

 

proc format; 
  picture dtfmt (default=30)
    other = '%Y%0m%0d%0H%0M%0S' (datatype=datetime)
  ;
run;

data _null_;
  export_date=put(datetime(), dtfmt.);
  put export_date;
run;

View solution in original post

8 REPLIES 8
AMSAS
SAS Super FREQ

Is this close enough:

 

 

data _null_;
  export_date=put(datetime(), E8601DT.);
  put export_date;
run;

See Dictionary of Formats for a list of formats 

 

PeterClemmensen
Tourmaline | Level 20

How about

 

data _null_;
export_date=put(datetime(), B8601DT.);
put export_date;
run;
DeepakSwain
Pyrite | Level 9
Date will look like 20211012134021
Swain
ballardw
Super User

@DeepakSwain wrote:
Date will look like 20211012134021

Requires custom format:

proc format;
picture mydatetime
low-high='%Y%0m%0d%0H%0M%0S' (datatype=datetime)
;
run;

data example;
   x='12Oct2021:13:40:21'dt;
   put x= mydatetime.;
run;

The directives in the Picture statement are case sensitive, the m is month, M is minutes mix them up and don't expect to understand any result. Also this is one place that you really need to use single quotes around the directives for the format, otherwise the directives get interpreted as macro calls and will throw errors. It will be up to you to make sure that the format is available in any session than needs to use this.

PeterClemmensen
Tourmaline | Level 20

@ballardw Great minds think alike 😉

PaigeMiller
Diamond | Level 26

I don't think there is a format that is exactly YYYYMMDDHHMMSS, this comes close:

 

data null;
export_date=datetime();
format export_date b8601dt20.0;
run;

If that isn't close enough, the PICTURE statement in PROC FORMAT allows you to create your own custom date/time formats that will match exactly.

--
Paige Miller
DeepakSwain
Pyrite | Level 9
e8601dt20. will return 2021-10-21T13:46:07.
I want 20211021134607.
Swain
PeterClemmensen
Tourmaline | Level 20

If you want an exact match, you can roll out your own format like this

 

proc format; 
  picture dtfmt (default=30)
    other = '%Y%0m%0d%0H%0M%0S' (datatype=datetime)
  ;
run;

data _null_;
  export_date=put(datetime(), dtfmt.);
  put export_date;
run;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 18094 views
  • 2 likes
  • 5 in conversation