BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jimksas
Calcite | Level 5

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...

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

7 REPLIES 7
Scott_Mitchell
Quartz | Level 8

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.;

jimksas
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.;

Scott_Mitchell
Quartz | Level 8

Did you execute the Proc Format code first?

Kurt_Bremser
Super User

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";

Tom
Super User Tom
Super User

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" ;

jimksas
Calcite | Level 5

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...

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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