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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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