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!
%macro getVarLabel(libds=sashelp.class /* two level name */
, var= /* variable name for which to return the label */
);
%local dsid vnum vlabel rc;
/* Open dataset */
%let dsid = %sysfunc(open(&libds));
%if &dsid > 0 %then %do;
/* Get variable number */
%let vnum = %sysfunc(varnum(&dsid, &var));
%if(&vnum. > 0) %then
/* Variable exists, so get label */
%let vlabel = %sysfunc(varlabel(&dsid, &vnum));
%else %do;
%put NOTE: Variable &var does not exist in &libds;
%let vlabel = %str();
%end;
%end;
%else %put dataset &libds not opened! (rc=&dsid);
/* Close dataset */
%let rc = %sysfunc(close(&dsid));
/* Return variable label */
&vlabel
%mend;
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!
data test;
label str='My String' num='My Number' ;
format str $1. num datetime19. x 8.;
stop;
run;
%put %getVarLabel(ds=test, var=str);
%put %getVarLabel(ds=work.test, var=num);
%put %getVarLabel(ds=test, var=x);
%put %getVarLabel(ds=test, var=renegade);
1
DATA test;
2
label str='My String' num='My Number' ;
3
FORMAT str $1. num datetime19. x 8.;
4
stop;
5
RUN;
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.
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.