JeffK627
JeffK627

Reputation: 305

Add Datetime Stamp to SAS 9.1.3 Filename?

I'll start by saying I have very little experience with SAS. I'm working with SAS 9.1.3 in Windows. I have no choice in the matter.

I need to name SAS Transport (XPT) file with the format "CR845_CLIN2001_LB_Date_Time.xpt". Currently I have a script that names it with just the date (sysdate9).

The lines of code that use the date are as follows - these are the lines that I think need to be changed - they are not all in a block:

libname XPORTOUT xport "\\ACMSHARES2\CLNTRIAL\DataMgt\C1845\DataTransfer\Data\Sent\SAS\CR845_CLIN2001_LB_&sysdate9..xpt";

data LabData.&fileBaseName._&sysdate9 (COMPRESS=YES);

data LB.LB; 
    set LabData.&fileBaseName._&sysdate9; 
run;

proc contents data = LabData.&fileBaseName._&sysdate9 varnum;
run;

I've tried a couple of things but SAS rejects colons in the time. Can anyone help me out? Thanks in advance!

Upvotes: 1

Views: 2952

Answers (5)

Reeza
Reeza

Reputation: 21274

Since the colons are the issue, you could remove them using the compress function which I believe did exist in SAS 9.1.3. Not pretty, but quick and effective.

%let today=%sysfunc(datetime(), datetime21.);
%let today=%sysfunc(compress(&today, :));
%put &today;

19AUG2015141413

And another way to get the T in there

data _null_;
date=put(datetime(), datetime21. -l);
substr(date, 10, 1)="T";
date=compress(date, ":");
call symputx('date_value', date);
run;

%put &date_value;

19AUG2015T141801

Upvotes: 0

Tom
Tom

Reputation: 51601

Generate a macro variable with the suffix you want. I would recommend using format of YYYYMMDD_HHMM as it will sort properly. Then use the new macro variable in place of &SYSDATE9 when generating the name of the XPORT file.

%let dt=%sysfunc(today(),yymmddn8)_%sysfunc(compress(%sysfunc(time(),time5),:));
libname XPORTOUT xport "\\....\CR845_CLIN2001_LB_&dt..xpt";

If you want to include seconds then use TIME8 instead of TIME5 format.

Upvotes: 0

Robert Penridge
Robert Penridge

Reputation: 8513

This may be overkill for your purpose, but others may find this useful. In SAS you can create your own datetime formats when you find the existing ones lacking.

A good whitepaper can be found here.

Here is an example that would create the format you are trying to achieve:

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

Example usage:

%put %sysfunc(datetime(), myfmt.);

Gives:

20150819_1304

If you want to add seconds, the token for it is: %0S.

Upvotes: 2

JeffK627
JeffK627

Reputation: 305

Well, I cobbled together a truly awful solution. It works, but it's ugly as sin. I added this mess:

%let cyear=%sysfunc(putn("&sysdate9"d, year4.));
%let cmon =%SUBSTR(%SYSFUNC(PUTN(%sysevalf(%SYSFUNC(TODAY())),DATE9.)),3,3);
%let cday =%SUBSTR(%SYSFUNC(PUTN(%sysevalf(%SYSFUNC(TODAY())),DATE9.)),1,2);
%let chour=%SUBSTR(%SYSFUNC(PUTN(%sysevalf(%SYSFUNC(TIME())),TIME5.)),1,2);
%let cmin=%SUBSTR(%SYSFUNC(PUTN(%sysevalf(%SYSFUNC(TIME())),TIME5.)),4,2);

and then put this in place of "sysdate9" in the lines of code shown in my original question:

&cyear&cmon&cday._&chour&cmin

It works, but I hate it. Improvements welcome!

Upvotes: 0

Joe
Joe

Reputation: 63424

Colons are disallowed in Windows filenames. Use a better format, such as my personal preference, b8601dt:

proc export data=sashelp.class 
            outfile="c:\temp\class_%sysfunc(datetime(),B8601DT15.).csv" 
            dbms=csv replace;
run;

However, for 9.1.3, B8601DT (and the other IEEE date formats) isn't supported, since that's around fifteen years old... you'd have to come up with another, more piecemeal solution.

My preference here would be to represent the time in seconds, which would make the filename still able to be unique (and sort properly). Split date and time. I assume here your use of &sysdate9 is acceptable (this is the date SAS starts, not the date of today, but if this is a batch job it'll be fine); but still I use %today() over &systime as it's easier to get it formatted quickly.

Upvotes: 1

Related Questions