BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Kirito1
Quartz | Level 8

First I was trying to give the filename the exact date on which I exported the file.

proc export data=AVG
outfile="xyz/Transactions \%sysfunc( today(), date9 ).csv"
REPLACE
DBMS=CSV;
run;

This is giving me an output filename as  (Transactions 22DEC2022).

--------------

But now I want the date which I specify in a code should be attached with the File name.

For Example: I specified a date in code as 19DEC2022  and the file name is Transactions. So, the output filename should be Transactions 19DEC2022.

I think this problem could be solved using macros but I am not sure how to proceed it. Please! help.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

SAS dates are counts of days, starting with 1960-01-01 as day zero.

So you only need to subtract a day from today:

%let yesterday = %sysfunc(putn(%sysfunc(today())-1,yymmddn8.));

Or you use a DATA step:

data _null_;
call symputx("yesterday",put(today()-1,yymmddn8.));
run;

View solution in original post

11 REPLIES 11
Kurt_Bremser
Super User

From longtime IT experience, two tips:

  • do not use DMY dates in filenames. Use YMD instead, so the files sort in chronological order on their own, and it is easiervto use wildcards to pick years or months.
  • avoid blanks in filenames. Use underlines instead.

How do you "specify a date in a code"? Please show that particular piece of code.

Use the "little running man" button to open a window into which you paste the code. This keeps formatting, leaves the code as is (no HTML formatting), and provides coloring similar to the SAS Enhanced Editor.

Kirito1
Quartz | Level 8
That's the question how can I just name a file using the date I specify. Sorry I think this is a bit confusing but is there a way where we %let to specify date and then just like called today(), I could call a date that I want with the file name to attach.
Kurt_Bremser
Super User

To store today's date in a macro variable, do

%let today = %sysfunc(today(),yymmddn8.);

If you want to use the date in code (for calculations or comparisons), omit the format in %SYSFUNC.

Kirito1
Quartz | Level 8
But, what if I want to store yesterday's date. Then how can do that?
Kurt_Bremser
Super User

SAS dates are counts of days, starting with 1960-01-01 as day zero.

So you only need to subtract a day from today:

%let yesterday = %sysfunc(putn(%sysfunc(today())-1,yymmddn8.));

Or you use a DATA step:

data _null_;
call symputx("yesterday",put(today()-1,yymmddn8.));
run;
Kirito1
Quartz | Level 8
Sorry to bug you so much, but what will we do if we want to store a date as crazy as 1999-10-24 with the file name while exporting it.
Kirito1
Quartz | Level 8
Supplied in another way, For example I am working on a task that was supposed to be submitted on so and so date but I am doing it today and want I want that the file name should consist the date when the file was supposed to be worked on.
Like I export a file with today() it will save file on my system with today's date but I want that file to be saved as 16-09-2022. Then I how could I do that.
Kurt_Bremser
Super User

Either you have a way to automatically calculate the date, or you need to write it literally into the code anyway.

I have already shown how to store a calculated date into a macro variable.

Kirito1
Quartz | Level 8
proc import datafile='xyz/IMPORT/TXN_DATA.csv'
out=CUSTAVG
DBMS=CSV
REPLACE;
run;
/*Defining yesterday as a function*/
%let yesterday = %sysfunc(putn(%sysfunc(today())-1,yymmddn8.));
/*Exporting a file*/
proc export data=CUSTAVG
outfile="xyz/Transactions \%sysfunc( yesterday(), date9 ).csv"
REPLACE
DBMS=CSV;
run;

Today() was working but if I use yesterday even after defining a macro Its giving me error.
Below Code was working fine and giving me desired results.
proc export data=AVG
outfile="xyz/Transactions \%sysfunc( today(), date9 ).csv"
REPLACE
DBMS=CSV;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 1456 views
  • 3 likes
  • 2 in conversation