BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hey folks,
I have a simplified version of my program to create dynamically named files:

data _NULL_;
length fName $10;
do i = 1 to 3;
fName = strip("test") || strip(i) || strip(".txt");
putlog "FNAME = " fName;

if (i > 1) then do;
fid = filename('out',"");
end;
fid = filename('out', strip(fName));

file out lrecl=20 notitle noprint;
put "Testing";
put i;
end;
run;

It results in the following output:

1 data _NULL_;
2 length fName $10;
3 do i = 1 to 3;
4 fName = strip("test") || strip(i) || strip(".txt");
5 putlog "FNAME = " fName;
6
7 if (i > 1) then do;
8 fid = filename('out',"");
9 end;
10 fid = filename('out', strip(fName));
11
12 file out lrecl=20 notitle noprint;
13 put "Testing";
14 put i;
15 end;
16 run;

NOTE: Numeric values have been converted to character
values at the places given by: (Line):(Column).
4:36
NOTE: The file OUT is:
Filename=\\Dgrfs-0\pgms\tsb\upid\upid7_html\out.dat,
RECFM=V,LRECL=20,File Size (bytes)=0,
Last Modified=13May2010:12:27:56,
Create Time=13May2010:11:14:25

FNAME = test1.txt
FNAME = test2.txt
FNAME = test3.txt
NOTE: 6 records were written to the file OUT.
The minimum record length was 1.
The maximum record length was 7.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.01 seconds


The ideal result is that out would equal (at each iteration) test1.txt, test2.txt, and test3.txt, each new file containing "Testing" and their iteration number.

What I get instead is out.dat holding:
Testing
1
Testing
2
Testing
3


Am I using FILE or FILENAME improperly? Is there a way to create multiple dynamically named files in the data step? (due to the method of naming for the end program's files, they must be created in a data step, and not a macro).

Thanks much!
-Jim
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
You need to look at the FILE statement and using the FILEVAR= keyword to dynamically change the target/output file within a DATA step. Check the SAS DOC and also the SAS forum archives for prior posts on FILEVAR.

Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
Thank you very much! That worked excellently. It was exactly what I needed 😄

-Jim
Cynthia_sas
SAS Super FREQ
Hi:
Using the FILEVAR option of the FILE statement would make your life MUCH easier. This example is a slightly different example, but shows creating 3 files named:
[pre]
c:\temp\wombat1.txt
c:\temp\wombat2.txt
c:\temp\wombat3.txt
[/pre]

and THEN, in a second program, adds some information to the files created in the first program. The key is the use of the FILEVAR option of the FILE statement.
Documentation:
http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000171874.htm

Specifically, my example is a modification of the documentation example #5, entitled, "Dynamically Changing the Current Output File".

cynthia
[pre]
** First method create the files new each time;
data _null_;
length namevar $200;
do i = 1 to 3;
Namevar = cats('c:\temp\','wombat',put(i,1.0),'.txt');
file out filevar=namevar;
put "Twas brillig and the slithy toves";
put i;
end;
run;

** Now Add to the file created above with the MOD option;
** Note that the NAMEVAR needs to be the same in both programs;
** for this to work;
data _null_;
length namevar $200;
do i = 1 to 3;
Namevar = cats('c:\temp\','wombat',put(i,1.0),'.txt');
file out filevar=namevar mod;
put "Did gyre and gimble in the wabe.";
put i;
end;
run;

[/pre]

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
  • 3 replies
  • 2708 views
  • 0 likes
  • 3 in conversation