Adding timestamp to data set name

From sasCommunity
Jump to: navigation, search
  • From: SAS(r) Discussion [1] On Behalf Of sas 9 bi user
  • Sent: Monday, October 27, 2008 1:16 AM
  • To: SAS-L@LISTSERV.UGA.EDU
  • Subject: Add Timestamp to dataset's name

All -

Say I have

data b;
set a;
run;

I want to add a timestamp to B. When I try to do the below it errors out.

data b_%sysfunc(datetime());
set a;
run;

This is what I would like B to be named as:

b_10/27/2008_12:14AM

Note - Date/time is what sysfunc generates at time of call (so the above is at the time of this email writing for example).

Any ideas?

Thanks!


Original Message-----

From: Mike Rhoads

I see a couple of issues here:

1. By default the value displayed by datetime() is an unformatted number. You need to format it to make it understandable to humans.

2. The data set name format that you want contains characters that are invalid in SAS data set names (: and /).

While there are a number of possible ways to approach this, I would create a format using date-time directives (check the PROC FORMAT documentation) to produce a format that is readable but only contains acceptable characters, and use that with the %sysfunc call.

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

data b_%unquote(%sysfunc(datetime(),mydtfmt.));
set  sashelp.class;
run;

NOTE: The data set WORK.B_20081027_0905AM has 19 observations and 5 variables.

Mike Rhoads

See also: Macro_Variables_of_Date_and_Time