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!
%if not (%mf_existds(&libds)) %then %do;
data &libds (index=(key/unique));
length key $64 valc $2048 valn 8 type $1;
call missing(of _all_);
stop;
run;
%end;
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!
proc sql;
delete from &libds
where key=symget('key');
insert into &libds
set key=symget('key')
%if &type=C %then %do;
,valc=symget('value')
,type='C'
%end;
%else %do;
,valn=symgetn('value')
,type='N'
%end;
;
quit;
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.
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.