Hello, so I was working on this macro to print the results of the id and each variable in a dataset if it is empty.
%macro filter_and_print(dataset, new_dataset, var_id);
%local dsid varnum varname vartype filter_condition;
/* Open the dataset */
%let dsid = %sysfunc(open(&dataset));
/* Check if dataset is opened successfully */
%if &dsid %then %do;
/* Get the number of variables in the dataset */
%let varnum = %sysfunc(attrn(&dsid, nvars));
/* Loop through variables */
%do i = 1 %to &varnum;
/* Get variable name and type */
%let varname = %qscan(%sysfunc(varname(&dsid, &i)), 1);
%let vartype = %sysfunc(vartype(&dsid, &i));
/* Determine filter condition based on variable type */
%if &vartype = C %then %do;
%let filter_condition = %sysfunc(quote(&varname)) = ' ';
%end;
%else %if &vartype = N %then %do;
%let filter_condition = %sysfunc(quote(&varname)) = .;
%end;
/* Create filtered dataset */
data &new_dataset;
set &dataset;
if &filter_condition;
run;
/* Print filtered dataset */
proc print; var &var_id &varname;
run;
%end;
/* Close the dataset */
%let dsid = %sysfunc(close(&dsid));
%end;
%else %put ERROR: Unable to open dataset &dataset;
%mend;
Right, so it does loop through each variable, if it is a character variable it checks if there is an empty string, if it is a numerical variable it checks if it is equal to .
Okay, so in the log, I get messages like
But using proc contents on the original dataset, I do see that the variable in question is numerical.
And I see the Note that says character values have been converted to numerical, but why would it be doing that?
I wouldn't recommend running the program with a large dataset though as it does take a bit of time to fully execute. I do get my printed results at the end of running the program, however it does not print out the empty/missing values as expected. It instead returns every observation but filtered by the id variable and the variable being observed.
Any help would be greatly appreciated. Thank you! 😁
... View more