DATA Step, Macro, Functions and more

macro for multiple text files, how to assign filename to each file

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

macro for multiple text files, how to assign filename to each file

[ Edited ]

Hello,

 

I am using a macro to read in 12 txt files. Each file corresponds to a month, but there is no variable within the file that reflects the month - this information is only in the filename. I need help figuring out how to extract the month from the file name within the macro. Each file is named tan<mon>yy. I haven't been able to apply any of the methods I've seen for extracting file names to my code below. Any ideas on how to do this?

 

filename tanf1 'Q:/txtfiles/tan*13.txt' lrecl=80 recfm=v;

%macro tanfin;
	input @1 relind $char1. @3 casdcn $char8. @12 indvdcn $char8.
  		 @21 county $char3. @25 zip $char5. @31 rac $char1. @33 sx 
                 $char1. @35 agen 3. @39 nmon 4. @46 edu $char2.;
%mend tanfin;

DATA one;
	infile tanf1;
	%tanfin;
run;
quit;

 

 


Accepted Solutions
Solution
‎09-18-2017 02:39 PM
Super User
Super User
Posts: 7,845

Re: macro for multiple text files, how to assign filename to each file

[ Edited ]

Not sure why you have coded that input statement as a macro.

Use the FILENAME= option on the INFILE statement to have SAS tell you want file the current record came from.

filename tanf1 'Q:/txtfiles/tan*13.txt' lrecl=80 recfm=v;

data all;
  length fname $256 filename $32 ;
  infile tanf1 filename=fname truncover;
  input @1 relind $char1. @3 casdcn $char8. @12 indvdcn $char8.
        @21 county $char3. @25 zip $char5. @31 rac $char1. 
        @33 sx $char1. @35 agen 3. @39 nmon 4. @46 edu $char2.
  ;
  filename = scan(fname,-2,'./\');
run;

View solution in original post


All Replies
Solution
‎09-18-2017 02:39 PM
Super User
Super User
Posts: 7,845

Re: macro for multiple text files, how to assign filename to each file

[ Edited ]

Not sure why you have coded that input statement as a macro.

Use the FILENAME= option on the INFILE statement to have SAS tell you want file the current record came from.

filename tanf1 'Q:/txtfiles/tan*13.txt' lrecl=80 recfm=v;

data all;
  length fname $256 filename $32 ;
  infile tanf1 filename=fname truncover;
  input @1 relind $char1. @3 casdcn $char8. @12 indvdcn $char8.
        @21 county $char3. @25 zip $char5. @31 rac $char1. 
        @33 sx $char1. @35 agen 3. @39 nmon 4. @46 edu $char2.
  ;
  filename = scan(fname,-2,'./\');
run;
Occasional Contributor
Posts: 14

Re: macro for multiple text files, how to assign filename to each file

I coded it as a macro, because I'm used to working with larger and more varied file layouts that have to be combined. Obviously, I was overthinking - I've never seen this approach - but it worked like a charm! Thank you!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 105 views
  • 1 like
  • 2 in conversation