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?
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.
* 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 */
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; " /
"* Maybe PROC MEANS step code goes here; " /
"filename MyOutputData " output_file "; " /
"DATA _NULL_;" /
"FILE MyOutputData;" /
"* DATA step code for PUT to flat file goes here; " /
* now include the SAS code generated in the DATA step above. ;
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 ;
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!
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".
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;