DATA Step, Macro, Functions and more

a macro code

Reply
Regular Contributor
Posts: 190

a macro code

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.

Regular Contributor
Posts: 190

Re: a macro code

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.
Frequent Contributor
Posts: 85

Re: a macro code

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;

 

Regular Contributor
Posts: 190

Re: a macro code

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
Contributor ndp
Contributor
Posts: 61

Re: a macro code

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;
Ask a Question
Discussion stats
  • 4 replies
  • 182 views
  • 0 likes
  • 3 in conversation