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!
/*--------------------------------------------------------------------------------------*
Copyright 2017, Rho, Inc. All rights reserved.
PROGRAM: list2format
PURPOSE: Create a numeric format based on a list of values.
INPUT: fmtname= format name
list= list of values
DETAILS:
- list should be specified as comma-separated values enclosed in parentheses.
e.g., list=(ALT,BILI,CREAT).
PROGRAM HISTORY:
DATE PROGRAMMER DESCRIPTION
--------- --------------- ------------------------------------------------------
2017-12-15 Shane Rosanbalm Create.
*--------------------------------------------------------------------------------------*/
%macro list2format
(fmtname=
,list=
);
%*--- get first item ---;
%local i item;
%let i = 1;
%let item = %qscan(%nrbquote(&list),&i,%str((),));
%*--- continue if more items ---;
%do %while(&item ne );
%local list_&i;
%let list_&i = &item;
%let i = %eval(&i+1);
%let item = %qscan(%nrbquote(&list),&i,%str((),));
%end;
%*--- number of items found ---;
%local list_n;
%let list_n = %eval(&i-1);
%*--- make a format ---;
proc format;
value &fmtname
%do i = 1 %to &list_n;
&i = "%sysfunc(strip(&&list_&i))"
%end;
;
run;
%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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.