DATA Step, Macro, Functions and more

Is it possible to create a timestamp in a file name

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

Is it possible to create a timestamp in a file name

%if "&option"="Y" %then %do;
%make_dlm_file(dataset = base_recs, /* make_dlm_file creates a delimited output file */
filename = "/home/xxxxx/Output_&cntry._&id.txt",
dlmr = "|",
qtes = "no",
header = "no",
label = "no");
run;

Is it possible to add a timestamp to the output file name :- /home/xxxxx/Output_&cntry._&id_<timestamp>.txt

 

 


Accepted Solutions
Solution
‎04-25-2017 03:49 AM
Super Contributor
Posts: 259

Re: Is it possible to create a timestamp in a file name

Yes, it is.

 

data _null_;   
   call symputx('timestamp', put(datetime(), B8601DT.));
run;

%make_dlm_file(dataset = base_recs, /* make_dlm_file creates a delimited output file */
filename = "/home/xxxxx/Output_&cntry._&id_&timestamp..txt",
dlmr = "|",
qtes = "no",
header = "no",
label = "no");

View solution in original post


All Replies
Trusted Advisor
Posts: 1,128

Re: Is it possible to create a timestamp in a file name

Please try the global macro variables
&sysdate9. for date and &systime5. for time
Thanks,
Jag
Solution
‎04-25-2017 03:49 AM
Super Contributor
Posts: 259

Re: Is it possible to create a timestamp in a file name

Yes, it is.

 

data _null_;   
   call symputx('timestamp', put(datetime(), B8601DT.));
run;

%make_dlm_file(dataset = base_recs, /* make_dlm_file creates a delimited output file */
filename = "/home/xxxxx/Output_&cntry._&id_&timestamp..txt",
dlmr = "|",
qtes = "no",
header = "no",
label = "no");
Frequent Contributor
Posts: 93

Re: Is it possible to create a timestamp in a file name

Yes, this is one of my favorite techniques for making a workbook with many sheets:

 

data _null_;
call symputx('nowyear', year(date()));
call symputx('nowmonth', month(date()));
call symputx('nowday', day(date()));
call symputx('nowhour', hour(datetime()));
call symputx('nowminute', minute(datetime()));
run;

%let myvar = %str(&nowyear)_%str(&nowmonth)_%str(&nowday)_%str(&nowhour)_%str(&nowminute); 
%let title = "F:\Main\Studies\ICO_DUALS\EnrollBuckets\Documents\EN_%str(&myvar).xlsx";

PROC EXPORT DATA=RPT_T1 outfile= &title dbms=xlsx replace;
sheet="RPT_T1_TRAN";
run;

You can modify for .txt I believe.  Repeat the proc export for as many as you would like. 

Super User
Posts: 6,928

Re: Is it possible to create a timestamp in a file name


thomp7050 wrote:

Yes, this is one of my favorite techniques for making a workbook with many sheets:

 

data _null_;
call symputx('nowyear', year(date()));
call symputx('nowmonth', month(date()));
call symputx('nowday', day(date()));
call symputx('nowhour', hour(datetime()));
call symputx('nowminute', minute(datetime()));
run;

%let myvar = %str(&nowyear)_%str(&nowmonth)_%str(&nowday)_%str(&nowhour)_%str(&nowminute); 
%let title = "F:\Main\Studies\ICO_DUALS\EnrollBuckets\Documents\EN_%str(&myvar).xlsx";

PROC EXPORT DATA=RPT_T1 outfile= &title dbms=xlsx replace;
sheet="RPT_T1_TRAN";
run;

You can modify for .txt I believe.  Repeat the proc export for as many as you would like. 


Keep in mind that unformatted values for month, day, hours, minutes, seconds may prevent proper sorting of filenames. Use the z2. format to keep a leading zero.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,392

Re: Is it possible to create a timestamp in a file name

As a slight away from your question, I note that most people put date/time information in filenames/sheet names etc.  Generally I find this practice to be a bit archaic and too much hard work.  Say you send me a files named XYZ_ddmmmyyyy. txtx, then in a few days another with a different date.  There then has to be faff about what file to use, how to get filename etc.  I would hope that in this day and age all users are using some sort of automated versioning system, and dated filenames just does not fit that at all, you want the next file to have the same filename so you can look at the audit trail of the files.  Just saying this as personally, if I am using a VCS then I would not accept  structural items (filename, column names and such like) changing between files.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 180 views
  • 3 likes
  • 6 in conversation