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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.