forked from Jiangtang/SAS_ListProcessing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_string.sas
55 lines (47 loc) · 2.06 KB
/
rename_string.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*%rename_string
Purpose: Create a list suitable for the rename statement where the variables in a list
are renamed so that they have a common text string as a prefix or suffix.
dependence: %num_tokens, %parallel_join, %add_string
Required arguments:
words – the variable list containing the original names
str – the text string to add to each renamed variable
Optional arguments:
location – whether to add the text string as a prefix or suffix [prefix|suffix, default: suffix]
delim – the character(s) separating each variable in the &words list [default: space]
Examples:
%put %rename_string(a b c, _1);
produces the text a=a_1 b=b_1 c=c_1
%put %rename_string(a b c, r_, location=prefix);
produces the text a=r_a b=r_b c=r_c
%put %rename_string(a|b|c, _1, delim=|);
produces the text a=a_1 b=b_1 c=c_1
Credit:
source code from Robert J. Morris, Text Utility Macros for Manipulating Lists of Variable Names
(SUGI 30, 2005) www2.sas.com/proceedings/sugi30/029-30.pdf
*/
%macro rename_string(words, str, delim=%str( ), location=suffix);
%* Verify macro arguments. ;
%if (%length(&words) eq 0) %then %do;
%put ***ERROR(rename_string): Required argument 'words' is missing.;
%goto exit;
%end;
%if (%length(&str) eq 0) %then %do;
%put ***ERROR(rename_string): Required argument 'str' is missing.;
%goto exit;
%end;
%if (%upcase(&location) ne SUFFIX and %upcase(&location) ne PREFIX) %then %do;
%put ***ERROR(rename_string): Optional argument 'location' must be;
%put *** set to SUFFIX or PREFIX.;
%goto exit;
%end;
%* Since rename_string is just a special case of parallel_join,
* simply pass the appropriate arguments on to that macro. ;
%parallel_join(
&words,
%add_string(&words, &str, delim=&delim, location=&location),
=,
delim1 = &delim,
delim2 = &delim
)
%exit:
%mend rename_string;