Published on :
Utility MIXTE

Macro to retrieve a variable's label

This code is also available in: Deutsch Español Français
Awaiting validation
The `%getVarLabel` macro takes as input the name of a dataset (`libds`, defaulting to `sashelp.class`) and the name of a variable (`var`). It uses SAS© system functions to open the dataset, check for the variable's existence, and extract its label. If the dataset cannot be opened or if the variable does not exist, an error message or note is issued in the SAS© log. The macro is compatible with SAS© Viya 4 and is designed to be reusable.
Data Analysis

Type : MIXTE


The `libds` parameter of the macro defaults to `sashelp.class`, which is an internal SAS dataset. The usage examples included in the macro's comments also create a temporary dataset (`data test;`) internally via a DATA step, demonstrating usage with data generated within the script.

1 Code Block
MACRO
Explanation :
This block defines the `%getVarLabel` macro. It opens a dataset specified by `libds` (or `sashelp.class` by default) using `%sysfunc(open)`. It then determines the variable number of `var` with `%sysfunc(varnum)` and retrieves its label with `%sysfunc(varlabel)`. Error messages or notes are generated if the dataset is not opened or if the variable is not found. Finally, the dataset is closed and the variable's label is returned.
Copied!
1%macro getVarLabel(libds=sashelp.class /* two level name */
2 , var= /* variable name for which to return the label */
3 );
4 %local dsid vnum vlabel rc;
5 /* Open dataset */
6 %let dsid = %sysfunc(open(&libds));
7 %IF &dsid > 0 %THEN %DO;
8 /* Get variable number */
9 %let vnum = %sysfunc(varnum(&dsid, &var));
10 %IF(&vnum. > 0) %THEN
11 /* Variable exists, so get label */
12 %let vlabel = %sysfunc(varlabel(&dsid, &vnum));
13 %ELSE %DO;
14 %put NOTE: Variable &var does not exist in &libds;
15 %let vlabel = %str();
16 %END;
17 %END;
18 %ELSE %put dataset &libds not opened! (rc=&dsid);
19 
20 /* Close dataset */
21 %let rc = %sysfunc(close(&dsid));
22 /* Return variable label */
23 &vlabel
24%mend;
2 Code Block
DATA STEP / MACRO CALLS Data
Explanation :
This block provides an example of using the `%getVarLabel` macro. It starts by creating a temporary dataset named `test` with three variables (`str`, `num`, `x`) and their associated labels and formats. Then, it calls the `%getVarLabel` macro four times with various combinations of datasets and variables, including cases where the variable exists and where it is missing, to demonstrate the macro's functionality. The results of the macro calls are displayed in the SAS log via the `%put` function.
Copied!
1DATA test;
2 label str='My String' num='My Number' ;
3 FORMAT str $1. num datetime19. x 8.;
4 stop;
5RUN;
6%put %getVarLabel(ds=test, var=str);
7%put %getVarLabel(ds=work.test, var=num);
8%put %getVarLabel(ds=test, var=x);
9%put %getVarLabel(ds=test, var=renegade);
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) 2016 Scott Bass (sas_l_739 @yahoo.com.au). This code is licensed under the Unlicense (http://unlicense.org/UNLICENSE), as indicated in the referenced @code_sas/@TEMPLATE.sas file.