BookmarkSubscribeRSS Feed
KBACHU
Fluorite | Level 6

I am using the below code to get the filename and filepath for the program I am executing from EG. Is there anyway, I can just get the path without the program name?

 

%put %unquote(&_sasprogramfile);
%let file_path = %unquote(&_sasprogramfile);
%put &=file_path;
%let file_name = %scan(&file_path,-2,./);
%put &=file_name;

 

Output

 

56 %put %unquote(&_sasprogramfile);
T:/test/Program10000.sas
57 %let file_path = %unquote(&_sasprogramfile);
58 %put &=file_path;
FILE_PATH=T:/test/Program10000.sas
59 %let file_name = %scan(&file_path,-2,./);
60 %put &=file_name;
FILE_NAME=Program10000
11 REPLIES 11
Astounding
PROC Star

I think what you are looking to extract is:

 

T:/test/

 

If that's the case, you are heading in the right direction.  Here would be a way to approach this:

 

%let path = %substr(&program_path, 1, %length(&program_path) - %length(%scan(&program_path, -1, /)));

 

 

KBACHU
Fluorite | Level 6

Thank You very much. This is what I am looking for. But when I ran it from EG I am getting the below warning message.

 

WARNING: Argument 3 to macro function %SUBSTR is out of range

PATH=

 

 

 

Astounding
PROC Star

What is the value for &_SASPROGRAMFILE when you run witin EG?

KBACHU
Fluorite | Level 6

%put %unquote(&_sasprogramfile);

'T:\egtest.sas'

 

Astounding
PROC Star

So within EG, your program path has a backwards slash instead of a forward slash.  That's easy enough to change.

 

Are the single quotes really part of the value?  Do you want them in the extracted string, or do you just want:

 

T:\

KBACHU
Fluorite | Level 6

It should be just T:\ without the quotes

Astounding
PROC Star

So safest would be to remove the quotes first:

 

%let path = %substr(&_sasprogramfile, 2, %length(&_sasprogramfile)-1);

 

Then apply the formula to the new variable using a backward slash:

 

%let path = %substr(&path, 1, %length(&path) - %length(%scan(&path, -1, \)));

 

If you plan on using the code across different operating systems, it is probably safe to include either slash as a delimiter:

 

%let path = %substr(&path, 1, %length(&path) - %length(%scan(&path, -1, \/)));

KBACHU
Fluorite | Level 6

I am still getting the same warning message. It is able to get the filename but not the path. Am I missing something?

 

Code:

 

%put %unquote(&_sasprogramfile);

%let file_path = %unquote(&_sasprogramfile);

%put &=file_path;

%let file_name = %scan(&file_path,-2,.\);

%put &=file_name;

 

 

%let path = %substr(&_sasprogramfile, 2, %length(&_sasprogramfile)-1);

%let path = %substr(&path, 1, %length(&path) - %length(%scan(&path, -1, \)));

%put &=path;

 

Log:

 

%put %unquote(&_sasprogramfile);

'T:\egtest1.sas'

25 %let file_path = %unquote(&_sasprogramfile);

26 %put &=file_path;

FILE_PATH='T:\egtest1.sas'

27 %let file_name = %scan(&file_path,-2,.\);

28 %put &=file_name;

FILE_NAME=egtest1

29

30

31

32 %let path = %substr(&_sasprogramfile, 2, %length(&_sasprogramfile)-1);

33 %let path = %substr(&path, 1, %length(&path) - %length(%scan(&path, -1, \)));

34

35 %put &=path;

36

37 GOPTIONS NOACCESSIBLE;

38 %LET _CLIENTTASKLABEL=;

39 %LET _CLIENTPROCESSFLOWNAME=;

40 %LET _CLIENTPROJECTPATH=;

41 %LET _CLIENTPROJECTNAME=;

42 %LET _SASPROGRAMFILE=;

43

44 ;*';*";*/;quit;run;

45 ODS _ALL_ CLOSE;

46

47

48 QUIT; RUN;

49

 

 

Astounding
PROC Star

My fault ... need to subtract 2 instead of 1 to account for both quotes:

 

%let path = %substr(&_sasprogramfile, 2, %length(&_sasprogramfile)-2);

 

It wouldn't hurt to add an additional %PUT statement after that, just to check as you go.

Tom
Super User Tom
Super User

You can use the DEQUOTE() function to remove quotes. It will also reverse any doubling of embedded quotes.

You need to find the file name first.

Then you can use the length of the name to subset the full prgram path to just the directory name.

 

%let file_name=%scan(%qsysfunc(dequote(&_sasprogramfile)),-1,/\);
%let file_path=%substr(%qsysfunc(dequote(&_sasprogramfile)),1,%length(&_sasprogramfile)-%length(&file_name)-2);

 Examples:

%let _sasprogramfile='T:\mydir\subdir\egtest.sas';
...
FILE_NAME=egtest.sas FILE_PATH=T:\mydir\subdir\


%let _sasprogramfile='/home/mydir/subdir/egtest.sas';
...
FILE_NAME=egtest.sas FILE_PATH=/home/mydir/subdir/
Ksharp
Super User
%let path=%sysfunc(prxchange(s/[^\/\\]+$//,-1,%str(&_sasprogramfile)));
%put &path ;

 

 

 

OUTPUT:

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
51
52 %let path=%sysfunc(prxchange(s/[^\/\\]+$//,-1,%str(&_sasprogramfile)));
53 %put &path ;
/folders/myfolders/
54
55
56 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
66

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 11 replies
  • 1545 views
  • 1 like
  • 4 in conversation