BookmarkSubscribeRSS Feed
taylormccormick
Calcite | Level 5

I'm trying to understand the output statement and the PDV.  

If I don't include the output statement after each assignment, the output data looks like this...

Code:
DATA WORK.HRLeaders2;
LENGTH Player $ 7;
Season=2016; Player='Trumbo'; HR=47;
Season=2017; Player='Stanton'; HR=59;
Season=2018; Player='Davis'; HR=48;
RUN;
Output:
Obs Player Season HR1
Davis201848

 

If I include after each statement it looks like this...

Code:
DATA WORK.HRLeaders2;
LENGTH Player $ 7;
Season=2016; Player='Trumbo'; HR=47;OUTPUT;
Season=2017; Player='Stanton'; HR=59;OUTPUT;
Season=2018; Player='Davis'; HR=48;OUTPUT;
RUN;
Output:
Obs Player Season HR123
Trumbo201647
Stanton201759
Davis201848
 

If I include after first statement only, it looks like this...

Code:
DATA WORK.HRLeaders2;
LENGTH Player $ 7;
Season=2016; Player='Trumbo'; HR=47; OUTPUT;
Season=2017; Player='Stanton'; HR=59;
Season=2018; Player='Davis'; HR=48;
RUN;
Output:
Obs Player Season HR1
Trumbo201647

 

Why wouldn't it output both the first and last observation in the output data set?  And are the other two observations then "lost" (created temporarily but gone because I didn't output them to a dataset)?

 

Thanks!  I know it's a simple question, just trying to understand.

 
2 REPLIES 2
Reeza
Super User
Once you have an explicit OUTPUT statement it overrides the default implicit OUTPUT statement which is at the RUN line.

So in the first set of code you have a single implicit output at the end, at the RUN. There is no loop as each line was overwritten with the previous value so only the last value is printed.

In the third example, your explicit OUTPUT statement overrides the implicit OUTPUT. It is only present once and dumps the current values. Since there is no other OUTPUT statement no other records are written to the data set.
CurtisMackWSIPP
Lapis Lazuli | Level 10

Rezza is of course correct.

 

Remember, the data step is an implied loop through every record in the input datasets.  If while in a DATA step loop it executes a SET, MERGE, UPDATE, MODIFY, or INPUT statement, it reads the next record into the PDV. If there is another records after the one read, it remembers that.  It then progress through the rest of the data step code.  If it finds an OUTPUT statement, it writes out the current record and remembers it has been written.  When it hits a "run" statement (or another DATA or PROC statement if you forget the RUN), and it hasn't already been output, it outputs the record.  If there are still more records from the last input dataset, it goes back to the top of the code and starts over.  You can have more than one OUTPUT and it will output the record every time it sees one.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 492 views
  • 0 likes
  • 3 in conversation