Hi Guys,
I'm trying to export a file and bring in yesterdays date into the file name.
Traditionally, we name files using the extract date- but we've been asked to do something differently this time.
Here is my code:
proc export dbms=csv
data=WORK.data
outfile="S:\D\D\ &sysdate..csv"
replace
label;
run;
How can I dynamically do this? ie &sysdate-1 for example.
so if I ran the file today, the file name would be:
15Apr14.csv
Thanks in advance
Try
%PUT %SYSFUNC(PUTN(%sysevalf(%SYSFUNC(TODAY())-1),DATE9.));
How would that look in my program?
proc export dbms=csv
data=WORK.data
%PUT %SYSFUNC(PUTN(%sysevalf(%SYSFUNC(TODAY())-1),DATE9.));
outfile="S:\D\D\ &sysdate..csv"
replace
label;
run;
??
I'm pretty new to coding in SAS
Actually, I worked it out...
%PUT date = %SYSFUNC(PUTN(%sysevalf(%SYSFUNC(TODAY())-1),DATE9.));
proc export dbms=csv
data=WORK.data
outfile="S:\D\D\DData_&date..csv"
replace
label;
run;
Yep you are right, you can even supply your own values also.
In your program you are referencing the value of a macro variable by using &SYSDATE.
You could use also reference a macro function call. In particular the %SYSFUNC() macro function that lets you reference anyone of many SAS functions in macro code.
proc export
dbms=csv
data=WORK.data
outfile="S:\D\D\%sysfunc(putn("&sysdate9"d-1,date.)).csv"
replace
label
;
run;
There are a couple of things to watch out for here.
1) I changed from using SYSDATE (which is formatted with a two digit year) to SYSDATE9 (which is the same value formatted with a four digit year).
2) I used the date literal syntax "ddMMMyyyy"d to convert the string that is in the SYSDATE9 macro variable back into a valid SAS date value and then subtracted one to get "yesterday".
3) I used the DATE format so that the value will look like what you used before (15APR14) but I strongly recommend using a flavor of YYMMDD format when putting date strings into filenames. These values will sort naturally into date order when sorted as character strings. If you use ddMMMyy format then the order in your directory of files will look almost random.
4) Note that SYSDATE is the date that SAS started and might not be today's date. If the session starts right before midnight or has been running a long time. If this is being run by users from interactive sessions then you are better off using the DATE(), or its alias TODAY(), function instead.
5) You could create a macro variable with the formatted date of choice and then the coding is a little clearer.
%let yesterday=%sysfunc(putn(%sysfunc(date())-1,yymmdd10));
proc export
dbms=csv
data=WORK.data
outfile="S:\D\D\&yesterday..csv"
replace
label
;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.