As the first step in the decommissioning of the site has been converted to read-only mode.

Here are some tips for How to share your SAS knowledge with your professional network.

Date datetime time stamp

From sasCommunity
Jump to: navigation, search

How do I create a unique data set name in a macro?

%local Data;%let Data = 

Notes: however long your macro name is, take the first 16 characters for the left-hand side; right-hand 16 characters is date+time stamp in hexadecimal, which is accurate to datetime22.2.

--macro maven == the radical programmer 18:27, 23 May 2007 (EDT)

How do I date-stamp a file?

Note: date is number of days since Jan 1, 1960, which is zero.

*julian date: ccyyDDD where DDD is in (001:366);
*ex: May 9, 2007;

%Put file: "filename%sysfunc(date(),julian7.).txt";

*file: "filename2007129.txt";

* YYMMDD generates names which are intelligible without consulting a calendar;

%Put file: "whatever-%sysfunc(date(),yymmddd10.).txt";

*file: "whatever-2007-05-09.txt";

How do I datetime-stamp a file?

Note: datetime is number of seconds since midnight, Jan 1, 1960, which is zero.

*julian date: ccyyDDD where DDD is in (001:366);
*ex: May 9, 2007, 9:43 am;

%Put file: "filename%sysfunc(datetime(),hex16.).txt";

*file: "filename41D64463C4CD2F1B.txt";

* datetimeW.D produces ;

%Put file: "whatever-%sysfunc(datetime(),datetime20.3).txt";

*file: "whatever-09MAY07:09:43:57.299.txt";
*note: colon cannot be used in file-name in Windows;

%Put file: "whatever-%sysfunc(translate(%sysfunc(datetime(),datetime20.3),--,.:)).txt";

*file: "whatever-09MAY07-09-48-23-893.txt";

How do I time-stamp a file?

Note: time is number of seconds from midnight which is zero.

*ex: May 9, 2007, 9:51 am;

%Put file: "filename%sysfunc(time(),hex16.).txt";

*file: file: "filename40E15D0991688000.txt";

Q: What is the datetime-stamp of a program log?

A: the global macro variable SysProcessId, a session constant, contains the datetime-stamp of the program log, in the first 16 characters, which are displayed in hex16.

Other information in SysProcessId:

%Put SysProcessId <&SysProcessId.>;
             0    +    1    +    2    +    3
SysProcessId <41D865326560624E4018000000000000>
*  1--16 hex16(datetime())
* 17--21 SysId == OpSys UserNmbr
options nosource;
%Put SysProcessId <&SysProcessId.>;
%Let DateTimeStart = %substr(&SysProcessId.,1,16);
%Put DateTimeStart<&DateTimeStart.>;
%Put DateTimeStart<%sysfunc(inputn(&DateTimeStart.,hex16.))>;
%Put ;
%Put DateTime. . .<%sysfunc(datetime(),hex16.)>;
%Put DateTime. . .<%sysfunc(datetime(),        20.3)>;
%Put DateTime. . .<%sysfunc(datetime(),datetime20.3)>;
%Put ;
%Let DateTimeNow   = %sysfunc(datetime(),hex16.);
%Put DateTimeNow .<&DateTimeNow.>;
%Put DateTimeNow .<%sysfunc(inputn(&DateTimeNow.,hex16.))>;
%Put ;
%Put DateTimeNow .<%sysfunc(  putn(
%Put DateTimeStart<%sysfunc(  putn(
%Put .                     ------------;
%Put TimeElapsed . . . . . %sysfunc(  putn(
                           %sysfunc(sum   (
                           %sysfunc(inputn(&DateTimeNow.  ,hex16.))
                                   )      )             ,time12.3))>;
     DATA _Null_;
put 'DATA _Null_';

    DateTimeStart   = input("%substr(&SysProcessId.,1,16)",hex16.);
put DateTimeStart   = hex16.
  / DateTimeStart   = datetime20.3;

    DateTimeElapsed = datetime()  - DateTimeStart;
put DateTimeElapsed = hex16.
  / DateTimeElapsed =     time12.3;

    DateTimeNow     = datetime()  ;
put DateTimeNow     = hex16.
  / DateTimeNow     = datetime20.3;

    DateTimeElapsed = sum(  DateTimeNow
                         ,- DateTimeStart);
put DateTimeElapsed = hex16.
  / DateTimeElapsed =     time12.3;


--Ronald_J._Fehd macro.maven == the radical programmer 9 May 2007 and 23 June 2012