Search notes:

SAS statements: put

The put statement, by default, writes text into the log. The destination can be changed with the file statement.
There are four put styles:
Note: there is also a function with the name put. This function can be used to convert between data types.

Destination

The destination of the text of a put statement by default goes to the log. It can be changed with the file statement. (See Change destination of put)

put all variables names with values

data _null_;

  answer      = 42;
  answer_text = "forty-two";
  x           = "eggs";

  put _ALL_;

run;
Github repository about-SAS, path: /programming/statements/put/__ALL__.sas

Named put

The put var= idiom prints the name and the value of a variable:
data _null_;

  a = 7;
  b = 6;

  answer = a*b;

  put answer=;

run;
Github repository about-SAS, path: /programming/statements/put/named-output.sas

Column put

data _null_;

  do i = 1 to 10;

     j = i**2;
     k = i**3;

     put i  1- 2
         j  5- 7
         k 10-13;
     
  end;

run;

/*
 1    1     1
 2    4     8
 3    9    27
 4   16    64
 5   25   125
 6   36   216
 7   49   343
 8   64   512
 9   81   729
10  100  1000
*/
Github repository about-SAS, path: /programming/statements/put/column/start-end.sas
In column puts, formats are not applied:
data _null_;

   format var_fmt ddmmyy10.;

   var_num =   42;
   var_fmt = 4242;

/* Named put, format is applied:                                       */
   put var_num= var_fmt=; /*             var_num=42 var_fmt=13/08/1917 */

/* Column put, format is not applied:                                  */
   put var_num 1-10 var_fmt 13-23;               42         4242       */

run;
Github repository about-SAS, path: /programming/statements/put/column/format-not-applied.sas
Without explictitely stating the number of digits after the decimal point, column put will omit the decimal point and the decimals:
data _null_;
         val = 12.3456;
     put val   2-8;    /*       12 */
     put val   2-8 .4; /*  12.3456 */
     put val   2-8 .2; /*    12.35 */
run;
Github repository about-SAS, path: /programming/statements/put/column/decimal-point.sas

Formatted put

With formatted put, a format can by dynamically applied to a variable in a put statement:
data _null_;
   val=4242;

   put val;             /* 4242            */
   put val deudfdd10.;  /* 13.08.1917      */
   put val time10.;     /*    1:10:42      */

/* Combining named and formatted put:      */
   put val=;            /* val=4242        */
   put val= deudfdd10.; /* val=13.08.1971  */
   put val= time10.;    /* val=1:10:42     */
run;
Github repository about-SAS, path: /programming/statements/put/formatted/basic.sas

Pointer control

During the subsequent executions of put statement, SAS maintains a column and a line pointer that control where the output of the next put statement goes to.
The column pointer is moved to a specific column with @expression. expression can be, for example a constant (@42), the value of a variable @var or the result of an expression within parenthesis: @(foo+bar+3).
The column pointer can be moved relatively with +expression.
The line pointer can be moved to a specific line with #expression.
The line pointer is moved to the next line with /.

expression

/* +(expr) moves the pointer to the column expr. */

data _null_;

  do pos=1 to 20;
     put +(pos) '*';
  end;

run;

Absolute control

/* @n moves the pointer to the absolute column n */

data _null_;

  a1 =   42; a2 = "hello world"; a3 =   9;
  b1 = 4711; b2 = "foo"        ; b3 = 111;

  put @1 a1 @6 a2 @20 a3;
  put @1 b1 @6 b2 @20 b3;
run;

Relative columns

/* +n moves the pointer n columns.

   The idiom +(-1) cancels the default space
   between variables.

*/

data _null_;

  a1 =   42; a2 = "hello world"; a3 =   9;
  b1 = 4711; b2 = "foo"        ; b3 = 111;

  put @1 a1 +4 a2 +(-1) a3;
  put @1 b1 +4 b2 +(-1) b3;
run;

Holding the line

data _null_;
   
/* A trailing at sign (line-hold specifier) does not put
   a new-line (carriage return), the next the put write
   on the same line */
 
   put "foo" @;
   put "bar" @;
   put "baz";

   put "one" @;
   put "two" @;
   put "three";

run;

Right aligned

data _null_;

  do i = 1 to 10;

     j = i**2;
     k = i**3;

     put @1  i 2.-r
         @4  '|'
         @6  j 3.-r
         @10 '|'
         @12 k 4.-r;

  end;

run;

/*
 1 |   1 |    1
 2 |   4 |    8
 3 |   9 |   27
 4 |  16 |   64
 5 |  25 |  125
 6 |  36 |  216
 7 |  49 |  343
 8 |  64 |  512
 9 |  81 |  729
10 | 100 | 1000
*/

Arrays

Put elements of an array;
data _null_;

  array fib[10];

  fib[1] = 0;
  fib[2] = 1;

  do i = 3 to dim(fib);
    fib[i] = fib[i-2] + fib[i-1];
  end;

* put each element of array fib ;
  put (fib[*]) (=);

* put some selected elements of array ;
  put (fib4 fib7 fib8) (=);

* put values on seperate lines ;
  put (fib[*]) (=/);

run;
Github repository about-SAS, path: /programming/statements/put/values-of-array.sas

Ampersand equal

%let   tq84_macro_var=foo bar baz;

* Show macro variable name AND its value: ;
%put &=tq84_macro_var;
Github repository about-SAS, path: /macro-processor/statements/put/ampersand-equal.sas

Times

data _null_;
  put 50*'*-';
run;
Github repository about-SAS, path: /programming/statements/put/times.sas

Variable

%let  tq84_macro_var=foo bar baz;
%put &tq84_macro_var;
Github repository about-SAS, path: /macro-processor/statements/put/variable.sas

put _infile_

put _infile_ copies the data of the input record buffer to the log file (or the output destination that is in effect).
This abbreviation can be used, for example, to copy a file in a data step.

See also

%put
SAS statements

Index