/********************************************************************************* * Macro: NVAR.SAS * * Location: P:\SAS RESOURCES\CODE LIBRARY\DWARD * * Client: --- * * Protocol: --- * * Author: D. WARD * * Created: 1/98 * * Summary: Resolve to the number of variables of a SAS dataset * * Parameters: 2) dataset (positional), like: wildcard: * * * * Uses for like operator: * * blank returns number of variables in dataset * * _numeric_ returns number of numeric variables * * _character_ returns number of character variables * * p* returns number of vars in mydata beginning with "P" * * *p returns number of vars in mydata ending with "P" * * *p* returns number of vars in mydata containing "P" * * p*s returns number of vars in mydata beginning with "P" and ending with "S" * *********************************************************************************/ %macro nvar (dataset,like); %if %quote(&like)^= %then %let like=%upcase(%sysfunc(translate(&like,¤,*))); %if %sysfunc(exist(&dataset))=0 and %sysfunc(exist(&dataset,VIEW))=0 %then %do; %put ** Macro nvar() requires one parameter, a valid SAS dataset name.; %put ** It also takes a second parameter, a "like" operator.; %put ** Valid like operators: _NUMERIC_,_CHARACTER_,COL*,*COL,*COL*,C*L; %put ** Example: nvar(mylib.data), nvar(mylib.data,_numeric_); %end; %else %if &like^= %then %do; /**** CASE: LIKE OPERATOR SPECIFIED ****/ %local _dsid _rc _nv _vname; %let _dsid=%sysfunc(open(&dataset)); %let _nvar=%sysfunc(attrn(&_dsid,NVAR)); %let _nv=0; /**** CASE: ASTERIK IN LIKE OPERATOR (SOUNDS LIKE) ****/ %if %index(&like,¤)>0 %then %do; %let _like2=%sysfunc(translate(%quote(&like),%str( ),¤)); %do _i=1 %to &_nvar; %let _vname=%sysfunc(varname(&_dsid,&_i)); /** CASE: ASTERIK AT FIRST AND LAST POSITION (INDEX FUNCTION) **/ %let _t1=%eval(%index(&like,¤)=1); %let _t2=%eval(%substr(&like,%length(&like),1)=¤); %if &_t1 and &_t2 %then %do; %if %index(&_vname,&_like2)>0 %then %let _nv=%eval(&_nv+1); %end; /** CASE: ASTERIK AT FIRST POSITION (STARTS WITH) **/ %else %if &_t1 %then %do; %if %length(&_vname)>=%length(&_like2) %then %do; %if %quote(%substr(&_vname,%eval(%length(&_vname)-%length(&_like2)+1),%length(&_like2))) =%quote(&_like2) %then %let _nv=%eval(&_nv+1); %end; %end; /** CASE: ASTERIK AT LAST POSITION (ENDS WITH) **/ %else %if &_t2 %then %do; %if %length(&_vname)>=%length(&_like2) %then %do; %if %quote(%substr(&_vname,1,%length(&_like2)))=%quote(&_like2) %then %let _nv=%eval(&_nv+1); %end; %end; /** CASE: ASTERIK IN MIDDLE OF EXPRESSION - MATCH BOTH ENDS OF VARIABLE NAME **/ %else %if %index(&like,¤)>0 %then %do; %let _t1=%scan(&like,1,¤); %let _t2=%scan(&like,2,¤); %if &_t1=%quote(%substr(&_vname,1,%length(&_t1))) and &_t2=%quote(%substr(&_vname,%eval(%length(&_vname)-%length(&_t2)+1),%length(&_t2))) %then %let _nv=%eval(&_nv+1); %end; %end; /** RETURN ANSWER **/ &_nv %end; /**** CASE: NUMERIC OR CHARACTER SPECIFIED ****/ %else %if %quote(&like)=_NUMERIC_ or %quote(&like)=_CHARACTER_ %then %do; /** LOOP THROUGH VARIABLES AND COUNT NUMBER OF CHAR/NUM **/ %do _i=1 %to &_nvar; %let _vtype=%sysfunc(vartype(&_dsid,&_i)); %if &_vtype=C and %quote(&like)=_CHARACTER_ %then %let _nv=%eval(&_nv+1); %else %if &_vtype=N and %quote(&like)=_NUMERIC_ %then %let _nv=%eval(&_nv+1); %end; /** RETURN ANSWER **/ &_nv %end; /**** MAKE SURE TO CLOSE DATASET ****/ %let _rc=%sysfunc(close(&_dsid)); %end; %else %do; /**** NORMAL CASE: RETURN THE NUMBER OF TOTAL VARIABLES IN DATASET ****/ %local _dsid _rc; %let _dsid=%sysfunc(open(&dataset)); %sysfunc(attrn(&_dsid,NVAR)) %let _rc=%sysfunc(close(&_dsid)); %end; %mend nvar;