Help using Base SAS procedures

how to write SAS code to search a file's location?

Reply
Regular Contributor
Posts: 222

how to write SAS code to search a file's location?

Hello everyone,

How to use SAS searchingenginer to search a file's physical location?

For example I have a file named "abc.txt", it locates at c:\myfile .I want to know what is the physical location of the file.

Given I only  know the file's name is "abc.txt" but I don't know where it is,...so how could I use searching enginer in SAS to get its location: "c:\temp"?

Thanks

Mike

PROC Star
Posts: 7,363

Re: how to write SAS code to search a file's location?

Would using a pipe to run a dir command (assuming you are on windose) suffice?

Regular Contributor
Posts: 222

Re: how to write SAS code to search a file's location?

Hi Art,

Yes I am using wondows

Thanks

Mike

PROC Star
Posts: 7,363

Re: how to write SAS code to search a file's location?

Mike,

Try something like:

filename indata pipe "dir \abc.txt /b /s";

data want;

  informat fil2read $255.;

  infile indata truncover;

  input fil2read &;

run;

New Contributor
Posts: 3

Re: how to write SAS code to search a file's location?

Great answer. It is working perfectly.

Regular Contributor
Posts: 222

Re: how to write SAS code to search a file's location?


Thank you Art,

the dir command works well but end with:  "file not found"

Mike

PROC Star
Posts: 7,363

Re: how to write SAS code to search a file's location?

Mike,

Are you trying to find a file within your computer's c: drive, another drive on your computer, or on a network?

New Contributor
Posts: 3

Re: how to write SAS code to search a file's location?

Hi Mike:

Art' code work well. But you need a little bit change since everyone's computer config is different. if you file is sitting in d: driver, please add "x d:;". The code will be like:

x d:;

filename indata pipe "dir \abc.txt /b /s";

data want;

  informat fil2read $255.;

  infile indata truncover;

  input fil2read &;

run;

Super User
Super User
Posts: 6,500

Re: how to write SAS code to search a file's location?

Are you asking to know the full path to a file that you can already find?  You could use the FILENAME option on INFILE or FILE statement.

70   data _null_;

71    length filename $200;

72    infile 'myfile.txt' filename=filename;

73    putlog filename=;

74   run;

NOTE: The infile 'myfile.txt' is:

      Filename=C:\Documents and Settings\XXXXX\myfile.txt,

      RECFM=V,LRECL=256,File Size (bytes)=0,

      Last Modified=20Aug2012:15:05:10,

      Create Time=20Aug2012:15:05:10

filename=C:\Documents and Settings\XXXXX\myfile.txt

Regular Contributor
Posts: 222

Re: how to write SAS code to search a file's location?

Hi Tom,

The code has problem to run,

 

45 data _null;

46 length filename $200;

47 infile 'list.txt' filename=filename;

48 putlog filename=;

49 run;

ERROR: Physical file does not exist, H:\list.txt.

Thanks

Mike

Valued Guide
Posts: 632

Re: how to write SAS code to search a file's location?

This error indicates to me that the file list.txt is not in the current directory (root of HSmiley Happy.  From the earlier posts it is still unclear to me that the OP knows the actual location of the file.

Super Contributor
Posts: 376

Re: how to write SAS code to search a file's location?

Hi Mike,

In a command window (start --> run --> cmd.exe), type "help dir" and carefully read the output.  Get your search working from Windows itself, then paste that code into Art's solution.

HTH,

Scott

Super User
Posts: 17,819

Re: how to write SAS code to search a file's location?

What happens if two files with the same name are stored in different locations, ie a new version and backed up one?

It seems one of those things that you should know where to look rather than search.

Windows Search is really useful and you can search by extension, so if i see some one has created a dataset called hidden_data I can search for it using

hidden_data ext:sas and it will list SAS programs that have that dataset in the code.

PROC Star
Posts: 7,363

Re: how to write SAS code to search a file's location?

Fareeza,

The code I suggested, which is basically using Window's search capabilities, will provide the full path to each file in the case of duplicate names.

Super User
Posts: 9,681

Re: how to write SAS code to search a file's location?

Or chang it if your file is under D disk.

filename indata pipe " dir d:\x.txt /s/b ";

Ksharp

Ask a Question
Discussion stats
  • 15 replies
  • 613 views
  • 5 likes
  • 9 in conversation