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

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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