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;
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;
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;
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!
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.