0
votes

Suppose, I have the following output from calling proc contents:

Name Type Format
  x    1    DATETIME
  y    2        $

Depending on the format I want to call different macros. Let's say I have two macros, %date(var = ) and %rest(var = ). Eventually, I would like to call the macro %date with name x and the macro %rest with name y. I hope it's more or less clear what I am trying to do. Thank you!

1

1 Answers

1
votes

Yes, this is possible, but rather than trying to process the out of PROC CONTENTS, you would typically request an output data set from PROC CONTENTS and then process that further:

proc contents data=mydata out=mycontents noprint;
run;

The NOPRINT option suppresses the normal printed output. The MYCONTENTS data set will contain a row for each variable in the MYDATA data set, with columns for type, length, format etc. You can then process that further however you wish, calling your %date() macro or whatever.

EDIT:

Here is a more complete program showing how to add a result variable to the contents data set, using a separate macro for each data type. Note that there's no particular need to use macros in this code.

%macro num_macro();
    result = 'I''m a numeric';
%mend;

%macro char_macro();
    result = 'I''m a character';
%mend;

data in;
  attrib datevar length=8 format=date9.;
  attrib charvar length=$ 20;
  attrib numvar length=8;
run;

proc contents data=in out=contents noprint;
run;

data contents_with_result;
  length result $ 40;
  set contents (keep = name type);
  if type=1 then do;
    %num_macro();
  end;
  else do;
    %char_macro();
  end;
run;

proc print;
run;

This uses the two small macros to encapsulate the different code required for each data type, but note that this isn't calling the macros once per record in the data set - the macros are called exactly once each when the data step is being parsed/compiled, and then the code the macros generated is executed once per record in the input data set.

There are ways to have a macro called once per record in an input data set, but that approach is only needed in quite specialised cases. If you want to do some per-record processing on data that is already in a data set, you should almost always do it with a data step if at all possible.