Help using Base SAS procedures

String manipulation in SAS

Reply
N/A
Posts: 0

String manipulation in SAS

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
Super Contributor
Super Contributor
Posts: 3,174

Re: String manipulation in SAS

Posted in reply to deleted_user
Explore using the FIND function with a minus third argument to start search from the end.

Scott Barry
SBBWorks, Inc.
Frequent Contributor
Frequent Contributor
Posts: 77

Re: String manipulation in SAS

Posted in reply to deleted_user
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,'\'));
N/A
Posts: 0

Re: String manipulation in SAS

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,'\'));
Valued Guide
Posts: 2,177

Re: String manipulation in SAS

Posted in reply to deleted_user
the scan function can simplify this[pre] file = scan( pafthfile, -1, '/\' ) ;[/pre]
try that

Peterc
Super User
Posts: 11,343

Re: String manipulation in SAS

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.
Ask a Question
Discussion stats
  • 5 replies
  • 353 views
  • 0 likes
  • 5 in conversation