I am struggling to automate file names for new text files i am creating. i would like to name the files as follows:
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;
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;
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:
How do you create a single file, and which parts of that code need to be made dynamic? Please post the whole code.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.