/*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; scl dict=test; exebefore; double records=0; double sum=0; endexebefore; sum=sum+random_value; records=records+1; if (LAST_DICT) SHAREDOBJECTS.put("mean",new Double(sum/records)); run; dataset out=mean; newvar mean=num; mean=((Double)SHAREDOBJECTS.get("mean")).doubleValue(); output; 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 just accessed (without creating a new data set) scl dict=test; *** These statements are executed before the data set is accessed and are valid till the end of the SCL step exebefore; *** The double variable "records" is created and it is initialized to 0 double records=0; *** The double variable "sum" is created and it is initialized to 0 double sum=0; *** Here end the statements that are executed before the data set is accessed endexebefore; *** In the variable "sum" is the current value of the sum of the values of the variable "random_value sum=sum+random_value; *** In the variable "records" is the current cumulative number of records records=records+1; *** When the last record is accessed, it is inserted the object named "mean" in a special contained called "SHAREDOBJECTS". *** The value of the object "mean" is the ratio between the sum and the number of records if (LAST_DICT) SHAREDOBJECTS.put("mean",new Double(sum/records)); run; *** A new data set is created, called mean dataset out=mean; *** In this new data set is defined a variable called "mean" newvar mean=num; *** The value of the variable "mean" is taken from the content of the SHAREDOBJECTS named "mean". *** Note that it is necessary to specify that the type of the object is "Double" and that it should *** be transformed in a "double" (a number) mean=((Double)SHAREDOBJECTS.get("mean")).doubleValue(); *** Here the record is written output; run; */