DATA Step, Macro, Functions and more

Find the path of a filename

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

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;

View solution in original post


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

Posted in reply to ShiroAmada

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).

#3. Read the saved file.

#4 Look and retian the keywords "Directory of"

 

Hope this helps.

Contributor
Posts: 28

Re: Find the path of a filename

Posted in reply to ShiroAmada

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.

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

Discussion stats
  • 7 replies
  • 677 views
  • 0 likes
  • 3 in conversation