Solved
Contributor
Posts: 28

# Find the path of a filename

Dear All,

I need to find the pathname of a filename in sas. The input should be only the filename.

Is it possible in SAS? or anybody knows how this can be done in UNIX?

Thanks,

Chithra

Accepted Solutions
Solution
‎09-20-2017 03:21 AM
Frequent Contributor
Posts: 118

## Re: Find the path of a filename

Here's a step by step approach

data test;
value='C:\Windows\Test.txt';
file=scan(value,-1,"\");
endpos=find(value,strip(file));
path=substr(value,1,endpos-1);
run;

All Replies
Frequent Contributor
Posts: 118

## Re: Find the path of a filename

[ Edited ]

Try this.....

data WANT;
filename="c:\windows\text.txt";
path=substr(filename,1,find(filename,reverse(scan(reverse(filename),1,"\")))-1);
run;

Hope this helps.

Contributor
Posts: 28

## Re: Find the path of a filename

Thanks.

But i have only the file name text.txt with me.

Don't know the path.

Super User
Posts: 4,030

## Re: Find the path of a filename

To find a file on a Unix server, type this at a Unix command line: find / -name text.txt



I wouldn't recommend doing this from SAS, assuming that you only want to do it once

Contributor
Posts: 28

## Re: Find the path of a filename

yes..exactly.

Thanks. I use this command with pipe and it works fine.

Frequent Contributor
Posts: 118

## Re: Find the path of a filename

[ Edited ]

Oh now i understand.

Now try this.

options noxwait;FILENAME pathloc 'e:\test.txt';

data MYPATH;
x "dir e:\sample.txt /s /p > e:\test.txt";
infile pathloc;
input locator \$100.;
if indexw(locator,'Directory of');
run;

#1 assign a fileref (just like a libname statement)

#2 Use x command and specify a directory and the file that will be searched, file is saved (same as fileref).

#4 Look and retian the keywords "Directory of"

Hope this helps.

Contributor
Posts: 28

## Re: Find the path of a filename

Thanks.

I tried this also. This helps.

Solution
‎09-20-2017 03:21 AM
Frequent Contributor
Posts: 118

## Re: Find the path of a filename

Here's a step by step approach

data test;
value='C:\Windows\Test.txt';
file=scan(value,-1,"\");
endpos=find(value,strip(file));
path=substr(value,1,endpos-1);
run;
☑ This topic is solved.