/*This example generates a data set with one variable and 100 records Each record contains a random number, generated from a uniform distribution, in the range [0:1]. The idea is to create a new data set with the mean value of all the records START ADaMSoft, copy the script in the command editor window and press F8*/ dataset out=test; newvar random_value=num; for (int i=0; i<100; i++) { random_value=RNDUNIFORM(); output; }; run; dataset dict=test out=mean; newvar mean sum records=num; retain sum records; if (ISMISSING(sum)) sum=0; if (ISMISSING(records)) records=0; sum=sum+random_value; records=records+1; if (LAST_DICT) { mean=sum/records; output; }; keep mean sum records; run; /*HOW IT WORKS: *** Here a new data set is created, it's name will be "test" dataset out=test; *** A numerical variable is created, it's name is "random_value" newvar random_value=num; *** The operations between the brackets are repeated 100 times for (int i=0; i<100; i++) { *** The ADaMSoft function RNDUNIFORM() is called and it's return value is copied into the variable "random_value" random_value=RNDUNIFORM(); *** here the record is written in the output data set output; }; run; *** Here the input data set is accessed and it will be created the result data set called "mean" dataset dict=test out=mean; *** Here are defined three new variables, called "mean", "sum" and "records" newvar mean sum records=num; *** Here it is specified that, for each record, the values of the variables "sum" and "records" should be retained retain sum records; *** Obvioulsy for the first record the values of "sum" and of "records" are null if (ISMISSING(sum)) sum=0; if (ISMISSING(records)) records=0; *** For each record the value of the "sum" is updated with the current value of the variable "random_value" sum=sum+random_value; *** For each record the counter of the number of record is updated by adding 1 records=records+1; *** The next statements will be executed after the last record was accessed if (LAST_DICT) { *** The mean is... the total sum divided by the number of records mean=sum/records; *** The idea is to write just this record output; }; *** Here it is specified that we want just these variables keep mean sum records; run;*/