Published on :
Macro CREATION_INTERNE

Key-Value Manager in a Dataset

This code is also available in: Deutsch Español Français
Awaiting validation
The `%mp_setkeyvalue` macro is a flexible tool for maintaining structured information as key-value pairs in a SAS© dataset. If the target dataset (`libds=`) does not exist, it initializes a new table with `key` columns (char $64 with unique index), `valc` (char $2048 for textual values), `valn` (num 8 for numeric values), and `type` (char $1 to indicate the stored value type). The use of a unique index on the `key` column ensures data integrity. It then uses `PROC SQL` to first delete any existing entry for the given `key`, ensuring only one entry exists for each key. Then, it inserts the new key-value pair. The `type` parameter (C for character by default, N for numeric) determines which column (`valc` or `valn`) the value is stored in. This approach is useful for configuration tables, dynamic parameters, or process tracking.
Data Analysis

Type : CREATION_INTERNE


The macro creates and manipulates an internal SAS dataset (default 'work.mp_setkeyvalue') or a user-specified dataset. It does not read data from external sources (files, external databases) or from SASHELP datasets, with the potential exception of the macro `%mf_existds` which might query system metadata on SAS datasets, but the primary objective is the management of a dataset defined by the user or the macro.

1 Code Block
DATA STEP Data
Explanation :
This conditional code block checks for the existence of the target dataset `&libds` using the `%mf_existds` macro. If it does not exist, a DATA step is executed to create the dataset. The `&libds` dataset is defined with specific attributes: a `key` column of 64 characters with a unique index for quick lookups, `valc` of 2048 characters for textual values, `valn` of 8 bytes for numeric values, and `type` of 1 character to indicate the stored value format. `CALL MISSING(OF _ALL_)` initializes all variables to missing values, and `STOP` prevents the DATA step from reading observations, thus creating an empty dataset with the defined structure.
Copied!
1%IF not (%mf_existds(&libds)) %THEN %DO;
2 DATA &libds (index=(key/unique));
3 LENGTH key $64 valc $2048 valn 8 type $1;
4 call missing(of _all_);
5 stop;
6 RUN;
7 %END;
2 Code Block
PROC SQL
Explanation :
This block uses `PROC SQL` to insert or update the key-value pair in the `&libds` dataset. First, a `DELETE` statement is executed to remove any existing row corresponding to the provided `key`, thus ensuring that only one entry exists for each key. Then, an `INSERT` statement is used to add the new key-value pair. The value is stored in the `valc` column if the `type` parameter is 'C' (character), or in `valn` if `type` is 'N' (numeric), using `%SYMGET` or `%SYMGETN` respectively to retrieve the macro variable values. The `type` column is also updated accordingly. `QUIT;` terminates the SQL procedure.
Copied!
1 PROC SQL;
2 delete from &libds
3 where key=symget('key');
4 insert into &libds
5 SET key=symget('key')
6 %IF &type=C %THEN %DO;
7 ,valc=symget('value')
8 ,type='C'
9 %END;
10 %ELSE %DO;
11 ,valn=symgetn('value')
12 ,type='N'
13 %END;
14 ;
15 
16 QUIT;
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 (c) 2001-2006 Rodney Sparapani (pour _version.sas, sous GNU General Public License); Copyright 2010-2023 HMS Analytical Software GmbH (pour macro_without_brief_tag.sas, part of SASUnit, sous GNU Lesser General Public License); Copyright © 2022, SAS Institute Inc. (pour print_macro_parameters.sas, sous SPDX-License-Identifier: Apache-2.0); Copyright (c) 2004 Rodney Sparapani (pour _file.json, sous GNU General Public License); Copyright 2017, Rho, Inc. All rights reserved. (pour list2items.json).