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.
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
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\
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.)
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
Thanks you very much, What the argument 'e' do in Findw statement?
@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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.