BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dht115
Calcite | Level 5

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). 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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);

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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;
dht115
Calcite | Level 5

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. 

Reeza
Super User

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);
Reeza
Super User

For sure!

 

However there are some things to consider:

  • Headers in each file?
  • How do you know which record a file comes from - do you need this information?
  • Do you need separate files for each file or a single file?

Rough outline is here:

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

 


@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). 

 

 


 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 1454 views
  • 0 likes
  • 3 in conversation