Published on :
Macro CREATION_INTERNE

Macro for editing a list of items

This code is also available in: Deutsch Español Français
This macro iterates through a list of space-separated items. For each item, it applies an editing string provided as a parameter. The current item is accessible via the macro variable '&item'. This is particularly useful for dynamically generating series of SAS© statements, such as variable renames or multiple assignments. The final string is the concatenation of the edited strings for each item.
Data Analysis

Type : CREATION_INTERNE


The macro does not read any data source. It operates on a list of character strings provided as a parameter during its call.

1 Code Block
Macro
Explanation :
The 'editlist' macro takes a list of words ('list') and an editing string ('editstr') as input. It initializes a local variable 'i' to 1 and an 'item' that takes the value of the first word in the list. A '%do %while' loop executes as long as an 'item' is found. Inside the loop, the %sysfunc(dequote(&editstr)) function is executed, which interprets the editing string by substituting the macro variable '&item' with its current value. Then, the counter 'i' is incremented and the next item is read. The final result is the set of edited strings, one after the other.
Copied!
1/*<pre><b>
2/ Program : editlist.sas
3/ Version : 1.0
4/ Author : Roland Rashleigh-Berry
5/ Date : 01-Nov-2012
6/ Purpose : Function-style macro to allow you to edit a list of space
7/ delimited items.
8/ SubMacros : none
9/ Notes : This macro is for tasks like generating rename statements where a
10/ repeat of items in a list is required (see usage notes). The edit
11/ string must be enclosed in single quotes. Elements of the list
12/ are written to the macro variable "item" which can be referenced
13/ in the edit string. If semicolons form part of the edit string
14/ then for certain uses these can be protected using %nrstr().
15/
16/ If used in sas code you might need to %unquote() the final string.
17/
18/ This macro is essentially the same as the %doallitem macro but
19/ giving a different usage emphasis and with no submacros.
20/
21/ Usage : %put >>> %editlist(aa bb cc dd,'&item=mr_&item');
22/ %put >>> %editlist(xx_aa xx_bb xx_cc,
23/ '&item=%substr(&item,4)');
24/ %put >>> %editlist(xx_aa xx_bb xx_cc,
25/ '%substr(&item,4)=&item%nrbquote(;)');
26/
27/ (will write to log:)
28/ >>> aa=mr_aa bb=mr_bb cc=mr_cc dd=mr_dd
29/ >>> xx_aa=aa xx_bb=bb xx_cc=cc
30/ >>> aa=xx_aa; bb=xx_bb; cc=xx_cc;
31/===============================================================================
32/ PARAMETERS:
33/-------name------- -------------------------description------------------------
34/ list (pos) List of space delimited items
35/ editstr (pos) Edit string (in single quotes)
36/===============================================================================
37/ AMENDMENT HISTORY:
38/ init --date-- mod-id ----------------------description------------------------
39/ rrb 31Oct12 New (v1.0)
40/===============================================================================
41/ This is public domain software. No guarantee as to suitability or accuracy is
42/ given or implied. User uses this code entirely at their own risk.
43/=============================================================================*/
44 
45%put MACRO CALLED: editlist v1.0;
46 
47%macro editlist(list,editstr);
48 %local i item;
49 %let i=1;
50 %let item=%scan(&list,&i,%str( ));
51 %DO %while(%LENGTH(&item));
52%sysfunc(dequote(&editstr))
53 %let i=%eval(&i + 1);
54 %let item=%scan(&list,&i,%str( ));
55 %END;
56%mend editlist;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : This is public domain software. No guarantee as to suitability or accuracy is given or implied. User uses this code entirely at their own risk.