@Rick_SAS wrote a SAS blog article posted July 27, 2015 (Convert a vector to a string) which described an algorithm to convert a vector of character strings to a single character variable. He needed a loop to perform the (sorry) recharacterization of the strings into a single string, and said that "[i]t is difficult to eliminate the DO loop in the module and truly vectorize this process."
I took his statement as a challenge and created a non-looping version of his MakeString module. As a test case, I used a modified line from Joyce Kilmer's poem, Trees.
Here is the SAS/IML program:
proc iml ;
start MakeString( vv, delim=' ', interm_delim='#' ) ;
/* purpose: convert vector of string elements vv into single string containing all elements of vv
*
* vv ::= string vector containing individual strings to be contained in resulting single string
* delim ::= character that separates individual strings in resulting single string
* interm_delim ::= intermediate delimiter used in internal processing
*
* note: intermediate delimiter interm_delim must not be a character in any of the elements of vv
*/
v = colvec( vv ) ;
v1 = v || repeat( interm_delim, nrow( v ) , 1 ) ;
v2 = rowcatc( rowvec( v1 )) ;
v3 = tranwrd( v2, interm_delim, delim ) ;
return v3 ;
finish ;
/* create a vector of strings */
a={ 'I' 'think' 'that' 'I' 'shall' 'never' 'see' 'an' 'algorithm' 'as' 'lovely' 'as' 'a' 'tree' } ;
/* convert vector of string elements into string variable */
b = MakeString( a ) ;
print a b ;
quit ;
The test case and result are reproduced below.
/* create a vector of strings */
a={ 'I' 'think' 'that' 'I' 'shall' 'never' 'see' 'an' 'algorithm' 'as' 'lovely' 'as' 'a' 'tree' } ;
/* convert vector of string elements into string variable */
b = MakeString( a ) ;
print a b ;
b | |
---|---|
COL1 | |
ROW1 |
I think that I shall never see an algorithm as lovely as a tree |
I think your function delets blanks in the matrix. Try this one
a = {"I think" "that I shall" "never see" "an algorithm"};
b = MakeString(a, "/") ;
print b;
Using your example, MakeString returns:
b
Ithink/thatIshall/neversee/analgorithm/
which is the result of ROWCATC() compressing all of the blanks in its argument and subsequent code separating strings. I designed MakeString to operate on simple character strings contained in a vector, not compound strings consisting of more than one blank-delimited character string such as represented in your example.
If your application requires that a vector of simple or compound strings be converted into a string variable, here is a possible solution:
a = {"I$think" "that$I$shall" "never$see" "an$algorithm"};
b = MakeString(a) ;
c = tranwrd( b, '$', ' ' ) ;
print b c ;
/******************************************************************************/
b
I$think that$I$shall never$see an$algorithm
c
I think that I shall never see an algorithm
HTH,
Ross
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.