BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Emma2021
Quartz | Level 8
I am using proc infile to get multiple text files into sas (filename all (“&path.file1.txt” “&path.file2.txt”),

How can I get the last string (1 and 2 from file names and create as new variable “filenumber” while reading the all files into sas?
Thank you.
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Use the FILENAME option to capture the name of the file being read into a variable and then you can parse the variable as a text string. 

 

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

 

data import_all;
 
*make sure variables to store file name are long enough;
length filename txt_file_name $256;
 
*keep file name from record to record;
retain txt_file_name;
 
*Use wildcard in input;
infile "Path\*.txt" eov=eov filename=filename truncover;
 
*Input first record and hold line;
input@;
 
*Check if this is the first record or the first record in a new file;
*If it is, replace the filename with the new file name and move to next line;
if _n_ eq 1 or eov then do;
input_file = filename;
txt_file_name = scan(filename, -1, "\");
eov=0;delete;
end;
 
*Otherwise  go to the import step and read the files;
else input
 
*Place input code here;
 
;
run;

Note that the variable is not saved to the data set automatically, you do need to save it explicitly though it's available for processing in the data step.

 


@Emma2021 wrote:
I am using proc infile to get multiple text files into sas (filename all (“&path.file1.txt” “&path.file2.txt”),

How can I get the last string (1 and 2 from file names and create as new variable “filenumber” while reading the all files into sas?
Thank you.

 

View solution in original post

2 REPLIES 2
Reeza
Super User

Use the FILENAME option to capture the name of the file being read into a variable and then you can parse the variable as a text string. 

 

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

 

data import_all;
 
*make sure variables to store file name are long enough;
length filename txt_file_name $256;
 
*keep file name from record to record;
retain txt_file_name;
 
*Use wildcard in input;
infile "Path\*.txt" eov=eov filename=filename truncover;
 
*Input first record and hold line;
input@;
 
*Check if this is the first record or the first record in a new file;
*If it is, replace the filename with the new file name and move to next line;
if _n_ eq 1 or eov then do;
input_file = filename;
txt_file_name = scan(filename, -1, "\");
eov=0;delete;
end;
 
*Otherwise  go to the import step and read the files;
else input
 
*Place input code here;
 
;
run;

Note that the variable is not saved to the data set automatically, you do need to save it explicitly though it's available for processing in the data step.

 


@Emma2021 wrote:
I am using proc infile to get multiple text files into sas (filename all (“&path.file1.txt” “&path.file2.txt”),

How can I get the last string (1 and 2 from file names and create as new variable “filenumber” while reading the all files into sas?
Thank you.

 

Tom
Super User Tom
Super User

The easiest way is to add the FILENAME= option to your INFILE statement (infile is not a proc).

To know when you are changing files the easiest way is to just check if the filename has changed.

So to count the number of files being read you could do something like this.

The FNAME variable is the one that the INPUT statement will update.  But it is not written to the output dataset, so you can copy it into another  variable, FILENAME in this case. The FILENO variable will increment each time a new file starts. Because of the sum statement its value will be retained.  Depending on how you are using it you might not need the INPUT statement, but you need to test the FNAME variable after you have started reading from the new file so here it just makes sure we are getting the name of the current file and not the previous one.

data want;
  length filename fname $256;
  infile all filename=fname ;
  input @;
  filename=fname;
  fileno + (lag(filename) ne filename);
  .... rest of code to read the file ;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 632 views
  • 2 likes
  • 3 in conversation