Desktop productivity for business analysts and programmers

help on datetime macro please

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

help on datetime macro please

Hello,

i want to set up filename ending with datetime as below on daily report...please help...

filename xyz_abc_20140710112330.txt

/*filename xyz_abc_yyyymmddhhmmss.txt*/

Thank you in advance...


Accepted Solutions
Solution
‎07-10-2014 04:33 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: help on datetime macro please

What does it say in the log with options mlogic sybolgen mprint.  I would imagine that you would at least need in the filename statement %trim(&fname) to avoid extra spaces.  Personally I wouldn't go with formats, but it depends on where you are getting the date and time from, is it today() function, a dataset etc.

data _null_;

     call symput('FNAME',compress(put(today(),yymmdd10.),"- ")||compress(put(time(),time8.),": "));

run;

%put xyz_abc_&fname.;

View solution in original post


All Replies
Super Contributor
Posts: 296

Re: help on datetime macro please

How about this?

Create a custom format.

PROC FORMAT;

  PICTURE YMDDT OTHER='%0Y%0M%0D%0H%0M%0S' (DATATYPE=DATETIME);

RUN;

Create macro variable containing the date and time of right now and use custom format.

DATA _NULL_;

CALL SYMPUTX ("FNAME","XYZ_ABC_"||TRIM(LEFT(PUT(DATETIME(),YMDDT.))));

RUN;

%PUT &FNAME.;

Contributor
Posts: 55

Re: help on datetime macro please

YMDDT.


is giving error...format cannot ound and could not be loaded...


i am trying create macro for only datetime function not with filename, so i can write down like this....


filename xyz_abc_&fname.txt

Solution
‎07-10-2014 04:33 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: help on datetime macro please

What does it say in the log with options mlogic sybolgen mprint.  I would imagine that you would at least need in the filename statement %trim(&fname) to avoid extra spaces.  Personally I wouldn't go with formats, but it depends on where you are getting the date and time from, is it today() function, a dataset etc.

data _null_;

     call symput('FNAME',compress(put(today(),yymmdd10.),"- ")||compress(put(time(),time8.),": "));

run;

%put xyz_abc_&fname.;

Super Contributor
Posts: 296

Re: help on datetime macro please

Did you execute the Proc Format code first?

Esteemed Advisor
Posts: 6,646

Re: help on datetime macro please

data _null_;

now=datetime();

dat1=datepart(now);tim1=put(timepart(now),time8.);

filname='xyz_abc'!!put(dat1,yymmddn8.)!!substr(tim1,1,2)!!substr(tim1,4,2)!!substr(tim1,7,2)!!'.txt';

call symput('filnam',filname);

run;

filename outfile "/path_to_the_directory/&filnam";

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 6,317

Re: help on datetime macro please

I would just use B8601DT. format.  If you really do not want the T between the date and time parts you could use COMPRESS() function to remove it.

%let dt=%sysfunc(datetime(),B8601DT);

%put dt=&dt;

dt=20140710T083935

%let filename=xyz_abc_%sysfunc(compress(&dt,T)).txt ;

%put filename=&filename ;

filename=xyz_abc_20140710083935.txt

You can now use anywhere a physical filename is required.

filename outfile "&filename" ;

file "&filename" ;

Contributor
Posts: 55

Re: help on datetime macro please

No Scott, i didn't run it Smiley Sad  ...may be that's why...- Thanks

Thanks all for your detail...many ways to do it... :-)

I am using RW9 solution...

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 484 views
  • 6 likes
  • 5 in conversation