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.
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.
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:
"
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 *
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;
@junlue did any of responses help in any way?
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".
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.
You're absolutely right about that. The trailing @ should be removed.
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!
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.