The macro does not create data. It reads the metadata of an existing SAS dataset, whose name is passed as a parameter (`libds`). This dataset can be an internal table (like `sashelp.class` in the examples) or any other accessible user dataset. The macro does not directly access external data sources but operates on datasets already loaded or accessible by SAS.
1 Code Block
MACRO DEFINITION
Explanation : Declaration of the `%mf_getvarlist` macro with its input parameters for the dataset, delimiter, quote options, and type filter. Local variables used by the macro are also defined here to avoid interference with other global macro variables.
Copied!
%macro mf_getvarlist(libds
,dlm=%str( )
,quote=no
,typefilter=A
)/*/STORE SOURCE*/;
/* declare local vars */
%local outvar dsid nvars x rc dlm q var vtype;
1
%macro mf_getvarlist(libds
2
,dlm=%str( )
3
,quote=no
4
,typefilter=A
5
)/*/STORE SOURCE*/;
6
/* declare local vars */
7
%local outvar dsid nvars x rc dlm q var vtype;
2 Code Block
MACRO LOGIC (QUOTE FORMATTING)
Explanation : This conditional block determines the value of the macro variable `q` which will represent the quote character. If the `quote` parameter is 'DOUBLE', `q` takes the value of the double quote (ASCII 34). If 'SINGLE', it takes the value of the single quote (ASCII 39). Otherwise, `q` remains empty.
Copied!
/* credit Rowland Hale - byte34 is double quote, 39 is single quote */
%if %upcase("e)=DOUBLE %then %let q=%qsysfunc(byte(34));
%else %if %upcase("e)=SINGLE %then %let q=%qsysfunc(byte(39));
1
/* credit Rowland Hale - byte34 is double quote, 39 is single quote */
Explanation : The dataset specified by the `libds` parameter is opened in read mode. The `%sysfunc(open(...))` function returns a dataset identifier (`dsid`) if the opening is successful, or 0 in case of failure.
Copied!
/* open dataset in macro */
%let dsid=%sysfunc(open(&libds));
1
/* open dataset in macro */
2
%let dsid=%sysfunc(open(&libds));
3
4 Code Block
MACRO LOGIC (VARIABLE PROCESSING)
Explanation : If the dataset is opened successfully, this block retrieves the number of variables (`nvars`). A loop iterates from 1 to `nvars`. For each variable, its type (`vtype`) is checked against `typefilter`. If the variable matches, its name (`varname`) is retrieved, potentially enclosed by `q`. If the variable name is empty, a warning is issued. The variable name is then added to `outvar`, separated by `dlm`. Finally, the dataset is closed.
Explanation : If the dataset opening fails, this block is executed. It displays clear error messages in the SAS log, including the macro name, the affected dataset, the opening return code, and the detailed system message, thus aiding diagnosis. The dataset is then closed to release resources, even if the initial opening failed.
Copied!
%else %do;
%put &sysmacroname: Unable to open &libds (rc=&dsid);
%put &sysmacroname: SYSMSG= %sysfunc(sysmsg());
%let rc=%sysfunc(close(&dsid));
%end;
1
%ELSE %DO;
2
%put &sysmacroname: Unable to open &libds (rc=&dsid);
3
%put &sysmacroname: SYSMSG= %sysfunc(sysmsg());
4
%let rc=%sysfunc(close(&dsid));
5
%END;
6 Code Block
MACRO RETURN
Explanation : This final block is responsible for the macro's return value. It uses `%unquote(&outvar)` to ensure that the character string `outvar`, containing the formatted list of variables, is returned as is, without unwanted interpretation of special characters by the macro processor. The macro is then terminated by `%mend`.
Copied!
%do;%unquote(&outvar)%end;
%mend mf_getvarlist;
1
%DO;%unquote(&outvar)%END;
2
%mend mf_getvarlist;
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 : Mentioned in associated comments: Allan Bowe. The referenced _version.sas file indicates 'Copyright (c) 2001-2006 Rodney Sparapani'.
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.