Matt
Matt

Reputation: 60

Creating a filename with today's date in SAS.

I would like to be able to save a file in SAS with today's date. I'm having trouble creating the file path with today's date.

Given that today's current date is 3/27/2018. I would like the created file path to be this:

"/sasFolder/MyFileName(3-27-2018).xlsx"

My current code is:

data _null_;
call symput('dateMonth', month(date()));
call symput('dateDay', day(date()));
call symput('dateYear', year(date()));
run;

%let filePath = "/sasFolder/MyFileName(&dateMonth.-&dateDay.-&dateYear.).xlsx";

data _null_;
put &filePath;
run;

Currently my output is this, with _ representing spaces.

"/sasFolder/MyFileName(___________3-__________26-________2018).xlsx"

I would like for the filename to not have all theses extra spaces in the name. Any ideas on how to do this?

Upvotes: 1

Views: 7499

Answers (3)

Allan Bowe
Allan Bowe

Reputation: 12691

You can do this very easily without a data step using %sysfunc() - this lets you call a SAS function and apply a format at the same time, eg:

%let filePath = "/sasFolder/MyFileName(%sysfunc(today(), mmddyyd10.)).xlsx";
%put &=filepath;

Which gives:

FILEPATH="/sasFolder/MyFileName(03-27-2018).xlsx"

Upvotes: 7

Tom
Tom

Reputation: 51601

There is a format for that date style already. Looks like you using the MMDDYYD10. format. You could use it in your data step code.

data _null_;
  call symputx('datestamp', put(date(),mmddyyd10.));
run;
%let filePath="/sasFolder/MyFileName(&datestamp).xlsx";

Or skip the data step and use the %sysfunc() macro function to call the date() function and apply the format.

%let datestamp=%sysfunc(date(),mmddyyd10.);

You can even eliminate the extra macro variable.

%let filePath="/sasFolder/MyFileName(%sysfunc(date(),mmddyyd10.)).xlsx";

Note that I recommend that you switch to using YYMMDD format instead of MMDDYY format. First it will prevent your English friends from confusing December tenth and October twelfth. Second it will allow your file names to sort in chronological order.

Upvotes: 2

Matt
Matt

Reputation: 60

Figured it out. Needed to change symput to symputx to remove the spaces.

The code is:

data _null_;
call symputx('dateMonth', month(date()));
call symputx('dateDay', day(date()));
call symputx('dateYear', year(date()));
run;

%let filePath = "/sasFolder/MyFileName(&dateMonth.-&dateDay.-&dateYear.).xlsx";

data _null_;
put &filePath;
run;

Upvotes: 0

Related Questions