Hello,
I have two separate .dat files. I need to import files by using DATA and INFILE statements.
Both files have same variables different values.
Is there a way for me to import two files so I can use same length, format options (reduce program size).
Then wrap the code into a macro? You can write a data step that handles this but a macro would be easier.
Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
Replace the inner code with working code for one data set and then it generalizes. Honestly, for just two files I'd probably copy and paste (rough guideline is if you repeat it 3 times or more, then generalize).
%macro import_dat_file(path=, out=);
data &out;
infile "&path" dsd truncover;
input ........;
run;
%mend;
%import_dat_file(path=/pathtofile1/myfile1.dat, out=data1);
%import_dat_file(path=/pathtofile1/myfile2.dat, out=data1);
Yes.
If the names are similar (and unique) just use a wildcard in the filename.
infile 'myfiles*.dat' ....
Define a fileref that points to both files and use the fileref in the INFILE statement.
filename mydata ('file1.dat','file2.dat') ;
...
infile mydata ...
Or create a dataset with the list of files and use the FILEVAR= option on the INFILE statement. So if you have a dataset name FILELIST with a variable named FILENAME then use code like:
data want;
set filelist ;
infile dummy filevar=filename end=eof;
do while (not eof);
input .... ;
...
output;
end;
run;
I have two separate .dat files and need to create two separate dataset.
both .dat files have different name
both dataset needs to have different name.
Then wrap the code into a macro? You can write a data step that handles this but a macro would be easier.
Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
Replace the inner code with working code for one data set and then it generalizes. Honestly, for just two files I'd probably copy and paste (rough guideline is if you repeat it 3 times or more, then generalize).
%macro import_dat_file(path=, out=);
data &out;
infile "&path" dsd truncover;
input ........;
run;
%mend;
%import_dat_file(path=/pathtofile1/myfile1.dat, out=data1);
%import_dat_file(path=/pathtofile1/myfile2.dat, out=data1);
For sure!
However there are some things to consider:
Rough outline is here:
@dht115 wrote:
Hello,
I have two separate .dat files. I need to import files by using DATA and INFILE statements.
Both files have same variables different values.
Is there a way for me to import two files so I can use same length, format options (reduce program size).
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.