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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: