Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing A Filename Into A Dataset

Status
Not open for further replies.

tubbsy123

Programmer
Dec 2, 2004
19
AU
Hi,

I'm having problems with some code. I read in 6 text files using a wildcard. There are many more files in the root folder that are not read in.

I need to write the filename of each of the files that are read in into a new column in the dataset.

Any help would be greatly received.

The code I used to read in the files is below:

-----------------------------------------------------------
dm log 'clear' output;

%let month=JAN09;
%let rootfolder=Y:\Tubbsy\;
%let staging=C:\Temp\Lookup\;
%let folder=SCT;
%let dlimit=',';
libname tmploc "&staging&folder";

data tmploc.lookup;
infile "&staging&folder\*.TXT" dsd truncover dlm=&dlimit;
length temp $1 pcode1 $5 pcode2 $5;
input temp$ pcode1$ pcode2$;

postcode=trim(pcode1)||' '||trim(pcode2);
drop temp;
run;

-----------------------------------------------------------

Thanks in advance.

Regards

Tubbsy123
 
Easy peasy. :) There's an infile statement option called filename= which you use to specify a temporary variable which will hold the full path of the file being read in. You can then use the scan function to get just the filename part of it.
Note - You need to specify the length of the temporary variable as a long path will get truncated.

Code:
data tmploc.lookup;
    length flnm $500 filename $50;
    infile "&staging&folder\*.TXT" dsd truncover dlm=&dlimit filename=flnm;
    length temp $1 pcode1 $5 pcode2 $5;
    input temp$ pcode1$ pcode2$;

    filename = scan(flnm,-1,'/');

    postcode=trim(pcode1)||' '||trim(pcode2);
    drop temp;
run;

That should do the trick for you. You may need to change the '/' in the scan function depending on your operating system. Bear in mind that flnm will be a temporary variable, and is therefore NOT kept in the output dataset.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Hi Chris,

This is really good but seems to only allow me identify the location of the file rather than the actual file name, eg Data20090301.txt

Can this code be adapted to allow for the actual filename to be output.

Regards

Tubbsy123
 
It should give the full path...
Try adding this line in to see it...
Code:
  fullpath = flnm;

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top