Desktop productivity for business analysts and programmers

Generating unique filename that includes a date

Reply
N/A
Posts: 0

Generating unique filename that includes a date

The following code runs daily in batch and writes today's date to the file 'd:\data\errors.txt'. What I want to do is to change the name filename each day to the date literal. Thus, today the filename would be 'd:\data\061117.txt' and tomorrow the filename would be d:\data\061118.txt'. How can I do this?


data _null_;
file 'd:\data\errors.txt' notitles;
today=put(today(),yymmdd7.);
put today;
run;
SAS Super FREQ
Posts: 8,820

Re: Generating unique filename that includes a date

Hi!
One thing you could do is use the FILEVAR option on the FILE statement. This allows you to use a variable from your program (like TODAY) to set the name of the file being created with the FILE/PUT statements. A lot of times, people use this to create a bunch of differently named files and not just one, but it works for just one file.

Try this:
[pre]
data _null_;
length dsnname $100;
today=put(today(),yymmdd7.);
dsnname = compress('c:\temp\'||today||'.txt');
file phold filevar=dsnname notitles;
put today;
run;
[/pre]

On the FILE statement, the fileref 'PHOLD' is just a placeholder that needs to be there to make the FILE statement work correctly. In the statement where the variable DSNNAME is being created, the COMPRESS function is used to remove all leading and trailing blanks from the string. In addition, the CONCATENATE operator (||) is used to stick strings together -- like the path, today and '.txt'.
cynthia
N/A
Posts: 0

Re: Generating unique filename that includes a date

Cynthia,

Thankyou. Now I would also like to use the same technique in a PROC EXPORT step. Can you explain how I can replace the 'ss07mod2' filename with today's date?

PROC EXPORT DATA= model1out
OUTFILE= "D:\SAS\Model1\Weekly_Model1_Extract\ss07mod2.csv"
DBMS=CSV REPLACE;
RUN;
SAS Super FREQ
Posts: 8,820

Re: Generating unique filename that includes a date

In this instance, there are 2 separate methods...one is to use %LET in open code to set a macro variable to the name and the other is to put your EXPORT program (and perhaps other steps into a SAS Macro program). I'll show you the %LET method.
[pre]
%let fname = %sysfunc(today(),yymmdd7.);
%put =====> fname= &fname;

PROC EXPORT DATA= sashelp.class
OUTFILE= "c:\temp\&fname..csv"
DBMS=CSV REPLACE;
RUN;
[/pre]

The %LET statement is invoking the SYSFUNC macro function to create a macro variable called FNAME. SYSFUNC allows you to use data step functions outside of a data step program (like in a TITLE statement or a %LET statement). Once the FNAME macro variable is created, the %PUT just echoes the value of the macro variable in the LOG so you can see that it worked.

Finally, the reference to the macro variable is used in the OUTFILE= option. There's only 1 tricky thing here -- if I had written:

OUTFILE="c:\temp\&fname.csv" then that would have been wrong because the result would be a file named: c:\temp\061128csv (the first dot (.) is a delimiter that indicates the boundary of the macro variable). So that's why I need to have 2 dots (..) in my file name:

OUTFILE="c:\temp\&fname..csv" where one dot (.) acts as the boundary or delimiter for the macro variable reference and the other dot (.) just becomes part of the file name. The only other important thing is that you MUST use double quotes if you want the macro variable reference to resolve correctly. Single quotes will NOT work.

If you need more help understanding how SAS macro usage can help you, Tech Support is a great resource -- especially if you're trying to figure out how to put together a whole set of programs using the same macro variable references. Good luck!
cynthia
N/A
Posts: 0

Re: Generating unique filename that includes a date

I hesitate to "guild the lilly" of a solution from Cynthia, but it just seemed an opportunity to describe the "one-line" solution approach for comparison.
A datastep provides an environment for processing information and comes with many features tuned for multiple rows. For a single piece of information the %sysfunc() environment is well-tuned. The ususal demo supplies a result to a macro variable and used that, like Cynthia's

%let fname = %sysfunc(today(),yymmdd7.);

OUTFILE= "c:\temp\&fname..csv"

However, the sas system allows that %sysfunc() expression to be built into most of its code environments, which allows a "one-line" solution for HuddersfieldTown, like:

PROC EXPORT DATA= model1out
OUTFILE= "D:\SAS\Model1\Weekly_Model1_Extract\%sysfunc( today(), yymmdd7 ).csv"
DBMS=CSV REPLACE;
RUN;

where you might notice no double-dots, because there is no macro variable.

PeterC
SAS Super FREQ
Posts: 8,820

Re: Generating unique filename that includes a date

I agree, the one-line solution is more elegant than what I posted. In fact, the one-line solution is the way I would code it if I were doing this for myself. But, in the forum, I have no way to know the expertise level of the person posting the question. So, I tend to be conservative and show steps that I can break down into smaller pieces (and -prove- that each separate piece worked -- hence the %put after the %let). It's the curse of teaching -- needing to show the steps to the solution.
cynthia
N/A
Posts: 0

Re: Generating unique filename that includes a date

Replacing the export file name with today's date via macro variables. The call symput function allows you to create/update macro variable values from data step code. It is similar to %let in open code.

data _null_;
length dsnname $100;
today=LEFT(put(today(),yymmdd7.));
call symput('today_macro_var',TRIM(today));
dsnname = compress('c:\temp\'||today||'.txt');
file phold filevar=dsnname notitles;
put today;
run;

PROC EXPORT DATA= sashelp.class
OUTFILE= "c:\&today_macro_var..csv"
DBMS=CSV
REPLACE
;
RUN;
Ask a Question
Discussion stats
  • 6 replies
  • 5156 views
  • 1 like
  • 2 in conversation