BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
junlue
Fluorite | Level 6
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. 

1 ACCEPTED SOLUTION

Accepted Solutions
jklaverstijn
Rhodochrosite | Level 12

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

6 REPLIES 6
jklaverstijn
Rhodochrosite | Level 12

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 *

Astounding
PROC Star

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;

jklaverstijn
Rhodochrosite | Level 12

@junlue did any of responses help in any way?

junlue
Fluorite | Level 6

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".

jklaverstijn
Rhodochrosite | Level 12

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.

Astounding
PROC Star

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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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