Published on :
Macro MIXTE

Macro hasvarsc - Checking for presence of character variables

This code is also available in: Deutsch Español Français
This functional-style macro checks for the presence of a list of character-type variables in a specified SAS© dataset. It relies on the sub-macros '%match' and '%varlistc'. If variables do not match, they are stored in the global macro variable '_nomatch_'. The macro handles case sensitivity via the 'casesens' parameter.
Data Analysis

Type : MIXTE


The macro does not use hardcoded data but operates on the dataset passed as a parameter ('ds' argument). It analyzes the metadata (variable names) of this dataset.

1 Code Block
MACRO
Explanation :
Definition of the '%hasvarsc' macro. It initializes parameters, calls the '%varlistc' macro to get the character variables from the target dataset, and uses the '%match' macro to compare this list with the required list ('varlist'). It returns 1 if all variables are found, otherwise 0 (with a warning message if no character variables exist in the source).
Copied!
1%put MACRO CALLED: hasvarsc v2.0;
2 
3%macro hasvarsc(ds,varlist,casesens=no);
4 %local varmatch varlistc;
5 %IF not %LENGTH(&casesens) %THEN %let casesens=no;
6 %let casesens=%upcase(%substr(&casesens,1,1));
7 %let varlistc=%varlistc(&ds);
8 %IF not %LENGTH(&varlistc) %THEN %DO;
9%put NOTE: (hasvarsc) There are no character variables in the INPUT dataset therefore;
10%put NOTE: (hasvarsc) the character variable(s) you are testing for will not be found.;
11 %let varmatch=%match(,&varlist,casesens=&casesens);
120
13 %END;
14 %ELSE %DO;
15 %let varmatch=%match(&varlistc,&varlist,casesens=&casesens);
16 %IF not %LENGTH(&_nomatch_) %THEN 1;
17 %ELSE 0;
18 %END;
19%mend hasvarsc;
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 : Roland Rashleigh-Berry (Public Domain)