BookmarkSubscribeRSS Feed
RTelang
Fluorite | Level 6

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.

4 REPLIES 4
RTelang
Fluorite | Level 6
my code to check variable type-->
%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.";
%mend vtype;
-->How can i edit this code to identify 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.
JerryLeBreton
Pyrite | Level 9

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 &notmissing = 0 %then

   %put WARNING: All &varname values are missing;

 

%mend vtype;

 

RTelang
Fluorite | Level 6
hello @Jerry applied your logic but the code did not work

data maini;
input ID var1 var2 var3 var4;
cards;
1 6 4 4 5
6 5 4 . 5
3 7 9 . .
7 9 4 2 6
run;
proc contents data=work.main0;
run;

%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 &notmissing = 0 %then
%put WARNING: All &varname values are missing;
%mend vtype;
%let vartyp = -99;
%vtype(dsname=work.maini, varname=var3, outputflag =vartyp);
%put var_type =&vartyp;

code log below there wasn't any error msg as the dataset variable var3 has missing values..

56 data maini;
57 input ID var1 var2 var3 var4;
58 cards;

NOTE: The data set WORK.MAINI has 4 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.02 seconds

63 run;

64 proc contents data=work.main0;
65 run;

NOTE: PROCEDURE CONTENTS used (Total process time):
real time 0.26 seconds
cpu time 0.27 seconds


66
67 %macro vtype(dsname=, varname=, outputflag=);
68 %if %sysfunc(exist(&dsname)) %then %do;
69 %let dsid = %sysfunc(open(&dsname));
70 %let varnum = %sysfunc(varnum(&dsid,&varname));
71 %let vartyp = %sysfunc(vartype(&dsid,&varnum));
72 %if &vartyp = C %then %do;
73 %let &outputflag=&vartyp;
74 %put &varname=character;
75 %end;
76 %else %if &vartyp = N %then %do;
77 %let &outputflag=&vartyp;
78 %put &varname=numeric;
79 %end;
80 %end;
81 %else %put "DSNAME parameter is invalid Please pass appropriate values.";
82 proc sql noprint;
83 select count(*)
84 into :notmissing
85 from &dsname
86 where &varname is not missing;
87 quit;
88 %if &notmissing = 0 %then
89 %put WARNING: All &varname values are missing;
90 %mend vtype;
91 %let vartyp = -99;
92 %vtype(dsname=work.maini, varname=var3, outputflag =vartyp);
var3=numeric
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


93 %put var_type =&vartyp;
var_type =N
ndp
Quartz | Level 8 ndp
Quartz | Level 8

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 &notmissing = 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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1606 views
  • 0 likes
  • 3 in conversation