How to Print Data values into the SAS Log

SAS internally generates logs while executing SAS statements and writes them into the SAS LOG window or any specified external log file.

The level of logging depends on the SAS system configuration or SAS options specified in the SAS program itself. But none of them prints actual data into the log window or log file. 

In order to print variables data values into SAS log, to the SAS output window, or to an external location you need to use the PUT statement. 

The PUT statement can write lines that contain variable values, character strings, and hexadecimal character constants. With the PUT statement, you specify what to write, where to write, and how to format and print data values in SAS.

Syntax:

PUT ‘message to print’ SAS data set variable(s) or macro variable(s) ;

The below example, creates a new data set named showtime with two variables dt for date and dttm for date and time. With the PUT statement you write its actual variable values and some formatted variable values as well.

data showdate;
  dt=today();
  dttm=datetime();
  put 'SAS date=' dt;
  put 'SAS datetime=' dttm;
  put 'formatted date=' dt date9.;
  put 'formatted datetime=' dttm datetime22.;
run;

This is how the SAS data set SHOWDATE looks like:

print showdate dataset values in SAS

Observe the LOG window where you can see the printed data values in SAS log generated by PUT statements.

print showdate dataset values in SAS LOG

4 Ways to Print Variable Data values in SAS 

There are four ways to write SAS variable data values into SAS log using PUT statement:

  1. Column output
  2. List output
  3. Formatted output
  4. Named output

To demonstrate how these 4 methods work, let’s create a sample data set and try to print variable data values into the SAS log.

You can literally print variable values while creating a data set by reading raw data through datalines statement or by reading/importing an external input file.

PUT _all_ ;

This could be the easiest way to print all the variable data values into the SAS log.

data employee;
  input name $ 1-18 age 8. DOB date9.;
  format DOB date9.;
  PUT _all_; /* print values of all the variables in SAS log*/

datalines;
Joseph                  31   11Jun1992
Mitchel                 26   26Sep1997
Sue Ellen               44   01Feb1977
Karl Jonas              28   23Nov1993
Erik Pet                46   09Mar1970
;
run;

Employee output data set:

print SAS variable data values in SAS dataset

Observe the Logs generated by PUT _all_ statement. It prints data values for all the variables in the given data set.

print SAS variable data values in SAS LOG

Let’s look at even more creative ways to print variable data values into the SAS log using different methods.

Since we already have a data set created above with the name “employee” we will use data _null_ statement to avoid creating any new data set while printing variable values into the SAS log.

In case if you haven’t created employee data set then let’s create it first.

data employee;
  input name $ 1-18 age 8. DOB date9.;
  format DOB date9.;

datalines;
Joseph                  31   11Jun1992
Mitchel                 26   26Sep1997
Sue Ellen               44   01Feb1977
Karl Jonas              28   23Nov1993
Erik Pet                46   09Mar1970
;
run;

1. Column Output – to print data into SAS log

In column output, column numbers follow the variable in the PUT statement. With the column numbers you tell SAS from which position it should print variable values in the log.

In the below example, SAS will start printing values for the variable “name” from the position 5th until 20th and from 25th until 30th for the variable Age.

The PUT statement writes values for NAME and AGE in the specified columns in the log.

data _null_;
set employee;
put name 5-20 age 25-30;
run;
Print SAS data values in SAS - Column output

If you notice, usually SAS prints information from the beginning of the line but in this case SAS has printed name variable values from the specified position that is 5th position because give range started from 5 to 20.

2. List Output – to print data into SAS log

In the list output, you can specify a list of columns and their very specific order in which you want to print data values in the log. 

For example you can write values for variable name followed by DOB, followed by age in the log window or file.

data _null_;
set employee;
put name DOB age;
run;
Print SAS data values in SAS - List output

3. Formatted Output – to print data into SAS log

In the formatted output, you get the option to specify SAS data format or user written format after each variable name. The below example prints the name variable in $char20. format, age in 2. format and DOB in ddmmyy10. format.

You can also override the default alignment and add some extra space between the two values of the variable using + (number). In this example we are adding 5 blank spaces between age and DOB. 

data _null_;
set employee;
put name $char30. age 2. +5 DOB mmddyy10.;
run;
Print SAS data values in SAS - Formatted output

4. Named Output – to print data into SAS log

In the names output, you can list the variable names followed by en equal to sign. For example, the below PUT statement will write the data values for NAME and AGE to the sas log.

data _null_;
set employee;
put name= age=;
run;
Print SAS data values in SAS - Named output

In case if you want to print values for all the variables in this format then you can use the PUT _ALL_ statement. It will do the magic for you.

data _null_;
set employee;
put _all_;
run;
Print values for all the SAS variables

Now you know how to print data values in SAS log. It is even easy to export this information and save it in the external log file.

FAQ

Can we print all the input data in SAS log file?

Yes. You can print entire data set or input file information along with its content in the log file using PUT statement. Use this SAS statement:

 

PUT _ALL_ ;
How many ways you can write data into SAS log file?

There are total 4 ways you can write data into SAS log file:

  1. Column output
  2. List output
  3. Formatted output
  4. Named output