12-02-2015 03:36 PM
I have a filename that has multiple files listed in it, like this example;
filename filesin ("%CMPRES(&folder\First File.txt)" "%CMPRES(&folder\Second File.txt)" "%CMPRES(&folder\Third File.txt)");
Then my datastep;
input @1 intext $200.;
The question is how can I set a variable in the datastep that contains the name of the input file?
* I found I needed the %CMPRES to allow Windows to get the correct name.
12-02-2015 03:40 PM
12-02-2015 03:44 PM
Correct me if I'm wrong but doesn't processing stop at the end of the smallest input file? Some of mine are long some are short but they are all different sizes.
12-02-2015 03:50 PM - edited 12-02-2015 04:09 PM
PS. I was trying to find an example, not just trying to be snarky
12-02-2015 04:08 PM - edited 12-02-2015 04:35 PM
Here's an example from the SAS Knowledge Base.
Add this line to after the infile statement to keep the file name. Note that all records are read, not just the smallest amount.
EDIT: Full code here, tested on SAS 9.3
/* Create external file EXTFILE1 */ data _null_; file 'c:\_localdata\temp\extfile1.txt'; put "05JAN2001 6 W12301 1.59 9.54"; put "12JAN2001 3 P01219 2.99 8.97"; put "16JAN2001 1 A00101 3.00 3.00"; put "19JAN2001 3 A00101 3.00 9.00"; put "24JAN2001 2 B90035 2.59 5.18"; run; /* Create external file EXTFILE2 */ data _null_; file 'c:\_localdata\temp\extfile2.txt'; put "02FEB2001 1 P01219 2.99 2.99"; put "05FEB2001 3 A00901 1.99 5.97"; put "07FEB2001 2 C21135 3.00 6.00"; put "14FEB2001 7 B90035 2.59 18.13"; put "20FEB2001 6 A00901 1.99 11.94"; put "27FEB2001 1 W12301 1.59 1.59"; put "27FEB2001 2 C00300 1.00 2.00"; put "28FEB2001 2 B90035 2.59 5.18"; run; /* Create the external file EXTFILE3 */ data _null_; file 'c:\_localdata\temp\extfile3.txt'; put "06MAR2001 4 A00101 3.59 14.36"; put "12MAR2001 2 P01219 2.99 5.98"; put "13MAR2001 2 A00101 3.00 6.00"; put "16MAR2001 3 B90035 2.59 7.77"; put "16MAR2001 1 W99201 5.50 5.50"; put "21MAR2001 3 C30660 2.00 6.00"; put "29MAR2001 5 A00901 1.99 9.95"; run; /* Path to files to be read are in the DATALINES. */ /* Each file is read in turn with the same INPUT statement. */ /* The END= variable is set to 1 each time the DATA step */ /* comes to the end of a file. */ /* */ /* Read the name of the file to be read from the DATALINES and */ /* store it in FIL2READ. The file is then read in the DO WHILE */ /* loop. At the end of the file, the DO loop ends, control */ /* passes back to the top of DATA step and the process starts */ /* over again until all files have been read. */ /* */ /* The argument "dummy" in the INFILE statement is a place- */ /* holder used in place of a file reference. */ data one; infile datalines; /* Ensure fully qualified path will fit in FIL2READ */ length fil2read $40; /* Input path of file to be read from DATALINES */ input fil2read $; infile dummy filevar=fil2read end=done; file_name=fil2read; do while(not done); /* Input statement for files to be read */ input @1 date date9. @11 quanity item $ price totcost; output; end; datalines; c:\_localdata\temp\extfile1.txt c:\_localdata\temp\extfile2.txt c:\_localdata\temp\extfile3.txt ; proc print data=one; run;
12-02-2015 05:43 PM
A complication is tht the file names have spaces in them. So for example one of them is
Empty Model Prelim
Should I enclose the names in tics (") to start with or how do I handle that?
I tried surrounding the names with ticks, that didn't work. Other ideas?
12-02-2015 05:45 PM
12-03-2015 09:51 AM
I got it to work like you see below. I get a lost card note in the log which I can't yet explain but I'm looking at it now.
If anyone sees a cleaner solution please let me know. And thank you everyone for the input.
%let folder = \\kcpublic\PUBLIC\Public\SAS\Costing Run Logs\Costing Run Logs 09-10\ ;
filename indsn "&folder";
infile datalines truncover;
length fil2read $150;
input fil2read $char150.;
fil2read = cats("&folder",fil2read);
infile dummy filevar = fil2read end = done;
file_name = fil2read;
do while(not done);
input @1 intext $char100.;
Empty Model Prelim Project Log.txt
Empty Model Previous Project Log.txt
Profman Containers to Cars Marketing Prelim Ordered List Project Log.txt
Profman Containers to Cars Marketing Previous Ordered List Project Log.txt
Profman Containers to Cars Prelim Ordered List Project Log.txt
Profman Containers to Cars Previous Ordered List Project Log.txt
Profman Output Marketing Prelim Data to SAS Prm Stg Project Log.txt
Profman Output to SAS PRM STG Prelim Project Log.txt
Profman Output to SAS Prm Stg Previous Project Log.txt
12-02-2015 03:54 PM
Some answers from this thread maybe relevant to you, @Reeza's suggestion was also mentioned.