Categories‎ > ‎

Code Helpers

The following macro programs help the user write code by providing information.

Ext*

Outputs the extension for a file either as a macro variable or as a value. Use as a value (e.g., in a PUT statement).

FindTable

Finds a table within a library based on a search term.

FindVar

Finds a variable within a library based on a search term.

IfPut

Writes IF/THEN statements with PUT as the output. Optionally use ELSE statement. Useful for quickly writing programs to check something and output a message if the conditions are met.

LibTbl

Split the library and table into two global macro variables for later use.

NObs*

Output the number of observations in a data set. Use as a value (e.g., in a PUT statement).

NVars*

Output the number of variables in a data set. Use as a value (e.g., in a PUT statement).

PrintType

Prints a list of objects from the SASHELP.VCATALG table, which stores information about formats, macro programs, fonts, etc. that can help a user determine what to use or where to find it.

TableInfo*

Output information about a table, including engine, label, type, sort, library, etc. to help the user write code or to create dynamic programs. Use as a value (e.g., in a PUT statement). Note that NObs and NVars are essentially subsets of this macro.

VarInfo*

Output information about a variable, including type, length, format, etc. to help the user write code or to create dynamic programs. User as a value (e.g., in a PUT statement).


Notes

* These macro programs output a value, so they cannot be used independently (e.g., %NObs(table) ). (For a list of similar macro programs, see Functions.) They must be used where values can be output, for example:

data _null_;
    if %nobs(table) = 0 then put NOTE: There are no records in TABLE.;
run;

Or using the macro language:

%if %nobs(table) = 0 %then %put NOTE: There are no records in TABLE.;

Using IF/THEN logic, the user could write dynamic programs, for example, using the macro language:

%macro ReviewTable(table) / des='Review the contents of a table in different ways based on its size';

    /* Check if the table exists */
    %if %sysfunc(exist(&TABLE))=0 %then %do;
        %put %str(E)RROR: The table &TABLE does not exist.;
        %return; /* End the macro program */
    %end;

    /* End the macro if there are zero records */
    %if %nobs(&TABLE) = 0 %then %do;
        %put NOTE: There are no records in &TABLE.;
        %return; /* End the macro program */
    %end;

    /* Review single records in the log */
    %else %if %nobs(table) = 1 %then %do;

        %put NOTE: There is only one record in &TABLE.;

        /* Output the entire table */
        data _null_;
            set &TABLE;
            put _all_;
        run;

    %end;

    /* Review multiple records by opening the table */
    %else %if %nobs(&TABLE) < 100 %then %do;

        %put NOTE: There are less than 100 records and more than 1 record in &TABLE.;

        /* Open the table for review */
        dm "vt %sysfunc(compress(&tbl))" vt;

    %end;

    /* Output the contents for large tables */
    %else %do;

        %put NOTE: There are over 100 records in &TABLE.;

        /* Generate contents */
        proc contents data=&TABLE;
        run;

    %end;

%mend ReviewTable;