SAS Programming

DATA Step, Macro, Functions and more
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;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 1168 views
  • 9 likes
  • 5 in conversation