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
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, /)));
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=
What is the value for &_SASPROGRAMFILE when you run witin EG?
%put %unquote(&_sasprogramfile);
'T:\egtest.sas'
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:\
It should be just T:\ without the quotes
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, \/)));
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
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.
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/
%let path=%sysfunc(prxchange(s/[^\/\\]+$//,-1,%str(&_sasprogramfile)));
%put &path ;
OUTPUT:
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.