/***************************************************************************************************************** SAS file name: datastep_missing.sas File location: __________________________________________________________________________________________________________________ Purpose: To investigate when the SAS Data retains values across Data Step iterations and when they are set to missing. Author: Peter Clemmensen Creation Date: This program supports the blog post "When Does the SAS Data Step Set Variable to Missing" on SASnrd.com *****************************************************************************************************************/ /* Examle data */ data have; input x @@; datalines; 1 2 3 4 ; /* As a general rule, the data step sets values to missing at the top of each iteration */ data want; set have; if _N_ = 2 then y = 1; run; /* Variables named in a RETAIN statement */ data want; set have; if _N_ = 2 then y = 1; retain y; run; /* Variables created in a SUM statement */ data want; set have; y + x; run; /* Data elements in a _TEMPORARY_ array */ data want; set have; array a{3} _temporary_ (10 20 30); t = catq('d', '|', of a[*]); put t=; run; /* Data elements that are initialized in an ARRAY statement */ data want; set have; array a{1} (1); run; /* Variables created with options on Statements, that read data (File, Infile, Set, Merge, Update, Modify) */ data want; set have nobs=nobs indsname=indsname curobs=curobs; _nobs = nobs; _indsname = indsname; _curobs = curobs; run; /* Automatic variables */ data want; set have; n = _N_; iorc = _iorc_; run;