- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I am having trouble to do extraction of 'String' from the folder path. How I can do it dynamically. Here is the example. I given two paths links with little modification. I got stuck after the following.
path1: Desktop\documents\school\class10\Grades\2022-10\expelled\
path2 : Desktop\archive\documents\school\class102\Grades\2021-09\success\
data link;
path1= 'Desktop\documents\school\classx\Grades\2022-10\expelled\';
xx = find(path1,'school');
folder = compress(substr(path1,xx+7),'-');
run;
My expectations is to extract dynamically from both paths :
Path1 : class10\Grades\202210
Path2 : class102\Grades\202109
Path length will be changes depending on the folders and file name, but strings after 'School' have the same number of subfolders. That means, the number '\' (backslash) will be same for both links after the 'School' even though lengths are different. Thank you.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
So you just want the three sub-directories under SCHOOL.
In that case use the E modifier on FINDW() to return the word instead of the character.
data want;
set have;
location = findw(path,'school','\','e');
if location then subpath=catx('\',scan(path,location+1,'\')
,scan(path,location+2,'\')
,scan(path,location+3,'\'));
run;
Obs path location subpath 1 Desktop\documents\school\class10\Grades\2022-10\expelled\ 3 class10\Grades\2022-10 2 Desktop\archive\documents\school\class102\Grades\2021-09\success\ 4 class102\Grades\2021-09
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Can you explain why you are removing the class number and the hyphen? Also why are you using only three of the sub-directories and not all of them?
data have;
input path $80.;
cards;
Desktop\documents\school\class10\Grades\2022-10\expelled\
Desktop\archive\documents\school\class102\Grades\2021-09\success\
;
data want;
set have;
location = findw(path,'school','/\');
if location then subpath=substr(path,location+7);
run;
proc print;
run;
Result
Obs path location subpath 1 Desktop\documents\school\class10\Grades\2022-10\expelled\ 19 class10\Grades\2022-10\expelled\ 2 Desktop\archive\documents\school\class102\Grades\2021-09\success\ 27 class102\Grades\2021-09\success\
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi Tom,
Sorry , I think I forgot add them when I edited the link, corrected it now. Thanks for the code.. However I have additional request. I am looking to remove the string colored in red of subpath you created. I am only extracting few subdirectories because based on this I will be creating macro variables.
class10\Grades\2022-10\expelled\
I am expecting ' class10\Grades\2022-10' ( I remove the the sting after the second '\' back slash in REVERS.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
So you just want the three sub-directories under SCHOOL.
In that case use the E modifier on FINDW() to return the word instead of the character.
data want;
set have;
location = findw(path,'school','\','e');
if location then subpath=catx('\',scan(path,location+1,'\')
,scan(path,location+2,'\')
,scan(path,location+3,'\'));
run;
Obs path location subpath 1 Desktop\documents\school\class10\Grades\2022-10\expelled\ 3 class10\Grades\2022-10 2 Desktop\archive\documents\school\class102\Grades\2021-09\success\ 4 class102\Grades\2021-09
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks you very much, What the argument 'e' do in Findw statement?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@SASuserlot wrote:
Thanks you very much, What the argument 'e' do in Findw statement?
Exactly what I said it does.
return the word instead of the character
Look at the value of LOCATION in the two examples I posted.