BookmarkSubscribeRSS Feed

We currently have the REPEAT function, which repeats a given string n+1 times, where n >= 0.  Thus, the return value will always contain the given string at least 1 time.

 

It would be convenient to have a version of REPEAT that repeats the string just n times, where n is allowed to be nonpositive.  When n is nonpositive, the return value would be a zero-length string -- i.e., very analogous to how SUBSTRN treats a nonpositive length argument.

 

I would propose calling this new function REPEATN, which seems to be in line with SAS's naming convention for functions related to zero-length strings, such as SUBSTRN, TRIMN, and LENGTHN.

 

My reasoning for this proposed new function is that there are often situations where a given string needs to be repeated 0 or more times, not 1 or more times.  Due to REPEAT always repeating the string at least once, a special check needs to be done for the case of 0 repeats.

 

I'm proposing that REPEATN repeats the given string n times instead of n+1, but others may find it preferable to stick with n+1 since that is how REPEAT works.  Either way, the primary benefit of REPEATN would be the ability to get back a zero-length string, made possible by expanding the allowable range of values for n.

 

Alternatively, instead of a new function, the current REPEAT function could be modified to accept negative values for n in order to return a zero-length string, although I was hesitant to propose that since it could break existing code.

 

Here is an example showing how REPEATN could lead to simpler, easier-to-read code:

 

 

* Goal: Left-pad strings with 0 to length 9.

* Note: This is of course not the only way to solve the given task --
* it's just meant to show an example usage of REPEATN. ;

* Create some test data. ;
data demo;
	length id $9;
	input id 1-9;
	datalines;
1
12
A23
1B34
12C45
123D56
1 34E67
12 45F78
123 56G89
run;

* REPEAT requires a special check to make sure the length is not already 9. ;
data demo2;
	set demo;
	if lengthn(id) < 9 then
		id2 = repeat('0', 9 - lengthn(id) - 1) || strip(id);
	else id2 = id;
run;

* REPEATN works for any starting length. ;
data demo3;
	set demo;
	id2 = repeatn('0', 9 - lengthn(id)) || strip(id);
run;