NC STATE University
SAS Consulting, Department of Statistics 
SAS version 8.2

Problem: Automatic retain on variables read with set statement cause unexpected results.


Create Input Data Sets

data ph;
input time tdaysq method $;
cards;
 1         100      ph
 1         225      ph
 1         400      ph
 2         100      ph
 2         225      ph
 2         400      ph
 3         100      ph
 3         225      ph
 3         400      ph
run;


data observed;
input time tday method $;
cards;
1        10     observed
1        15     observed
1        20     observed
2        10     observed
2        15     observed
2        20     observed
3        10     observed
3        15     observed
3        20     observed
run;

Problem: incorrect tday values


data all; set ph observed;
if tday=. then do; miss='Yes';  tday=sqrt(tdaysq); end;
else miss='No ';
proc print data=all; 
title1 'Problem - tday values are incorrect when method=ph';
title2 'Why? tday was read with a set statment and it was automatically retained';
title3 '(not set to missing at the beginning of each iteration of the data step)';
run;

 
               Problem - tday values are incorrect when method=ph
    Why? tday was read with a set statment and it was automatically retained
    (not set to missing at the beginning of each iteration of the data step)

               Obs    time    tdaysq    method      tday    miss

                 1      1       100     ph           10     Yes
                 2      1       225     ph           10     No
                 3      1       400     ph           10     No
                 4      2       100     ph           10     No
                 5      2       225     ph           10     No
                 6      2       400     ph           10     No
                 7      3       100     ph           10     No
                 8      3       225     ph           10     No
                 9      3       400     ph           10     No
                10      1         .     observed     10     No
                11      1         .     observed     15     No
                12      1         .     observed     20     No
                13      2         .     observed     10     No
                14      2         .     observed     15     No
                15      2         .     observed     20     No
                16      3         .     observed     10     No
                17      3         .     observed     15     No
                18      3         .     observed     20     No

Solution 1: Do calculation before concatenating data sets.

 
data ph2; set ph;
tday=sqrt(tdaysq); 
run;

data all1; set ph2 observed;
proc print data=all1; 
title1 'Solution 1: Do calculation before putting data together.';
run;

 Solution 1: Do calculation before putting the data together.

                   Obs    time    tdaysq    method      tday

                     1      1       100     ph           10
                     2      1       225     ph           15
                     3      1       400     ph           20
                     4      2       100     ph           10
                     5      2       225     ph           15
                     6      2       400     ph           20
                     7      3       100     ph           10
                     8      3       225     ph           15
                     9      3       400     ph           20
                    10      1         .     observed     10
                    11      1         .     observed     15
                    12      1         .     observed     20
                    13      2         .     observed     10
                    14      2         .     observed     15
                    15      2         .     observed     20
                    16      3         .     observed     10
                    17      3         .     observed     15
                    18      3         .     observed     20


Solution 2: Add the variable tday to the input data set.


data ph3; set ph;
tday=.; 
run;

data all2; set ph3 observed;
Miss='No ';
if tday=. then do; miss='Yes';  tday=sqrt(tdaysq);  end;
proc print data=all2; 
title1 'Solution 2: add tday=. to the input data set';
run;


                  Solution 2: add tday=. to the input data set

               Obs    time    tdaysq    method      tday    Miss

                 1      1       100     ph           10     Yes
                 2      1       225     ph           15     Yes
                 3      1       400     ph           20     Yes
                 4      2       100     ph           10     Yes
                 5      2       225     ph           15     Yes
                 6      2       400     ph           20     Yes
                 7      3       100     ph           10     Yes
                 8      3       225     ph           15     Yes
                 9      3       400     ph           20     Yes
                10      1         .     observed     10     No
                11      1         .     observed     15     No
                12      1         .     observed     20     No
                13      2         .     observed     10     No
                14      2         .     observed     15     No
                15      2         .     observed     20     No
                16      3         .     observed     10     No
                17      3         .     observed     15     No
                18      3         .     observed     20     No


Solution 3: Create a new variable that isn't automatically retained.


data all3; set ph observed;
if tday ne . then tday2=tday;
else tday2=sqrt(tdaysq);
proc print data=all3; 
title1 'Solution 3: create anew variable that is not retained';
run;

  Solution 3: create a new variable that is not retained.

               Obs    time    tdaysq    method      tday    tday2

                 1      1       100     ph            .       10
                 2      1       225     ph            .       15
                 3      1       400     ph            .       20
                 4      2       100     ph            .       10
                 5      2       225     ph            .       15
                 6      2       400     ph            .       20
                 7      3       100     ph            .       10
                 8      3       225     ph            .       15
                 9      3       400     ph            .       20
                10      1         .     observed     10       10
                11      1         .     observed     15       15
                12      1         .     observed     20       20
                13      2         .     observed     10       10
                14      2         .     observed     15       15
                15      2         .     observed     20       20
                16      3         .     observed     10       10
                17      3         .     observed     15       15
                18      3         .     observed     20       20

[an error occurred while processing this directive]
Maintained by:Sandy Donaghy and Joy Smith
Last Modified: Tuesday, 22-Oct-2002 08:11:56 EDT
Filename: /working_groups/sas/samples/base/retain.html