a macro that identifies variables either character or numeric for which all of the observations are missing values. When this occurs, a warning message is output to the SAS log.
Just add this to your macro...
%macro vtype(dsname=, varname=, outputflag=);
%if %sysfunc(exist(&dsname)) %then %do;
%let dsid = %sysfunc(open(&dsname));
%let varnum = %sysfunc(varnum(&dsid,&varname));
%let vartyp = %sysfunc(vartype(&dsid,&varnum));
%if &vartyp = C %then %do;
%let &outputflag=&vartyp;
%put &varname=character;
%end;
%else %if &vartyp = N %then %do;
%let &outputflag=&vartyp;
%put &varname=numeric;
%end;
%end;
%else %put "DSNAME parameter is invalid Please pass appropriate values.";
proc sql noprint;
select count(*) into :notmissing from &dsname where &varname is not missing;
quit;
%if ¬missing = 0 %then
%put WARNING: All &varname values are missing;
%mend vtype;
Here is a slightly modified version that will take care of wrong dataset/variable name:
%macro vtype(dsname=, varname=);
%if %sysfunc(exist(&dsname)) %then %do;
%let dsid = %sysfunc(open(&dsname));
%let varnum = %sysfunc(varnum(&dsid,&varname));
%if &varnum <=0 %then %do;
%put W A R N I N G: VARNAME parameter is invalid Please pass appropriate values.;
%end;
%else %do;
%let vartyp = %sysfunc(vartype(&dsid,&varnum));
%if &vartyp = C %then %do;
%let outputflag=Charater;
%end;
%else %if &vartyp = N %then %do;
%let outputflag=Numeric;
%end;
proc sql noprint;
select count(*) into :notmissing from &dsname where &varname is not missing;
quit;
%if ¬missing = 0 %then %put W A R N I N G: All &varname &outputflag. values are missing;
%else %do; %put N O T E: Atleast some &varname &outputflag. values are not missing; %end;
%end;
%end;
%else %put W A R N I N G: DSNAME parameter is invalid Please pass appropriate values.;
%mend vtype;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.