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
Barite | Level 11

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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