Previous Page | Next Page

Macro Statements

%PUT Statement



Writes text or macro variable information to the SAS log.
Type: Macro statement
Restriction: Allowed in macro definitions or open code

Syntax
Details
Examples
Example 1: Displaying Text
Example 2: Displaying Automatic Variables
Example 3: Displaying User-Generated Variables
Example 4: Displaying Local Variables

Syntax

%PUT <text | _ALL_ | _AUTOMATIC_ | _GLOBAL_ | _LOCAL_ | _USER_>;

no argument

places a blank line in the SAS log.

text

is text or a text expression that is written to the SAS log. If text is longer than the current line size, the remainder of the text appears on the next line. The %PUT statement removes leading and trailing blanks from text unless you use a macro quoting function.

_ALL_

lists the values of all user-generated and automatic macro variables.

_AUTOMATIC_

lists the values of automatic macro variables. The automatic variables listed depend on the SAS products installed at your site and on your operating system. The scope is identified as AUTOMATIC.

_GLOBAL_

lists user-generated global macro variables. The scope is identified as GLOBAL.

_LOCAL_

lists user-generated local macro variables. The scope is the name of the currently executing macro.

_USER_

describes user-generated global and local macro variables. The scope is identified either as GLOBAL, or as the name of the macro in which the macro variable is defined.


Details

When you use the %PUT statement to list macro variable descriptions, the %PUT statement includes only the macro variables that exist at the time the statement executes. The description contains the macro variable's scope, name, and value. Macro variables with null values show only the scope and name of the variable. Characters in values that have been quoted with macro quoting functions remain quoted. Values that are too long for the current line size wrap to the next line or lines. Macro variables are listed in order from the current local macro variables outward to the global macro variables.

Note:   Within a particular scope, macro variables might appear in any order, and the order might change in different executions of the %PUT statement or different SAS sessions. Do not write code that depends on locating a variable in a particular position in the list.  [cautionend]

The following figure shows the relationship of these terms.

%PUT Arguments by Type and Scope

[%PUT Arguments by Type and Scope]

The %PUT statement displays text in different colors to generate messages that look like ERROR, NOTE, and WARNING messages generated by SAS. To display text in different colors, the first word in the %PUT statement must be ERROR, NOTE, or WARNING, followed immediately by a colon or a hyphen. You might also use the national-language equivalents of these words. When you use a hyphen, the ERROR, NOTE, or WARNING word is blanked out.

Note:   If you use the %PUT statement and the last message text that was generated by the SYSWARNINGTEXT and SYSERRORTEXT automatic macro variables contained an & or %, you must use the %SUPERQ macro quoting function. For more information, see SYSERRORTEXT Automatic Macro Variable and SYSWARNINGTEXT Automatic Macro Variable.   [cautionend]


Examples


Example 1: Displaying Text

The following statements illustrate using the %PUT statement to write text to the SAS log:

%put One line of text.;
%put %str(Use a semicolon(;) to end a SAS statement.);
%put %str(Enter the student%'s address.);

When you submit these statements, these lines appear in the SAS log:

One line of text.
Use a semicolon(;) to end a SAS statement.
Enter the student's address.


Example 2: Displaying Automatic Variables

To display all automatic variables, submit

%put _automatic_;

The result in the SAS log (depending on the products installed at your site) lists the scope, name, and value of each automatic variable:

AUTOMATIC SYSBUFFR
AUTOMATIC SYSCMD
AUTOMATIC SYSDATE 21JUN97
AUTOMATIC SYSDAY Wednesday
AUTOMATIC SYSDEVIC
AUTOMATIC SYSDSN         _NULL_
AUTOMATIC SYSENV FORE
AUTOMATIC SYSERR 0
AUTOMATIC SYSFILRC 0
AUTOMATIC SYSINDEX 0
AUTOMATIC SYSINFO 0


Example 3: Displaying User-Generated Variables

This example lists the user-generated macro variables in all scopes.

%macro myprint(name);
   proc print data=&name;
      title "Listing of &name on &sysdate";
      footnote "&foot";
   run;
   %put _user_;
%mend myprint;

%let foot=Preliminary Data;

%myprint(consumer)

The %PUT statement writes these lines to the SAS log:

MYPRINT NAME consumer
GLOBAL FOOT Preliminary Data

Notice that SYSDATE does not appear because it is an automatic macro variable.

To display the user-generated variables after macro MYPRINT finishes, submit another %PUT statement.

%put _user_;

The result in the SAS log does not list the macro variable NAME because it was local to MYPRINT and ceased to exist when MYPRINT finished execution.

GLOBAL FOOT Preliminary Data


Example 4: Displaying Local Variables

This example displays the macro variables that are local to macro ANALYZE.

%macro analyze(name,vars);

   proc freq data=&name;
      tables &vars;
   run;

   %put FIRST LIST:;
   %put _local_;

   %let firstvar=%scan(&vars,1);

   proc print data=&name;
      where &firstvar ne .;
   run;

   %put SECOND LIST:;
   %put _local_;

%mend analyze;

%analyze(consumer,car house stereo)

In the result that is printed in the SAS log, the macro variable FIRSTVAR, which was created after the first %PUT _LOCAL_ statement, appears only in the second list.

FIRST LIST:
ANALYZE NAME consumer
ANALYZE VARS car house stereo

SECOND LIST:
ANALYZE NAME consumer
ANALYZE VARS car house stereo
ANALYZE FIRSTVAR car

Previous Page | Next Page | Top of Page