Maiia S.
Maiia S.

Reputation: 323

SAS - Custom Date Format - YYYYMMDDhhmmss

I need to add a current date to the output I am exporting from SAS in the following format: filename_YYYYMMDDhhmmss.csv

I am creating a macro variable the following way:

%let date_human = %sysfunc(today(), YYYYMMDDn8.);

Does anybody know how to create a custom format for the date I have got? datetime20. gives an incorrect one.

Thank you.

Upvotes: 1

Views: 10982

Answers (3)

Tom
Tom

Reputation: 51566

Use the B8601 formats.

%let now=%sysfunc(datetime());
%let date_human=%sysfunc(putn(&now,B8601DN8))%sysfunc(timepart(&now),B8601TM6);

Upvotes: 11

kstats9pt3
kstats9pt3

Reputation: 873

Use picture in proc format:

proc format;
    *custom format for the datetime values;
   picture cust_dt other = '%0Y%0m%0d%0H%0M%0S' (datatype=datetime);
run;

** put into macro **;
data test;
    dt = datetime();
    call symputx("dt",strip(put(dt,cust_dt.)));
run;

%put &dt.;

** use macro for filename **;
data file_&dt.; set sashelp.class;
run;

Upvotes: 0

Stu Sztukowski
Stu Sztukowski

Reputation: 12849

Two solutions:

1. Create a picture format with datetime directives using proc format

Here, we are creating a format called date_human. You can name this anything that you'd like.

proc format;
    picture date_human other='%0Y%0M%0D%0H%0M%0S' (datatype=datetime);
run;

%let date_human = %sysfunc(datetime(), date_human.);

2. Mash together various macro variables

/* YYYYMMDD part */
%let yymmdd   = %sysfunc(today(), yymmdd8.);

%let datetime = %sysfunc(datetime());
%let time     = %sysfunc(putn(%sysfunc(timepart(&datetime.)), time8.0));

/* hhmmss part */
%let hour     = %scan(&time., 1, :);
%let min      = %scan(&time., 2, :);
%let sec      = %scan(&time., 2, :);

/* Put it all together */
%let date_human = &yymmdd.&hour.&min.&sec.;

Upvotes: 5

Related Questions