DATA Step, Macro, Functions and more

Extract filename from path string

Reply
N/A
Posts: 0

Extract filename from path string

Hi,

I have a string to store the path of a file, such as:C:\abc\example.csv

Suppose, Now, I have a lot of paths.
Like, C:\abc\1.csv
C:\abc\information\1.csv
C:\abc\t\aac.csv
e:\abct\b.csv
......

How can I get the filename(example) from the undefined string?
I think I can use substr to find the last '.' and position of last '/' , then get the filename.
How can I do that?

Thanks.
Super Contributor
Super Contributor
Posts: 3,174

Re: Extract filename from path string

As I understand your post, you want to extract the "filename.extension" information from a complete file-path data string? If this is true, you can use the SCAN function in a DATA step with the third argument "-1" with the delimiter "\" as you have explained. If you are using the SAS macro language, you can use the function %SYSFUNC along with SCAN, as explained above.

Scott Barry
SBBWorks, Inc.

SAS.COM support website resources:

-- SAS 9.2 DOC on SCAN function:
http://support.sas.com/documentation/cdl/en/lrdict/59540/HTML/default/a000214639.htm

-- SAS Documentation web page:
http://support.sas.com/documentation/onlinedoc/base/index.html
N/A
Posts: 0

Re: Extract filename from path string

Hi, Scott, thank you for you time.

I solved it!

Best wishes,
Jun
Occasional Contributor
Posts: 15

Re: Extract filename from path string

Hi,

 

Can you please share, how did you solved it, as i m looking for same thing.

 

Thanks,

Sunil

Ask a Question
Discussion stats
  • 3 replies
  • 1710 views
  • 0 likes
  • 3 in conversation