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-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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