BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I wanted some help to get some string manipulation functions working in my SAS program.

I have a data set which has a column called file with the absolute path of SAS dataset files in a particular specified location on File System. The data set looks something like..

file
----------------------
C:\MySAS\MyData\Folder1\unit1.sas7bdat
C:\MySAS\Folder2\unit2.sas7bdat
C:\MySAS\Folder3\unit3.sas7bdat

...
..

And so on

What I want to do is, for each entry in the dataset I want to get the dataset name
In the above example, for first entry C:\MySAS\MyData\Folder1\unit1.sas7bdat
I want to get "unit1.sas7bdat" string extracted.

I tried to use the index function..INDEX(file,'\');

but don't know how to use it recursively to get till the last slash. After getting the index of last '\' I thouguht I can use substring to get the part of string from that index onwards.

[BTW, is there any function that returns the index of last occurence of a character in string..like in Java there lastindexof(String,character)]

Any pointers , help on this?

Any help much appreciated!

Thanks,
Neelam
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Explore using the FIND function with a minus third argument to start search from the end.

Scott Barry
SBBWorks, Inc.
LAP
Quartz | Level 8 LAP
Quartz | Level 8
If you are not sure that it is always the third argument, you could use the reverse function and use the index function to find the first "word" (delimited by \) and then reverse back.

ie last_file_name = reverse(scan(reverse(pathname),1,'\'));
deleted_user
Not applicable
Thank you Scott and LAP. You were of great help!

Both options worked for me ..I tried

index = FIND(file,'\',"i",-99);
datasetName = SUBSTR(file,index+1);

and


datasetName = reverse(scan(reverse(file),1,'\'));
Peter_C
Rhodochrosite | Level 12
the scan function can simplify this[pre] file = scan( pafthfile, -1, '/\' ) ;[/pre]
try that

Peterc
ballardw
Super User
In a possibly completely warped approach if all of the files are 1) SAS data sets and 2) in the same folder I would be tempted to assign a library name to the folder and then use the SAS dictionary.members table to get the names.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1335 views
  • 0 likes
  • 5 in conversation