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

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

2 REPLIES 2
Tom
Super User Tom
Super User

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;
MillerEL
Obsidian | Level 7
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!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 917 views
  • 1 like
  • 2 in conversation