list2format Macro

This code is also available in: Deutsch Español Français
Difficulty Level
Beginner
Published on :
This SAS© macro, named `list2format`, dynamically generates a SAS© numeric format. It accepts two parameters: `fmtname` to specify the name of the format to create, and `list` for a character string containing a comma-separated list of values, ideally enclosed in parentheses (e.g., `(ALT,BILI,CREAT)`). The macro parses this list, extracts each element, and sequentially associates it with an integer in the format definition via `PROC FORMAT`. Each element in the list is cleaned of superfluous spaces using `%sysfunc(strip())` before being integrated into the format. It is a practical tool for converting a list of strings into a usable numeric format for categorical variables.
Data Analysis

Type : CREATION_INTERNE


The data used for format creation comes from a list of values passed as a parameter to the macro and processed internally, without external dependencies or SASHELP data.

1 Code Block
MACRO / PROC FORMAT Data
Explanation :
This block defines the `list2format` macro. It initializes local variables `i` (counter) and `item` to iterate over the list of values provided in the `&list` parameter. The `%qscan` function is used with `%nrbquote` to extract each element from the list, taking into account the specified delimiters (comma and parentheses). Each extracted element is stored in a dynamic macro variable (`list_&i`). Once the list is fully traversed, `list_n` contains the total number of elements. Then, a `PROC FORMAT` block is generated. A `%do` loop is used to create number=value associations in the format definition. For each iteration, the number (`&i`) is associated with the corresponding list value (referenced by `&&list_&i`) after being cleaned of spaces with `%sysfunc(strip())`. The final `run;` executes the formatting procedure.
Copied!
1/*--------------------------------------------------------------------------------------*
2 Copyright 2017, Rho, Inc. All rights reserved.
3 
4 PROGRAM: list2format
5 
6 PURPOSE: Create a numeric format based on a list of values.
7 
8 INPUT: fmtname= format name
9 list= list of values
10 
11 DETAILS:
12 - list should be specified as comma-separated values enclosed in parentheses.
13 e.g., list=(ALT,BILI,CREAT).
14
15 PROGRAM HISTORY:
16 DATE PROGRAMMER DESCRIPTION
17 --------- --------------- ------------------------------------------------------
18 2017-12-15 Shane Rosanbalm Create.
19 
20*--------------------------------------------------------------------------------------*/
21 
22%macro list2format
23 (fmtname=
24 ,list=
25 );
26 
27 %*--- get first item ---;
28 %local i item;
29 %let i = 1;
30 %let item = %qscan(%nrbquote(&list),&i,%str((),));
31
32 %*--- continue IF more items ---;
33 %DO %while(&item ne );
34 %local list_&i;
35 %let list_&i = &item;
36 %let i = %eval(&i+1);
37 %let item = %qscan(%nrbquote(&list),&i,%str((),));
38 %END;
39
40 %*--- number of items found ---;
41 %local list_n;
42 %let list_n = %eval(&i-1);
43
44 %*--- make a FORMAT ---;
45 PROC FORMAT;
46 value &fmtname
47 %DO i = 1 %to &list_n;
48 &i = "%sysfunc(strip(&&list_&i))"
49 %END;
50 ;
51 RUN;
52 
53%mend list2format;
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 : Copyright 2017, Rho, Inc. All rights reserved.


Related Documentation

Aucune documentation spécifique pour cette catégorie.