BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi all,
I have 500 data files in fixed ASCII format. They are named from s1-s500. Now I need to average some variables in each of the data file and then export them in fixed ASCII format with the same file name. I want to use "do" loop to go through the 500 files. For example, I let n=1 to 500. Then I transfer "n" from numeric to character. My question is, how can I put "s" and the number "n" stands for together to designate the data file? What is the code for doing this?
Thanks!
Malena
8 REPLIES 8
deleted_user
Not applicable
Sorry the title is acturally wrong.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Hello,

I agree the SUBJECT is unrelated to the post content, so I will focus on the post content, not the SUBJECT.

The code below demonstrates how to use the PUT function with assigning a SAS variable, representing your input and output file names. SAS code below uses the DO LOOP technique but without requiring macro language, only using SAS DATA step language to generate the SAS code.

You have some choices with SAS -- depending on your SAS expertise using DATA step and MACRO language to generate (and execute) SAS code using SAS code. Here is one technique that does not require any MACRO language.
Obviously you will need to fill-in your data-input and data-output coding portions.

Suggest you take this template example, add your own code for the INPUT and OUTPUT processing, and come back to the discussion forum if there are any more questions -- very important to share what code you have in order to get the most valuable and meaningful feedback.


Scott Barry
SBBWorks, Inc.

* One technique using DATA step logic to generate SAS code. ;
* No MACRO coding involved -- for some, a good thing to consider. ;

filename tempsas temp; /* temp file to store instream generated SAS code */
data _null_;
file tempsas;
do i=1 to 500;
* build input file name string - demo shows suffix # 1 to 500, as in: ;
* c:\data\InputFile_500.txt for illustration purposes only. ;
length input_file output_file $255;
input_file = "c:\data\DetailInputFile_" !! put(i,3. -l) !! ".txt";
output_file = "c:\data\MEANSOutputFile_" !! put(i,3. -l) !! ".txt";
put "filename MyInputData " input_file "; " /
"DATA _your_sas_file_;" /
"INFILE MyInputData;" /
"* more DATA step code goes here; " /
"RUN;" /
"* Maybe PROC MEANS step code goes here; " /
"RUN;" /
"filename MyOutputData " output_file "; " /
"DATA _NULL_;" /
"FILE MyOutputData;" /
"* DATA step code for PUT to flat file goes here; " /
"RUN;" ;
end;
stop;
run;
* now include the SAS code generated in the DATA step above. ;
%include tempsas;
deleted_user
Not applicable
Thank you so much! I am working on my code based on what you suggested and will post my code when I am done.
deleted_user
Not applicable
you should not need a DO loop to go through these files.
The infile statement accepts a "global character" * in the physical name, like[pre] infile "c:\data\InputFile_*.txt" ..... [/pre] The file statement also supports a way of writing multiple files.
The file statement option FILEVAR= names a variable that holds the name of the file you want to write, like [pre] data _null_ ;
length phys_name output_nm $1000 ;
retain eov 0 ;
infile "c:\data\InputFile_*.txt" filename= phys_name eov=eov ;
input @22 var1 12. @44 var2 12. ;
* supply your own input definitions ;
s_v1 + var1 ;
s_v2 + var2 ;
n_v1 +n(var1) ;
n_v2 +n(var2) ;
if eov then do;
* having come to end of one of the files,
average and output ;
output_nm = trim( phys_name) !! 'out.txt' ;
file dumo filevar= output_nm lrecl=10000 ;
if s_n1 then a_v1=s_v1/n_v1 ; else a_v1= .;
if s_n2 then a_v2=s_v2/n_v2 ; else a_v2= .;
put @22 a_v1 12. @44 a_v2 12. ;
* or whatever you need on output ;
eov = 0 ; * prepare for next file ;
end ;
run ;
deleted_user
Not applicable
Hi Peter_c,
I have a question about your code. In this row "file dumo filevar= output_nm lrecl=10000 ;", should I write a path where I want to save the output data after "file"? I tried to write a path, but I always got error message like "ERROR: A Physical file reference (i.e. "PHYSICAL FILE REFERENCE" ) or an aggregate file storage reference (i.e. AGGREGATE(MEMBER) ) reference cannot be used with the FILEVAR= option."
I also attached my code as below. Thank you for your help!

data _null_;
length phys_name output_name $50;
retain eov 0;
infile 'G:\s1*.dat'
filename= phys_name eov=eov;
input x1 1-12 x2 13-25 x3 26-38 x4 39-51 x5 52-64 x6 65-77 x7 78-90 x8 91-103 x9 104-116 x10 117-129 x11 130-142 x12 143-155 group $ 158;
c1=(x1+x2+x3)/3;
c2=(x4+x5+x6)/3;
c3=(x7+x8+x9)/3;
c4=(x10+x11+x12)/3;
drop x1-x12;
if eov then do;
output_name= trim(phys_name) !! '.dat';
file 'C:\output_name' filevar=output_name lrecl=10000 ;
put @1 c1 12.6
@13 c2 12.6
@25 c3 12.6
@37 c4 12.6
@51 group 12.0;
eov=0;
end;
run ;
deleted_user
Not applicable
with FILEVAR= there cannot also be a physical "filepath/name" in the file statement.

FILEVAR= provides a way of providing a "derived value" for the physical name, which is the theory behind the original question (I thought).
As one might expect, the "derived value" is stored in a variable, and it is the name of this variable to which FILEVAR points.

Because the physical name is not provided as a constant in the file statement, that variation of the file statement does not apply. Instead the compiler expects you to use the FILEREF style of file statement. That fileref does not need to "pre-exist" and will not be created or saved by the data step. As the documentation puts it, "when you use the FILEVAR= option, the fileref is simply a placeholder".

Try the online doc, like http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000171874.htm .


PeterC
deleted_user
Not applicable
Hi Peter_c,
I have been working on the code for a while but I still couldn't figure out the correct code. Now I can sucessfully "input" the original data, but I don't know how to output them in the same format with the same file name.
The code for outputting the file is listed as below. What I want to do is to output the 500 datasets, namely, s1-s500 in the folder "c:\". What should I correct in the code?
Thank you so much!

if eov then do;
output_name= trim(phys_name) !! '.dat';
file 'C:\output_name' filevar=output_name lrecl=10000 ;
put @1 c1 12.6
@13 c2 12.6
@25 c3 12.6
@37 c4 12.6
@51 group 12.0;
eov=0;
end;
run ;
deleted_user
Not applicable
change the output statement from

file 'C:\output_name' filevar=output_name lrecl=10000 ;

to
file dfref filevar=output_name lrecl=10000 ;

The physical name comes from variable
output_name
and not from the file statement

PeterC

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 8 replies
  • 1907 views
  • 0 likes
  • 2 in conversation