BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.

data t1;
length v1 $ 100;
input v1 $;
datalines;
G:/Data/CXV/PGMS/CTS_Test/Scripts/ScannedFiles1.txt
D:/Data/ABC/PGMS/CTS_Test/Scripts/ScannedFiles2.txt
E:/Data/XYZ/PGMS/CTS_Test/Scripts/ScannedFiles3.txt
C:/Data/DEF/PGMS/CTS_Test/Scripts/ScannedFiles4.txt
F:/Data/JFK/PGMS/CTS_Test/Scripts/ScannedFiles5.txt
;
run;

 

I need to extract only folder, not file (which is last ). Can you please help. 

G:/Data/CXV/PGMS/CTS_Test/Scripts
D:/Data/ABC/PGMS/CTS_Test/Scripts
E:/Data/XYZ/PGMS/CTS_Test/Scripts
C:/Data/DEF/PGMS/CTS_Test/Scripts
F:/Data/JFK/PGMS/CTS_Test/Scripts

 

Thanks in Adavance. 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data t1;
length v1 $ 100;
input v1 $;
want=prxchange('s/\/[^\/]+$//',1,strip(v1));
datalines;
G:/Data/CXV/PGMS/CTS_Test/Scripts/ScannedFiles1.txt
D:/Data/ABC/PGMS/CTS_Test/Scripts/ScannedFiles2.txt
E:/Data/XYZ/PGMS/CTS_Test/Scripts/ScannedFiles3.txt
C:/Data/DEF/PGMS/CTS_Test/Scripts/ScannedFiles4.txt
F:/Data/JFK/PGMS/CTS_Test/Scripts/ScannedFiles5.txt
;
run;

View solution in original post

4 REPLIES 4
MayurJadhav
Quartz | Level 8

You can use substr function in SAS to extract sub-string from a string. You can set the start and end position to fetch sub string from variable v1.

@kumarsandip975 

 

Try this one:

data out; 
set t1;
v1_new = substr(v1,1, 33);
run;
Mayur Jadhav
BI Developer. Writer. Creative Educator.

SAS Blog → https://learnsascode.com
YouTube Channel: → https://www.youtube.com/@imayurj
A_Kh
Lapis Lazuli | Level 10

Addition to the above solution. 

data want;
set t1;
v2= substr(v1, 1, length(v1) - length(scan(v1, -1, '/'))-1);
proc print; run; 

This code works for dynamic path. 

PaigeMiller
Diamond | Level 26
data want;
    set t1;
    where=find(v1,'/',-100);
    folder_name=substr(v1,1,where-1);
    drop where;
run;

 

@kumarsandip975 please do not use meaningless subject lines like "Programming Help", because virtually every thread can be "Programming Help". Please, from now on, make the subject line a brief description of the problem.

--
Paige Miller
Ksharp
Super User
data t1;
length v1 $ 100;
input v1 $;
want=prxchange('s/\/[^\/]+$//',1,strip(v1));
datalines;
G:/Data/CXV/PGMS/CTS_Test/Scripts/ScannedFiles1.txt
D:/Data/ABC/PGMS/CTS_Test/Scripts/ScannedFiles2.txt
E:/Data/XYZ/PGMS/CTS_Test/Scripts/ScannedFiles3.txt
C:/Data/DEF/PGMS/CTS_Test/Scripts/ScannedFiles4.txt
F:/Data/JFK/PGMS/CTS_Test/Scripts/ScannedFiles5.txt
;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 426 views
  • 3 likes
  • 5 in conversation