BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
chithra
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
ShiroAmada
Lapis Lazuli | Level 10

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

9 REPLIES 9
ShiroAmada
Lapis Lazuli | Level 10

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.

chithra
Quartz | Level 8

Thanks.

 

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

Don't know the path.

SASKiwi
PROC Star

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 

 

chithra
Quartz | Level 8

yes..exactly.

 

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

ShiroAmada
Lapis Lazuli | Level 10

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.

chithra
Quartz | Level 8

Thanks.

 

I tried this also. This helps.

ShiroAmada
Lapis Lazuli | Level 10

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;
JimE
Calcite | Level 5

To find the path to a filename use: 

%let myFilePath=%sysfunc(pathname(myFileName));

or for a library, use:

%let myLibraryPath=%sysfunc(pathname(myLibName));

 

See: https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245924.htm

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 14583 views
  • 0 likes
  • 5 in conversation