Add-Innovation Home

About Add-Innovation

Contact Us

 

 SAS Institute logo.    USE OF RETAIN.

The first two considerations save relatively little CPU per iteration of the data step (although they may amount to a significant saving for a high number of input records), but they are actually quicker to implement than their slower counterparts.

 The retain statement simply tells SAS not to reset the value of any variable listed upon the next iteration of the data step. Since resetting the variable involves processing, it is more efficient to retain a variable than not to.

When specifying a value to retain, a default start value may be specified. It is much more efficient to use this processing than to specify the value when _N_=1.

For example ;

Data Format(Drop=ProdCode);
    Retain FMTName ‘@KEEPAC’ Label 1 HLO ‘ ‘;
Infile Inventory EOF=Label;
Input @15 ProdCode $6. @;
If Prodcode=’A12345’;
Input @1  Start $10.;
Return;
Label:Start=’ ‘;
    Label=0;
    HLO=’O’;
    Output;

is more efficient than ;

Data Format(Drop=ProdCode);
    Retain FMTName Label HLO;
Infile Inventory EOF=Label;
Input @15 ProdCode $6. @;
If _N_=1 Then Do;
        FMTName=‘@KEEPAC’;
Label=1;
HLO=‘ ‘;
    End;
If Prodcode=’A12345’;
Input @1  Start $10.;
Return;
Label:Start=’ ‘;
    Label=0;
    HLO=’O’;
    Output;

because the test “IF _N_=1” does not need to be processed upon every loop of the data step.

and much more efficient than ;

Data Format(Drop=Prodcode);
    Infile Inventory EOF=Label;
Input @15 ProdCode $6. @;
    FMTName=‘@KEEPAC’;
Label=1;
HLO=‘ ‘;
    If Prodcode=’A12345’;
Input @1  Start $10.;
Return;
Label:Start=’ ‘;
    Label=0;
    HLO=’O’;
    Output;

because the three variables FMTName, Label, and HLO are reset upon every iteration of the data step, and the values are re-assigned upon every loop of the data step (even if the product code is not the required one !).

EOF=Label    : This statement forces SAS to process the statements following “LABEL:” in the program when it tries to read passed the last record in the file. In the code immediately above this would mean at the line “Input @15 Prodcode $6. @;” after the last record in the file had been read at the line “Input @1 Start $10.”  on the previous iteration of the datastep. The benefit is a slight saving in CPU by not having to check and End Of File flag on every iteration of the datastep. The preceding Return; statement is necessary to stop the statements following Label: being processed for every observation.

FMTName=’@KEEPAC’;  The ‘@’ specified in the variable name FMTName specifies to the Format Procedure that a character Input Format is being defined. Specifying a ‘$’ symbol would generate a character format (used on the PUT statement or function), whilst specifying nothing would generate a numeric format. The use of the character informat is described later.