Guanhua Lu’s Web Notes

December 18, 2008

NOBS, POINT, RETAIN

Filed under: SAS — Tags: , , , — guanhualu @ 3:33 am

“NOBS=” creates and names a temporary variable whose value is usually the total number of observations in the input data set or data sets. If more than one data set is listed in the SET statement, NOBS= the total number of observations in the data sets that are listed.

data test2;
do sampleobs = 1 to totalobs by 2;
set test1 point=sampleobs nobs=totalobs;
tot = totalobs;
output;
end;
stop;
run;

At compilation time, SAS reads the descriptor portion of each data set and assigns the value of the NOBS= variable automatically. Thus, you can refer to the NOBS= variable before the SET statement. The variable is available in the DATA step but is not added to any output data set.

“POINT=” specifies a temporary variable whose numeric value determines which observation is read. POINT= causes the SET statement to use random (direct) access to read a SAS data set. “STOP” statement is needed here to prevent infinite loops.

The next example shows that “NOBS” can be obtained without execute the “SET” statement. The NOBS= value is filled when the DATA step begins, NOT when the SET statement executes!

data _null_;
if 0 then set test1 nobs=nobs;
call symput(‘totalobs’,nobs);
stop;
run;

The “dictionary” part of the dataset does not have to be read because all of that information, including the number of OBS is stored in the dictionary tables. SAS can access the dictionary tables much faster than opening a file.

If a value appears in a RETAIN statement, variables that appear before it in the list are set to that value initially.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Create a free website or blog at WordPress.com.