What does this Retain statement do in this Data Step?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

What does this Retain statement do in this Data Step?

data total_points (drop=TeamName); 
length Event3 Event2 Event1 8. TeamName ParticipantName $8. ;
format Event2 z8. ;
retain TeamName Count ;
	input TeamName $ Count @ ;
		do i = 1 to Count ;
			input ParticipantName $ Event1 Event2 Event3 ;
				TeamTotal + (Event1 + Event2 + Event3);
					 output ;
		end ;
datalines; 
Knights 1 
Sue 6 8 8 
Kings 1 
Jane 9 7 8 
Knights 4 
John 7 7 7 
Lisa 8 9 9 
Fran 7 6 6 
Walter 9 8 10
;
proc print data=total_points;
run;

I've run this code with and without the Retain statement. It made no difference. 


Accepted Solutions
Solution
‎04-11-2016 09:34 AM
Super Contributor
Posts: 438

Re: What does this Retain statement do in this Data Step?

[ Edited ]

Hi @junlue

 

The @ holds the inpout for the next INPUT statement in the same datastep iteration. A special case @@ holds the record even for the next iteration.

 

If the input is spread over consecutive records in the input you can choose to use multiple input statements but without the @. Or you use the '/' in INPUT that instructs to load the next record and start reading at pos 1.

 

Have (more than) a look at the doc as the INPUT statement is very rich and also fundamental. 

 

Regards,

- Jan.

View solution in original post


All Replies
Super Contributor
Posts: 438

Re: What does this Retain statement do in this Data Step?

[ Edited ]

Retain prevents the variable(s) from being initialized to MISSING every time the datastep iterates. This way you can carry a value over from one iteration step (observation) to the next. When a variable is also mentioned in an input statement (as is the case in your code) the value is overwritten as every execution of INPUT assigns a new value to these variables. So you can retain to your liking but you will not see the effect unless you dome stuff before the input statement. But that is not the case in your example.

 

In your example also note that RETAIN is always on for variables that are mentioned in a SET statement.

 

From the doc:

"It is redundant to name any of these items in a RETAIN statement, because their values are automatically retained from one iteration of the DATA step to the next:

  • variables that are read with a SET, MERGE, MODIFY or UPDATE statement
    ...

 "

 

Also, sometimes RETAIN can be usefull even in this scenario if one decides to influence the variable order in the dataset.

 

Hope this helps,

- Jan.

 

 * edited for accuracy; added link *

Super User
Posts: 5,498

Re: What does this Retain statement do in this Data Step?

Posted in reply to jklaverstijn

While all of this is true, it's important to note another aspect of the answer to your question.  In this DATA step, the RETAIN statement could be removed.  It's not doing anything useful.  The value of the retained variables gets replaced each time, so there is no benefit to RETAIN.

 

Also note, the variable TeamTotal is a running total across all teams and participants.  Since you are dropping TeamName, this is likely the result that you are looking for.  But if you really want a separate total for each team, you would need to add this statement before the first INPUT statement:

 

TeamTotal=0;

Super Contributor
Posts: 438

Re: What does this Retain statement do in this Data Step?

@junlue did any of responses help in any way?

Occasional Contributor
Posts: 10

Re: What does this Retain statement do in this Data Step?

Posted in reply to jklaverstijn

Not really.

 

But I'm also not quite sure what the "Trailing @" does as well.

 

As I understand it, the trailing @ holds a record from the raw dataset. 

 

Take say the record "knights 4".

 

Since this record is held in the input buffer, how can we input the other variables? The other variables are in the records below. But the input buffer is still occupied by the record "knights 4".

Solution
‎04-11-2016 09:34 AM
Super Contributor
Posts: 438

Re: What does this Retain statement do in this Data Step?

[ Edited ]

Hi @junlue

 

The @ holds the inpout for the next INPUT statement in the same datastep iteration. A special case @@ holds the record even for the next iteration.

 

If the input is spread over consecutive records in the input you can choose to use multiple input statements but without the @. Or you use the '/' in INPUT that instructs to load the next record and start reading at pos 1.

 

Have (more than) a look at the doc as the INPUT statement is very rich and also fundamental. 

 

Regards,

- Jan.

Super User
Posts: 5,498

Re: What does this Retain statement do in this Data Step?

You're absolutely right about that.  The trailing @ should be removed.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 421 views
  • 2 likes
  • 3 in conversation