DATA Step, Macro, Functions and more

How to create a text file that has file names in a directory

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

How to create a text file that has file names in a directory

In my "D:\work\" folder, I have two types of files, that are "8-digit-number_type.dbf" and "character_type.dbf" as follows: 00093282.dbf, 00345602.dbf, 69209340.dbf, 69806980.dbf, 92406482.dbf, 99999999.dbf,... aaa.dbf, bbb.dbf, ...

I want to take only the "8-digit-number_type.dbf" files, and exclude the character type files. (I don't care about the character type files.) Then, I want to create a text file (say, namelist.txt) that contains the list of names of the dbf files as follows:

00093282

00345602

69209340

69806980

99999999

....

How can I do that in SAS?

Thanks.


Accepted Solutions
Solution
‎04-29-2012 08:24 PM
Valued Guide
Posts: 765

Re: How to create a text file that has file names in a directory

hi ... same idea, use a pipe but with DIR/B...


filename x pipe 'dir/b "z:\*.dbf" ' ;

filename y 'z:\namelist.txt';

data _null_;

infile x dlm='.';

file y;

input name : $20.;

if ^verify(trim(name),'0123456789') then put name;

run;

View solution in original post


All Replies
Respected Advisor
Posts: 4,646

Re: How to create a text file that has file names in a directory

Start with this:

filename toto PIPE "DIR ""D:\work\*.dbf"" " console=min;

data _null_;
length name $200;
infile toto firstobs=6 truncover;
input a$ b$ c$ file $char200.;
file "D:\work\namelist.txt";
name = trim(tranwrd(upcase(file),".DBF",""));
if anyalpha(name) = 0 then put name;
run;

PG

PG
Contributor
Posts: 40

Re: How to create a text file that has file names in a directory

I run it, and the namelist.txt looks like the following:

35,548 00093282

17,539 00095480

12,406 00095520

38,854 00095602

10,927 00098880

18,670 00099999

29,819 00340240

12,952 00340560

569,260 00345602

198,085 00346162

3,458 00347560

25,072 00349999

18,670 00400040

3,010 00409999

65,737 00801692

...

Some numbers that I don't want in the first column. But I can remove the first column later.

Thank you so much.

Respected Advisor
Posts: 4,646

Re: How to create a text file that has file names in a directory

Take Mike's code, it's much better, mine was just an approximation. - PG

PG
Solution
‎04-29-2012 08:24 PM
Valued Guide
Posts: 765

Re: How to create a text file that has file names in a directory

hi ... same idea, use a pipe but with DIR/B...


filename x pipe 'dir/b "z:\*.dbf" ' ;

filename y 'z:\namelist.txt';

data _null_;

infile x dlm='.';

file y;

input name : $20.;

if ^verify(trim(name),'0123456789') then put name;

run;

Contributor
Posts: 40

Re: How to create a text file that has file names in a directory

This code produces the list that I want. Thank you so much.

Super User
Posts: 9,676

Re: How to create a text file that has file names in a directory

If you only want 8 digit file name ,I prefer to Perl Regular Expression.

if prxmatch('/\d{8}/',name) then put name;

Ksharp

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 408 views
  • 0 likes
  • 4 in conversation