Writing Lines to the SAS Log or to an Output File |
Writing a Character String |
In its simplest form, the PUT statement writes the character string that you specify to the SAS log, to a procedure output file, or to an external file. If you omit the destination (as in this example), then SAS writes the string to the log. In the following example, SAS executes the PUT statement once during each iteration of the DATA step. When SAS encounters missing values for MORNING_VALUES or EVENING_COPIES, the PUT statement writes a message to the log.
data _null_; length state $ 15; infile 'your-input-file'; input state $ morning_copies evening_copies year; if morning_copies=. then put '** Morning Circulation Figures Missing'; else if evening_copies=. then put '** Evening Circulation Figures Missing'; run;
The following output shows the results:
Writing a Character String to the SAS Log
93 data _null_; 94 length state $ 15; 95 infile 'your-input-file'; 96 input state $ morning_copies evening_copies year; 97 if morning_copies =. then put '** Morning Circulation Figures Missing'; 98 else 99 if evening_copies =. then put '** Evening Circulation Figures Missing'; 100 run; NOTE: The infile 'your-input-file' is: File Name=file-name, Owner Name=xxxxxx,Group Name=xxxx, Access Permission=rw-r--r--, File Size (bytes)=223 ** Evening Circulation Figures Missing ** Morning Circulation Figures Missing ** Morning Circulation Figures Missing NOTE: 7 records were read from the infile 'your-input-file'. The minimum record length was 30. The maximum record length was 31.
Writing Variable Values |
Writing a Character String to the SAS Log shows that the value for MORNING_COPIES is missing for two observations in the data set, and the value for EVENING_COPIES is missing for one observation. To identify which observations have the missing values, write the value of one or more variables along with the character string. The following program writes the value of YEAR and STATE, as well as the character string:
data _null_; length state $ 15; infile 'your-input-file'; input state $ morning_copies evening_copies year; if morning_copies =. then put '** Morning Circulation Figures Missing: ' year state; else if evening_copies =. then put '** Evening Circulation Figures Missing: ' year state; run;
Notice that the last character in each of the strings is blank. This is an example of list output. In list output, SAS automatically moves one column to the right after writing a variable value, but not after writing a character string. The simplest way to include the required space is to include it in the character string.
SAS keeps track of its position in the output line with a pointer. Another way to describe the action in this PUT statement is to say that in list output, the pointer moves one column to the right after writing a variable value, but not after writing a character string. In later parts of this section, you will learn ways to move the pointer to control where the next piece of text is written.
The following output shows the results:
Writing a Character String and Variable Values
164 data _null_; 165 length state $ 15; 166 infile 'your-input-file'; 167 input state $ morning_copies evening_copies year; 168 if morning_copies =. then put 169 '** Morning Circulation Figures Missing: ' year state; 170 else 171 if evening_copies =. then put 172 '** Evening Circulation Figures Missing: ' year state; 173 run; NOTE: The infile 'your-file-name' is: File Name=file-name, Owner Name=xxxxxx,Group Name=xxxx, Access Permission=rw-r--r--, File Size (bytes)=223 ** Evening Circulation Figures Missing: 1997 Massachusetts ** Morning Circulation Figures Missing: 1999 Alabama ** Morning Circulation Figures Missing: 1996 Alabama NOTE: 7 records were read from the infile 'your-input-file'. The minimum record length was 30. The maximum record length was 31.
Writing on the Same Line More than Once |
By default, each PUT statement begins on a new line. However, you can write on the same line if you use more than one PUT statement and at least one trailing @ ("at" sign).
The trailing @ is a type of pointer control called a line-hold specifier. Pointer controls are one way to specify where SAS writes text. In the following example, using the trailing @ causes SAS to write the item in the second PUT statement on the same line rather than on a new line. The execution of either PUT statement holds the output line for further writing because each PUT statement has a trailing @. SAS continues to write on that line when a later PUT statement in the same iteration of the DATA step is executed and also when a PUT statement in a later iteration is executed.
options linesize=80 pagesize=60; data _null_; length state $ 15; infile 'your-input-file'; input state $ morning_copies evening_copies year; if morning_copies =. then put '** Morning Tot Missing: ' year state @; if evening_copies =. then put '** Evening Tot Missing: ' year state @; run;
The following output shows the results:
Writing on the Same Line More than Once
157 options linesize=80 pagesize=60; 158 159 data _null_; 160 length state $ 15; 161 infile 'your-input-file'; 162 input state $ morning_copies evening_copies year; 163 if morning_copies =. then put 164 '** Morning Tot Missing: ' year state @; 165 if evening_copies =. then put 166 '** Evening Tot Missing: ' year state @; 167 run; NOTE: The infile 'your-input-file' is: File Name=file-name, Owner Name=xxxxxx,Group Name=xxxx, Access Permission=rw-r--r--, File Size (bytes)=223 ** Evening Tot Missing: 1997 Massachusetts ** Morning Tot Missing: 1999 Alabama ** Morning Tot Missing: 1996 Alabama NOTE: 7 records were read from the infile 'your-input-file'. The minimum record length was 30. The maximum record length was 31.
If the output line were long enough, then SAS would write all three messages about missing data on a single line. Because the line is not long enough, SAS continues writing on the next line. When it determines that an individual data value or character string does not fit on a line, SAS brings the entire item down to the next line. SAS does not split a data value or character string.
Releasing a Held Line |
In the following example, the input file has five missing values. One record has missing values for both the MORNING_COPIES and EVENING_COPIES variables. Three other records have missing values for either the MORNING_COPIES or the EVENING_COPIES variable.
To improve the appearance of your report, you can write all the missing variables for each observation on a separate line. When values for the two variables MORNING_COPIES and EVENING_COPIES are missing, two PUT statements write to the same line. When either MORNING_COPIES or EVENING_COPIES is missing, only one PUT statement writes to that line.
SAS determines where to write the output by the presence of the trailing @ sign in the PUT statement and the presence of a null PUT statement that releases the hold on the line. Executing a PUT statement with a trailing @ causes SAS to hold the current output line for further writing, either in the current iteration of the DATA step or in a future iteration. Executing a PUT statement without a trailing @ releases the held line.
To release a line without writing a message, use a null PUT statement:
put;
A null PUT statement has the same characteristics of other PUT statements: by default, it writes output to a new line, writes what you specify in the statement (nothing in this case), and releases the line when it finishes executing. If a trailing @ is in effect, then the null PUT statement begins on the current line, writes nothing, and releases the line.
The following program shows how to write one or more items to the same line:
If a value for MORNING_COPIES is missing, then the first PUT statement holds the line in case EVENING_COPIES is missing a value for that observation.
If a value for EVENING_COPIES is missing, then the next PUT statement writes a message and releases the line.
If EVENING_COPIES does not have a missing value, but if a message has been written for MORNING_COPIES (MORNING_COPIES=.), then the null PUT statement releases the line.
If neither EVENING_COPIES nor MORNING_COPIES has missing values, then the line is not released and no PUT statement is executed.
options linesize=80 pagesize=60; data _null_; length state $ 15; infile 'your-input-file'; input state $ morning_copies evening_copies year; if morning_copies=. then put '** Morning Tot Missing: ' year state @; if evening_copies=. then put '** Evening Tot Missing: ' year state; else if morning_copies=. then put; run;
The following output shows the results:
Writing One or More Times to a Line and Releasing the Line
7 data _null_; 8 length state $ 15; 9 infile 'your-input-file'; 10 input state $ morning_copies evening_copies year; 11 if morning_copies=. then put 12 '** Morning Tot Missing: ' year state @; 13 if evening_copies=. then put 14 '** Evening Tot Missing: ' year state; 15 else if morning_copies=. then put; 16 run; NOTE: The infile 'your-input-file' is: File Name=your-input-file, Owner Name=xxxxxx,Group Name=xxxx, Access Permission=rw-r--r--, File Size (bytes)=223 ** Evening Tot Missing: 1997 Massachusetts ** Morning Tot Missing: 1999 Alabama ** Morning Tot Missing: 1998 Alabama ** Evening Tot Missing: 1998 Alabama ** Morning Tot Missing: 1996 Alabama NOTE: 7 records were read from the infile 'your-input-file'. The minimum record length was 30. The maximum record length was 31.
Copyright © 2012 by SAS Institute Inc., Cary, NC, USA. All rights reserved.