options nodate nocenter nonumber ls=80; title ; * A SAS function returns a value from a computation or system ; * manipulation that requires zero or more arguments. ; * Most functions use arguments supplied by the user, however a few ; * obtain their arguments from the operating system. Here is the ; * syntax of a function: ; * function-name(argument1, argument2) ; * We will illustrate some functions using the following dataset ; * that includes name, test1 test2 and test3 ; DATA funct; INPUT name $14. x test1 test2 test3; * numeric functions ; t1int = INT(test1); t2int = INT(test2); /* integer part of a number */ t1rnd = ROUND(test1);t2rnd = ROUND(test2,.1); /* round to nearest whole number */ tave = MEAN(test1, test2, test3); /* mean across variables */ xsqrt = SQRT(x); /* square root */ xlog = LOG(x); /* log base 10 */ xexp = EXP(x); /* e raised to the power */ * string functions ; c1 = UPCASE(name); /* convert to upper case */ c2 = SUBSTR(name,3,8); /* substring */ len = LENGTH(name); /* length of string */ ind = INDEX(name,' '); /* position in string */ fname = SUBSTR(name,1,INDEX(name,' ')); lname = SUBSTR(name,INDEX(name,' '),LENGTH(name)-INDEX(name,' ')+1); DATALINES; John Smith 4.2 86.5 84.55 81 Samuel Adams 9.0 70.3 82.37 . Ben Johnson -6.2 82.1 84.81 87 Chris Adraktas 9.5 94.2 92.64 93 John Brown . 79.7 79.07 72 ; PROC PRINT DATA=funct; VAR test1 test2 test3 t1int t2int t1rnd t2rnd tave; RUN; PROC PRINT DATA=funct; VAR x xsqrt xlog xexp; RUN; PROC PRINT DATA=funct; VAR name c1 c2 len ind fname lname; RUN; * Random numbers are more useful than you might imagine, ; * they are used extensively in Monte Carlo studies, ; * but they are also frequently used in many other ; * situations; * ; * We will look at two of SAS's random number functions ; * UNIFORM(SEED) - generates values from a random ; * uniform distribution between 0 and 1 ; * NORMAL(SEED) - generates values from a random ; * normal distribution with mean 0 and ; * standard deviation 1 ; * The statements IF x>.5 THEN coin = 'heads' and ; * ELSE coin = 'tails' creates a random variable ; * called coins that has values 'heads' and 'tails' ; DATA fun1; INPUT id; x1 = UNIFORM(-1); /* interval 0-1 */ y1 = 50 + 3*NORMAL(-1); /* mean ->50 sd ->3 */ IF x1>.5 THEN coin1 = 'heads'; ELSE coin1 = 'tails'; DATALINES; 1 2 3 4 5 ; DATA fun2; set fun1; x2 = UNIFORM(-1); /* interval 0-1 */ y2 = 50 + 3*NORMAL(-1); /* mean ->50 sd ->3 */ IF x2>.5 THEN coin2 = 'heads'; ELSE coin2 = 'tails'; data fun3; set fun2; x3 = UNIFORM(123456); /* interval 0-1 */ y3 = 50 + 3*NORMAL(123456); /* mean ->50 sd ->3 */ IF x3>.5 THEN coin3 = 'heads'; ELSE coin3 = 'tails'; data fun4; set fun3; x4 = UNIFORM(123456); /* interval 0-1 */ y4 = 50 + 3*NORMAL(123456); /* mean ->50 sd ->3 */ IF x4>.5 THEN coin4 = 'heads'; ELSE coin4 = 'tails'; * Variables x1 and x2 use a SEED value of -1 ; * Negative SEED values will result in different random ; * numbers being generated each time. ; * Sometimes we will want to generate the same random ; * numbers each time so that we can debug our programs. ; * To do this we just enter the same postive number ; * as the SEED value. ; * Variables x3 and x4 illustrate how to generate the ; * same results; PROC PRINT DATA=fun4; VAR x1 coin1 x2 coin2 x3 coin3 x4 coin4; RUN; * This time y1 and y2 use -1 as the seed, while y3 and y4 ; * use the same positive number as the seed. ; PROC PRINT DATA=fun4; VAR y1 y2 y3 y4; PROC MEANS DATA=fun4; VAR y1 y2 y3 y4; RUN;