ࡱ> z|yq` 3bjbjqPqP 4~::V+J`,`,`,8,,,07r--"------6666666$8h ;6!/--!/!/6--6_3_3_3!/--6_3!/6_3_3_3-- `3)`,!/p_33$7007_3;2v;_3;_3,->,.,_3X.$|.---663X---07!/!/!/!/'`,`, A Brief Introduction to SAS Operators and Functions (commands=functions.sas) This chapter introduces SAS arithmetic, comparison, and Boolean operators, and SAS mathematical and statistical functions and provides some basic rules for using them. SAS operators and functions are used as part of SAS programming statements, including ifthen statements and assignment statements used in the data step to create new variables and carry out other tasks, and where statements used in proc steps to select cases. Examples using operators and functions appear throughout this workbook. You can get more information on SAS operators and functions in the SAS online documentation and in the Language Reference. SAS Arithmetic Operators: The symbols for SAS arithmetic operators are given below, along their definitions and examples of how they could be used in an expression to set up a new variable. Symbol Definition Example ** Exponentiation y = x**2; z = x**y; * Multiplication r = x*y; / Division r = x/y; + Addition s = x+y; - Subtraction t = x-y; Note that an asterisk (*) must always be used to indicate multiplication e.g. y=2*x, not y=2x, or 2(x). If one of the arguments for an arithmetic operator is missing, the result is missing. SAS Comparison Operators: SAS comparison operators can be written as symbols or as their mnemonic equivalents, as shown below: SymbolMnemonicDefinition<LtLess than<=LeLess than or equal to>GtGreater than>=GeGreater than or equal to=EqEqual to~=NeNot equal to. If the symbol for a comparison operator is used, it is not necessary to have blank spaces around it, but if the mnemonic is used, it must be set off by spaces, as illustrated in the examples below: if x= > ~< ~> & (AND) | (OR) SAS Functions: A SAS function performs a computation or system manipulation on argument(s) and returns a value. For example, the log function returns the natural log of an argument. The argument(s) to a function are contained in parentheses immediately following the function name; argument(s) may be either variable names, constants such as numbers, or SAS expressions (e.g. other SAS functions or mathematical expressions). If a function requires more than one argument, they are separated by commas. There are many types of SAS functions, including arithmetic, array, truncation, mathematical, trigonometric, probability, quantile, sample statistics, random number, financial, character, date and time, state and ZIP code functions. For a complete list of SAS functions by category, see the SAS Language Reference. SAS Mathematical Functions: A short list of some of some commonly used mathematical functions is given below: Selected Mathematical Functions Function NameDefinitionAssignment Statement ExampleAbsAbsolute valueY1 = abs(x);IntInteger (takes the integer part of the argument)Y2 = int(x);LogNatural logY3=log(x);Log10Log base 10Y4=log10(x);RoundRounds the argument to the nearest specified level (e.g., hundredths)y5=round(x,.01); SqrtSquare rootY6=sqrt(x); If the value of the argument is invalid (such as using the sqrt function with a negative value as the argument), SAS will return a missing result, and display an error message in the SAS Log. However this error will not prevent the program from executing. Example using SAS mathematical functions to transform variables and create new variables: The example below shows how to use SAS mathematical functions and arithmetic operators to transform variables and create new variables in a data step. Each new variable is created using an assignment statement, in which the new variable is named on the left hand side of the equals sign, and the function or expression is on the right hand side. These new variables must be created between the data statement and the run statement of a data step to be valid. data math; input x y; /*mathematical functions*/ absx = abs(x); sqrtx = sqrt(x); log10y = log10(y); lny = log(y); int_y = int(y); roundy = round(y,.1); /*arithmetic operators*/ mult = x*y; divide = x/y; expon = x**y; tot = x + y; diff = x - y; cards; 4 5.23 -15 22.0 . 18.51 -1 3 6 0 5 5.035 ; proc print data=math; run; The output from these commands is shown below. Notice the missing values as the result of applying arithmetic or mathematical operators to a missing or illegal argument. OBS X Y ABSX SQRTX LOG10Y LNY INT_Y ROUNDY 1 4 5.230 4 2.00000 0.71850 1.65441 5 5.2 2 -15 22.000 15 . 1.34242 3.09104 22 22.0 3 . 18.510 . . 1.26741 2.91831 18 18.5 4 -1 3.000 1 . 0.47712 1.09861 3 3.0 5 6 0.000 6 2.44949 . . 0 0.0 6 5 5.035 5 2.23607 0.70200 1.61641 5 5.0 OBS MULT DIVIDE EXPON TOT DIFF 1 20.920 0.76482 1408.55 9.230 -1.230 2 -330.000 -0.68182 7.48183E25 7.000 -37.000 3 . . . . . 4 -3.000 -0.33333 -1.00 2.000 -4.000 5 0.000 . 1.00 6.000 6.000 6 25.175 0.99305 3306.08 10.035 -0.035 SAS Statistical Functions: Statistical functions can be used to generate such values as the mean, sum, and standard deviation of values within a case. Statistical functions operate on at least 2 arguments. The arguments can be listed separated by commas, or lists of variables can be used if the keyword of precedes the list. The result of a statistical function is based on the non-missing values of the arguments. Selected Statistical Functions Function NameDefinitionAssignment Statement ExampleMeanMean of non-missing valuesy1 = mean (x1,x2,x3);MinMinimum of non-missing valuesy2 = min (of x1-x3);MaxMaximum of non-missing valuesy3 = max (cash, credit);NThe number of non-missing valuesy4 = n (of age--weight);NmissThe number of missing valuesy5 = nmiss (of wt1-wt3);StdStandard deviation of non-missing valuesy6 = std (5,6,7,9);StderrStandard error of the mean of non-missing valuesy7 = stderr(of x1-x20);SumSum of non-missing valuesy8 = sum(of x1-x20); The example below shows the use of selected statistical functions on a hypothetical data set with three variables containing the salary for each person in the years 2001, 2002, and 2003 (SAL01, SAL02, and SAL03, respectively). The format statement is used to display the values of the salary variables and some of the result variables in dollar form, with 9 places (dollar9.). You can change the way these values are displayed by modifying the format used. For example, you could display these variables with dollars and cents by using the dollar12.2 format. data salary; input SAL01 SAL02 SAL03; AvgSalary = mean(SAL01,SAL02,SAL03); StdSalary = std(SAL01,SAL02,SAL03); MaxSalary = max(SAL01,SAL02,SAL03); YrsSalary = n(SAL01,SAL02,SAL03); TotSalary = sum(SAL01,SAL02,SAL03); format SAL01 SAL02 SAL03 AvgSalary MaxSalary TotSalary dollar9.; cards; 50000 55000 60000 . 65000 70000 . . 52000 50000 . . ; title "Salary Example"; proc print data=salary; run; The output from these commands is shown below: Salary Example Std Yrs Obs SAL01 SAL02 SAL03 AvgSalary Salary MaxSalary Salary TotSalary 1 $50,000 $55,000 $60,000 $55,000 5000.00 $60,000 3 $165,000 2 . $65,000 $70,000 $67,500 3535.53 $70,000 2 $135,000 3 . . $52,000 $52,000 . $52,000 1 $52,000 4 $50,000 . . $50,000 . $50,000 1 $50,000 To restrict the calculation of the mean salary to cases that have at least two valid values in the arguments, you could use the n function in combination with the mean function, as shown below for the variable AVGSAL2: data salary2; input SAL01 SAL02 SAL03; AvgSalary = mean(SAL01,SAL02,SAL03); StdSalary = std(SAL01,SAL02,SAL03); MaxSalary = max(SAL01,SAL02,SAL03); YrsSalary = n(SAL01,SAL02,SAL03); TotSalary = sum(SAL01,SAL02,SAL03); if n(SAL01,SAL02,SAL03)>=2 then AvgSalary2 = mean(SAL01,SAL02,SAL03); format SAL01 SAL02 SAL03 AvgSalary AvgSalary2 MaxSalary TotSalary dollar9.; cards; 50000 55000 60000 . 65000 70000 . . 52000 50000 . . ; title "Salary Example, with Two Ways of Calculating Mean"; proc print data=salary2; var SAL01 SAL02 SAL03 AvgSalary AvgSalary2; run; Salary Example, with Two Ways of Calculating Mean Avg Obs SAL01 SAL02 SAL03 AvgSalary Salary2 1 $50,000 $55,000 $60,000 $55,000 $55,000 2 . $65,000 $70,000 $67,500 $67,500 3 . . $52,000 $52,000 . 4 $50,000 . . $50,000 .     PAGE  PAGE 6 Intro to SAS operators and functions. 4LMNjtv   L S c m   % 8 @    '(0پپپٶٶٶٶٶٶپٮٮٮٮٮٮh/eCJaJh/eOJQJ h/e>*h/eCJ aJ h/e5CJ aJ h/e5h/e h/eCJ$hh/e5CJ$aJ$h/e5CJ$aJ$ h/e5CJ0D4MN % A B     $$Ifa$gd gd/e$a$gd/eV33 {ooo $$Ifa$gd kd$$IflF :=8 0    44 la  {ooo $$Ifa$gd kd$$IflF :=8 0    44 la  {ooo $$Ifa$gd kdd$$IflF :=8 0    44 la  {ooo $$Ifa$gd kd$$IflF :=8 0    44 la  {ooo $$Ifa$gd kd$$IflF :=8 0    44 la '{ooo $$Ifa$gd kdz$$IflF :=8 0    44 la '()  !:;z{{vvvvvvvvvvvvgd/ekd,$$IflF :=8 0    44 la  06!9{23 +VRU!"v#01ALM_lmݱh/eCJOJQJaJh/eCJaJh/eCJ aJ h/e0J h/e0J5h/e5CJaJh/e5OJQJ h/e>*h/e5CJ aJ h/eOJQJh/eOJQJh/e h/e5>23,-UV h^h`gd/egd/e"#uv $$Ifa$gd $a$gd/egd/e h^h`gd/eymdd $Ifgd  $$Ifa$gd kd$$If    F%&  x   0        44 a#0ymdd $Ifgd  $$Ifa$gd kd$$If    F%& x 0        44 a015ALymdd $Ifgd  $$Ifa$gd kd$$If    F%& x 0        44 aLMS_lymdd $Ifgd  $$Ifa$gd kd$$If    F%& x 0        44 almsymddd $Ifgd  $$Ifa$gd kd( $$If    F%& x 0        44 aymdd $Ifgd  $$Ifa$gd kd< $$If    F%& x 0        44 aJK#0ypppkkkkkkkkgd/e 7$8$H$gd/ekdP $$If    F%&  x   0        44 a '+IJKr789>w#{#|##䳩乒tht_h/e5CJ aJ h/eCJOJQJaJhr[h/eCJOJQJaJhr[h/eCJOJQJaJh/eCJOJQJhr[h/eCJOJQJh/eCJOJQJ h/eCJh/eOJQJ h/e5CJ$h/e5CJaJh/e\fHq h/e5h/e.h/eCJOJQJ\^JaJfHq 01L]p&/:DOW_kq89gd/e9 8!!!!!"`"""2#x#y#z#{#|###%%>%L% $$Ifa$gd $a$gd/e 7$8$H$gd/egd/e###$$$$$%%>%t%u%%%%% &!&]&^&&&&&-'.'a'b'J(P((())))z*D+t+--f.徥w"h/eCJ\aJfHq h/eCJOJQJ^JaJh/e\fHq 1h/e5CJOJQJ\^JaJfHq .h/eCJOJQJ\^JaJfHq h/eCJaJ h/e5CJh/e h/e5"h/eCJ \aJ fHq *L%W%t%u%z%%%offf $Ifgd kdd $$Ifl    FL#     0        44 la $$Ifa$gd %%%%%{rrr $Ifgd kdk $$Ifl    FL#   0        44 la%%%& &{rrr $Ifgd kdl$$Ifl    FL#   0        44 la &!&#&D&]&{rrr $Ifgd kdm$$Ifl    FL#   0        44 la]&^&d&&&{rrr $Ifgd kdn$$Ifl    FL#   0        44 la&&&&&{rrr $Ifgd kdo$$Ifl    FL#   0        44 la&&&'-'{rrr $Ifgd kdp$$Ifl    FL#   0        44 la-'.'2'L'a'{rrr $Ifgd kdq$$Ifl    FL#   0        44 laa'b'c')))))) *0*T*z**{vvvvvvvvvvvvgd/ekdr$$Ifl    FL#      0        44 la ****** + ++&+>+C+D+s+t++ ,i,,'----...../ 7$8$H$gd/egd/ef.g.......//1T3U3V3W3Y3Z3\3]3_3`3b3c3i3j3k3m3n3t3u3v3w3x33333гЯyh/eh2CJaJh20JmHnHuh2 h20Jjh20JUh jh Uhtlh/eCJOJQJ^JaJh/e5CJOJQJ^JaJh/eCJOJQJ^JaJh/eh/e\fHq h/e5\fHq $/9/_//////B0I0[0m0000000111l11 2 2_223U3 7$8$H$gd/egd/eU3V3X3Y3[3\3^3_3a3b3k3l3m3x33333 &`#$gd gd/e21:pJB/ =!"#$% $$If !vh55=58 #v#v=#v8 :V l055=58 / a $$If !vh55=58 #v#v=#v8 :V l055=58 / a $$If !vh55=58 #v#v=#v8 :V l055=58 / a $$If !vh55=58 #v#v=#v8 :V l055=58 / a $$If !vh55=58 #v#v=#v8 :V l055=58 / a $$If !vh55=58 #v#v=#v8 :V l055=58 / a $$If !vh55=58 #v#v=#v8 :V l055=58 / a  $$If!vh5&5x5#v&#vx#v:V 0    5&5x5/  / / / /  /  / 4 $$If!vh5&5x5#v&#vx#v:V 0    ,5&5x5/  / / / / / /  4 $$If!vh5&5x5#v&#vx#v:V 0    ,5&5x5/  / / / / / /  4 $$If!vh5&5x5#v&#vx#v:V 0    ,5&5x5/  / / / / / /  4 $$If!vh5&5x5#v&#vx#v:V 0    ,5&5x5/  / / / / / /  4 $$If!vh5&5x5#v&#vx#v:V 0    ,5&5x5/  / / / / / /  4 $$If!vh5&5x5#v&#vx#v:V 0    ,5&5x5/  /  / / / /  /  4 $$If!vh555 #v#v#v :V l0    ,555 /  / / / /  /  / $$If!vh555 #v#v#v :V l0    555 /  / / / / / /  $$If!vh555 #v#v#v :V l0    555 /  / / / / / /  $$If!vh555 #v#v#v :V l0    555 /  / / / / / /  $$If!vh555 #v#v#v :V l0    555 /  / / / / / /  $$If!vh555 #v#v#v :V l0    555 /  / / / / / /  $$If!vh555 #v#v#v :V l0    555 /  / / / / / /  $$If!vh555 #v#v#v :V l0    555 /  / / / / / /  $$If!vh555 #v#v#v :V l0    555 /  /  / / / /  /  @`@ /eNormalCJ_HaJmH sH tH DA@D Default Paragraph FontVi@V  Table Normal :V 44 la (k@(No List BoB /estrong15OJQJ\^Jo(ph4 @4 /eFooter  !.)@. /e Page Number4@"4 /eHeader  !+ ~4MN %AB '()  !:;z{23    , - U V "#uv#015ALMS_lmsJK#01L]p&/:DOW_kq8998`2xyz{|>LWtuz !#D]^d-.2Labc!!!!!! "0"T"z""""""" # ##&#>#C#D#s#t## $i$$'%%%%&&&&&'9'_''''''B(I([(m((((((()))l)) * *_**+U+V+X+Y+[+\+^+_+a+b+k+m+x++++000000000000000000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00000000000000000000000000000000000000000000000000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 00000000000000000000000000000000000000000000000000000000000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 000000000000000000000000000000000000000000000000000000000000000000000@0I00@0I00I00gI00I00gI00@0I00g@0@0I0 0 gI00  IIIL0#f.3$.1< '0Ll0L%%% &]&&&-'a'*/U33 !"#%&'()*+,-/023456789:;=>3  L!! zC \!zC"zCe#zC}((D)D)+((L)L)+:*urn:schemas-microsoft-com:office:smarttagsStreet;*urn:schemas-microsoft-com:office:smarttagsaddress x5>!#{}  V X %*NR_d  (-29RVZ^bjqu !!!!!!!!"">"@"b"f"|"""""###&#*#>#A###G%b%%%%%&&&&&&!'%'G'K'm'o'''''''B(G((((((((())*++-+@+T+V+V+X+X+Y+Y+[+\+^+_+a+b+++333333333333333333333333333333333333333333333333333333333333333333U+V+V+X+X+Y+Y+[+\+^+_+a+b+j+m+w+x+++V+V+X+X+Y+Y+[+\+^+_+a+b+++JB/e 2tl '(v#015ALMS_lms>LWtuz !#D]^d-.2LabU++ 3 3 3 3@U+U+YU+U++@UnknownGz Times New Roman5Symbol3& z Arial?5 z Courier New71 CourierC5  SAS Monospace"1h w$Nw$Nd4@+@+2QHX ?/e23A Brief Introduction to SAS Operators and FunctionskwelchkwelchOh+'0$ 4@ ` l x 4A Brief Introduction to SAS Operators and Functionskwelch Normal.dotkwelch2Microsoft Office Word@@ @B @B w$՜.+,00 hp  University of MichiganN@+ 4A Brief Introduction to SAS Operators and Functions Title  !"#$%&'()*+,-./0123456789:;<=>?ABCDEFGHIJLMNOPQRSTUVWXYZ[\]^_`abcdefghjklmnoprstuvwx{Root Entry F)}Data @s1TableK;WordDocument4~SummaryInformation(iDocumentSummaryInformation8qCompObjq  FMicrosoft Office Word Document MSWordDocWord.Document.89q