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

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

1 ACCEPTED SOLUTION

Accepted Solutions
SKK
Calcite | Level 5 SKK
Calcite | Level 5

Try

%PUT  %SYSFUNC(PUTN(%sysevalf(%SYSFUNC(TODAY())-1),DATE9.));

View solution in original post

5 REPLIES 5
SKK
Calcite | Level 5 SKK
Calcite | Level 5

Try

%PUT  %SYSFUNC(PUTN(%sysevalf(%SYSFUNC(TODAY())-1),DATE9.));

breelloyd
Fluorite | Level 6

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

breelloyd
Fluorite | Level 6

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;

SKK
Calcite | Level 5 SKK
Calcite | Level 5

Yep you are right, you can even supply your own values also.

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 12326 views
  • 0 likes
  • 3 in conversation