BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ganeshk
Obsidian | Level 7
Hi team,

I have a dataset with a variable path, I need to extract the path till last but one and save it as new variable. Example:
Path;
/c:/user/new/path1/path2/path3/path4/cjx.sas
/c:/user/old/new/path1/path2/path3/path4/cjx.sas
/c:/user/new/path5/path2/path3/path4/cjx.sas
/c:/user/old/new/path5/path2/path3/path4/cjx.sas

What I want is:
Path_1;
/c:/user/new/path1/path2/path3/path4/
/c:/user/old/new/path1/path2/path3/path4/
/c:/user/new/path5/path2/path3/path4/
/c:/user/old/new/path5/path2/path3/path4/

Regards,
GK
1 ACCEPTED SOLUTION

Accepted Solutions
anushreebiotech
Obsidian | Level 7

Hi,

 

data have;
input path $50.;
datalines;
/c:/user/new/path1/path2/path3/path4/cjx.sas
/c:/user/old/new/path1/path2/path3/path4/cjx.sas
/c:/user/new/path5/path2/path3/path4/cjx.sas
/c:/user/old/new/path5/path2/path3/path4/cjx.sas
;
run;

 

data want;
set have;
new_path= Tranwrd(path,'cjx.sas',"");
run;

 

Regards,

Anushree

View solution in original post

4 REPLIES 4
heffo
Pyrite | Level 9

Would this work:

data want;
	FilePath = "/c:/user/new/path1/path2/path3/path4/cjx.sas";
	Folder = substr(FilePath,1,length(FilePath)-length(scan(FilePath,-1,"/")));
run;
andreas_lds
Jade | Level 19

Or by using substr and findc:

 

data have_want;
   length path directory $ 200;

   input path;

   directory = substr(path, 1, findc(path, '/', 'b'));

datalines;
/c:/user/new/path1/path2/path3/path4/cjx.sas
/c:/user/old/new/path1/path2/path3/path4/cjx.sas
/c:/user/new/path5/path2/path3/path4/cjx.sas
/c:/user/old/new/path5/path2/path3/path4/cjx.sas
;
run;
anushreebiotech
Obsidian | Level 7

Hi,

 

data have;
input path $50.;
datalines;
/c:/user/new/path1/path2/path3/path4/cjx.sas
/c:/user/old/new/path1/path2/path3/path4/cjx.sas
/c:/user/new/path5/path2/path3/path4/cjx.sas
/c:/user/old/new/path5/path2/path3/path4/cjx.sas
;
run;

 

data want;
set have;
new_path= Tranwrd(path,'cjx.sas',"");
run;

 

Regards,

Anushree

Ksharp
Super User
data have_want;
   length path directory $ 200;

   input path;

   directory = prxchange('s/[^\/]+$//',1,strip(path));

datalines;
/c:/user/new/path1/path2/path3/path4/cjx.sas
/c:/user/old/new/path1/path2/path3/path4/cjx.sas
/c:/user/new/path5/path2/path3/path4/cjx.sas
/c:/user/old/new/path5/path2/path3/path4/cjx.sas
;
run;

proc print;run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1512 views
  • 9 likes
  • 5 in conversation