CS计算机代考程序代写 MET MA 603: SAS Programming and Applications

MET MA 603: SAS Programming and Applications

MET MA 603:
SAS Programming and Applications

Retain and Sum Statements

1

1

The Retain Statement
By default, SAS processes data one observation at a time. After executing each statements in the Data step, SAS moves on to the next observation, setting initial values back to missing. Any information from the previous observation is lost.
The Retain Statement instructs SAS to remember the value of a variable from the previous observation. This allows the information to be used in processing the subsequent observations.
retain running_total 0 ;
In the retain statement, indicate the variable to be retained, as well as its initial value. More than one variable can be retained in the same statement.

2

2

The Sum Statement
The Sum Statement is a short-hand, special case of the running total version of the Retain statement. It allows for much of the code ordinarily needed to execute the Retain statement to be omitted.
running_total + number;
Note that the Sum statement only works to calculate a running total; for anything other than a running total, the full Retain statement must be used.
Also note, the Sum statement is different than the Sum function, and that the Sum statement does not actually use the word sum in its syntax.

3

3

The end= Option
When a dataset is read using the Set or Merge statement, the end= option can be used to instruct SAS to create a Boolean (1 or 0) variable that indicates whether or not the observation being processed is the last one in the input dataset.
Set Mydata.Integers end=eof;
“eof” (end-of-file) is a common choice of variable name used within the SAS literature, but any valid name can be used. Note that the variable does not appear in the dataset, similar to an automatic variable.

4

4

Summing by Weather Flag
Use the Loss.sas7bdat dataset for the following:
Use the Sum statement to calculate the total of the Amount. Your answer should only include the observation containing the total.
Use the Retain statement to calculate the total of the Amount for observations where the Date is after 2010.
Use the Retain statement to calculate the total of the Amount separately (in the same Data step) for when WeatherFlag=0 and for when WeatherFlag=1.

5

5

Readings

Textbook section (6th edition) 3.15
Textbook section (5th edition) 3.10

6

6

/docProps/thumbnail.jpeg