DATA Step, Macro, Functions and more

Truncate .sas string

Reply
Contributor
Posts: 70

Truncate .sas string

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
Super User
Posts: 5,096

Re: Truncate .sas string

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, /)));

 

 

Contributor
Posts: 70

Re: Truncate .sas string

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=

 

 

 

Super User
Posts: 5,096

Re: Truncate .sas string

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

Contributor
Posts: 70

Re: Truncate .sas string

%put %unquote(&_sasprogramfile);

'T:\egtest.sas'

 

Super User
Posts: 5,096

Re: Truncate .sas string

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:\

Contributor
Posts: 70

Re: Truncate .sas string

It should be just T:\ without the quotes

Super User
Posts: 5,096

Re: Truncate .sas string

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, \/)));

Contributor
Posts: 70

Re: Truncate .sas string

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

 

 

Super User
Posts: 5,096

Re: Truncate .sas string

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.

Super User
Super User
Posts: 6,502

Re: Truncate .sas string

[ Edited ]

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/
Super User
Posts: 9,691

Re: Truncate .sas string

%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
Ask a Question
Discussion stats
  • 11 replies
  • 494 views
  • 1 like
  • 4 in conversation