-
Notifications
You must be signed in to change notification settings - Fork 8
/
substrn.sas
22 lines (22 loc) · 1.01 KB
/
substrn.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
%macro substrn
/*----------------------------------------------------------------------
Subset string (simulation of SUBSTRN function)
----------------------------------------------------------------------*/
(string /* String to subset */
,position /* Start position */
,length /* Length of substring */
);
%local len s e;
%*----------------------------------------------------------------------
Get length of string. Calculate start and end positions within string.
When LENGTH is not specified use length of string.
----------------------------------------------------------------------;
%let len=%length(&string);
%if 0=%length(&length) %then %let length=&len;
%let s=%sysfunc(max(&position,1));
%let e=%sysfunc(min(&len,&length+&position-1));
%*---------------------------------------------------------------------
Use SUBSTR to return the part of the string requested.
----------------------------------------------------------------------;
%if (&s <= &len) and (&s <= &e) %then %substr(&string,&s,&e-&s+1);
%mend substrn;