Help using Base SAS procedures

Simplified creation of files with dynamic filenames.

Reply
N/A
Posts: 0

Simplified creation of files with dynamic filenames.

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)Smiley SadColumn).
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
Super Contributor
Super Contributor
Posts: 3,174

Re: Simplified creation of files with dynamic filenames.

Posted in reply to deleted_user
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.
N/A
Posts: 0

Re: Simplified creation of files with dynamic filenames.

Thank you very much! That worked excellently. It was exactly what I needed Smiley Very Happy

-Jim
SAS Super FREQ
Posts: 8,868

Re: Simplified creation of files with dynamic filenames.

Posted in reply to deleted_user
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]
Ask a Question
Discussion stats
  • 3 replies
  • 130 views
  • 0 likes
  • 3 in conversation