Hi there,
I am struggling to automate file names for new text files i am creating. i would like to name the files as follows:
Program1_260122.txt
I have my todays date
/*Get todays date*/ data _null_; cdate=today(); cdate = today(); call symputx('cdate', put(cdate, ddmmyyn8.)); run; %put &cdate.;
Now I am stuck where it must create the text files with the name 'Program1' in front of the date. I tried up to here:
data tsave; do i = 1 to 5; y = "Program" + i; output; end; run;
If you want to create a file name in the do loop of the data step, you can write the following
data tsave;
do i = 1 to 5;
y = cats("Program",i,"_&cdate..txt");
output;
end;
run;
If you want to create a file name in the do loop of the data step, you can write the following
data tsave;
do i = 1 to 5;
y = cats("Program",i,"_&cdate..txt");
output;
end;
run;
I STRONGLY recommend to use a YMD order for dates in filenames, as they will then sort correctly in time order, and to ALWAYS use 4-digit years.
So you should get your date in a single %LET with this:
%let cdate = %sysfunc(today(),yymmddn8.);
I also recommend to use a format for your running count that accommodates the maximum number and adds leading zeroes, so program 11 won't come before program 2.
data tsave;
do i = 1 to 5;
y = cats("Program",put(i,z2.),"_&cdate..txt");
output;
end;
run;
hi
thank you
Variable passes are not working when I pass it into my filename:
/*Get todays date*/ data _null_; cdate=datetime(); call symputx('cdate', put(cdate, datetime20.)); run; %put &cdate.; data test; %let i = 1 ; %let filename = cats("&i","_&cdate."); proc export data=files outfile="/C:/'&filename'.txt" dbms=tab; run;
You cannot run PROCEDURE steps inside a DATA step. The PROC keyword constitutes a step boundary and immediately ends the DATA step.
It seems that you want to automatically export a series of datasets to a series of text files? If yes, please supply examples for the dataset names and text file names. We need to see the whole picture to give you proper help.
At the beginning of your process, do you start with an empty directory (and an empty main.txt), or will there already be files in it?
What data is written to the new files?
This code (tested on my SAS On Demand account) creates 5 files and a main.txt containing the filenames:
%let cdate = %sysfunc(today(),yymmddn8.);
data tsave;
length y fname $200;
do i = 1 to 5;
y = cats("~/Program",put(i,1.),"_&cdate..txt");
fname = y;
output;
file dummy filevar=y;
end;
run;
data _null_;
file "~/main.txt";
set tsave;
put fname;
run;
Please study my code with care, especially the use of the FILEVAR= option to dynamically create files.
My code creates 5 files and the master file, you just need to change the path in the CATS function so it works on Windows.
Windows version of my code:
%let cdate = %sysfunc(today(),yymmddn8.);
data tsave;
length y fname $200;
do i = 1 to 5;
y = cats("C:\Program",put(i,1.),"_&cdate..txt");
fname = y;
output;
file dummy filevar=y;
end;
run;
data _null_;
file "C:\main.txt";
set tsave;
put fname;
run;
Add additional path elements as needed. If this does not work as expected, please post the complete log into a window opened with this button:
Thank you. This works well. I'm now trying to add the name of the file in a macro variable so that the file can then be created with this name via a macro. Not sure how to do that
How do you create a single file, and which parts of that code need to be made dynamic? Please post the whole code.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.