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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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