BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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;
8 REPLIES 8
Cynthia_sas
SAS Super FREQ
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
deleted_user
Not applicable
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;
Cynthia_sas
SAS Super FREQ
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
deleted_user
Not applicable
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
Cynthia_sas
SAS Super FREQ
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
couthelle
Fluorite | Level 6

Hello Cynthia,

Could you also please provide advice on how I can fix my code? 

This is my code:

 

%let date = %sysevalf('31mar2020'd);

data _null_;
	call symput('z', put(&date, monyy5.));
run;

proc export data=sashelp.cars
outfile='/team_folder/&z.xlsx'
dbms=xlsx
replace;
run;

After running the code the filename of the saved file is &z.xlsx instead of MAR20.xlsx.

 

Appreciate your help.

 

Thanks,

Elle

 

PaigeMiller
Diamond | Level 26

Best not to ask new questions in a 17 year old thread.

 

In this case, the answer is simple: use double quotes around any text that includes macro variables. However file names that have dates such as MAR20 is a bad idea, because these will not sort properly, you will find that APR is the first month and AUG is the second month and so on. Better to use 2020M03 or something like that.

 

Next time, start a new thread.

--
Paige Miller
deleted_user
Not applicable
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;

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 8 replies
  • 22396 views
  • 3 likes
  • 4 in conversation