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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

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;

View solution in original post

13 REPLIES 13
japelin
Rhodochrosite | Level 12

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;
Kurt_Bremser
Super User

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;

 

Citrine10
Obsidian | Level 7

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;
Kurt_Bremser
Super User

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.

Citrine10
Obsidian | Level 7
the only reason I added the dataset was because I received an error saying:Syntax error, expecting one of the following: ;, DATA, DBLABEL, DBMS, DEBUG, FILE, LABEL, OUTFILE, OUTTABLE, REPLACE,
TABLE, _DEBUG_.

What I am trying to achieve is the following, it is a bot complex:
Have a main text file which contains a list of all 5 text files in a directory, lets call it main.txt.
Thereafter text files need to be created using SAS with naming convention Program1_todaysdatetime.I am allowed to create up to 5 of these files thus up to Program5_todaysdatetime and everytime this file gets created, it needs to be recorded in main.txt.

What needs to happen is I need to count number of records in main.txt. If this count is less than 5 then we must create a new text file and populate the name of this new text file into the main text file. This loop needs to continue.

So far I got to the point where is counts the number of records in main.txt and now I am trying to automate the rest.

/*Count records*/
data files;
length fname $256 lines 8 cmd $300;
infile "ls &path.&pattern" pipe truncover ;
input fname $100.;
cmd = catx(' ','wc -l',fname);
infile cmd pipe filevar=cmd truncover;
input lines @;
run;

proc sql;
select lines into :linecount from files;
quit;
Kurt_Bremser
Super User

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?

Citrine10
Obsidian | Level 7
Hi Kurt, empty dir and empty main.txt.
The new files dont need to have data written in them
Kurt_Bremser
Super User

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;
Citrine10
Obsidian | Level 7
thank you Kurt. I have used the code and it works well. Now I want to create the 5 files with there respective names. however the files are not creating properly and it seems its because of the way I have placed my &fname variable in the path

%let cdate = %sysfunc(datetime(), datetime20.);

data t;
length y fname $300;
do i = 1 to 5;
y = cats("Program",put(i,1.),"_&cdate..txt");
fname = y;
file "/C:/&fname.txt";
output;
end;
run;
Kurt_Bremser
Super User

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.

Kurt_Bremser
Super User

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:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

Citrine10
Obsidian | Level 7

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 13 replies
  • 2210 views
  • 0 likes
  • 3 in conversation