DATA Step, Macro, Functions and more

Macro to automate checks on columns

Reply
Occasional Contributor
Posts: 11

Macro to automate checks on columns

Hi,

I have been given a massive table (800+ columns, over 200,000 rows) with both character and numeric columns. I have been asked to find the proportion of missing data in each column, both overall and the average annually, and present the results in a table.

I have written macros for both numeric and character variables which will produce a row of the required data and append it to a table which will become the final table.

However, at the moment I will have to write all 800+ variable names into the macros one by one and then run it all at once.

Is there any way to write a macro that will run through the variables, decide if the current variable is numeric or character, and put it into the appropriate macro?

For example, something like:

%macro master_macro(dataset, date_var);

while(current variable exists)

    if the current variable is numeric do

        my_numeric_macro(dataset, current variable, date_var);

    else do

        my_character_macro(dataset, current variable, date_var);

    current variable = next variable

%mend

 

Apologies for the sloppy pseudo code! 

Super User
Posts: 6,624

Re: Macro to automate checks on columns

If you search the forum here, you will probably find that this question has been addressed in ways that are shorter ... you don't need to name each variable individually.  However, if that doesn't appeal to you, the easiest route might be to divide the variables into separate data sets:

 

data numerics (keep=_numeric_)

characters (keep=_character_);

set have;

run;

 

Now you are guaranteed that every variable in one data set is numeric, and every variable in the other data set is character.

Super User
Super User
Posts: 9,397

Re: Macro to automate checks on columns

You do not need macro at all.  Your problem can be broken down into two simple steps - get nmiss of number, get nmiss of characters, for instance, produce a report of missing numbers:

proc means data=have nmiss;
  var _numeric_;
run;

There are loads of very simple methodolgies for doing what you want, for instance:

https://stats.idre.ucla.edu/sas/faq/how-can-i-see-the-number-of-missing-values-and-patterns-of-missi...

 

You can use means, summary, freq, datastep, sql, etc.

Ask a Question
Discussion stats
  • 2 replies
  • 89 views
  • 0 likes
  • 3 in conversation