SAS Expression and Functions.docx
- 文档编号:15492163
- 上传时间:2023-07-05
- 格式:DOCX
- 页数:28
- 大小:34.98KB
SAS Expression and Functions.docx
《SAS Expression and Functions.docx》由会员分享,可在线阅读,更多相关《SAS Expression and Functions.docx(28页珍藏版)》请在冰点文库上搜索。
SASExpressionandFunctions
Topic:
SASExpressionandFunctions
1.OverviewofOperatorsinSAS
2.InfixOperatorsinSAS
3.SASexpression&Conditionalexpression
(WHEREandIF)
4.Numericfunctions
5.SASTimeandDate
6.Characterfunctions
7.GlobalStatements
1.OverviewofOperatorsinSAS
Definitions:
ASASoperatorisasymbolthatrepresentsacomparison,arithmeticcalculation,orlogicaloperation;aSASfunction;orgroupingparentheses.SASusestwomajorkindsofoperators:
∙prefixoperators
∙infixoperators.
Aprefixoperatorisanoperatorthatisappliedtothevariable,constant,function,orparentheticexpressionthatimmediatelyfollowsit.Theplussign(+)andminussign(-)canbeusedasprefixoperators.Thefollowingareexamplesofprefixoperatorsusedwithvariables,constants,functions,andparentheticexpressions:
∙+y
∙-25
∙-cos(angle1)
∙+(x*y)
Aninfixoperatorappliestotheoperandsoneachsideofit,forexample,6<8.Infixoperatorsincludethefollowing:
∙arithmetic
∙comparison
∙logical,orBoolean
∙minimum
∙maximum
∙Concatenation.
Whenusedtoperformarithmeticoperations,theplusandminussignsareinfixoperators.
2.InfixOperator
2.1Arithmeticoperators:
ArithmeticOperators
Symbol
Definition
Example
*
multiplication
wherebonus=salary*.10;
/
Division
wheref=g/h;
+
Addition
wherec=a+b;
-
subtraction
wheref=g-h;
**
exponentiation
wherey=a**2;
2.2Comparisonoperators:
Thefollowingtableliststhecomparisonoperators:
ComparisonOperators
Symbol
MnemonicEquivalent
Definition
Example
=
EQ
equalto
whereempnumeq3374;
^=or~=or¬=
NE
notequalto
wherestatusnefulltime;
>
GT
greaterthan
wherehiredategt'01jun1982'd;
<
LT
lessthan
whereempnum<2000;
>=
GE
greaterthanorequalto
whereempnum>=3374;
<=
LE
lessthanorequalto
whereempnum<=3374;
IN
equaltoonefromalistofvalues
wherestatein('NC','TX');ornumin(‘3’,‘4’,‘5’)
2.3Others
2.3.1INOperator
TheINoperator,whichisacomparisonoperator,searchesforcharacterandnumericvaluesthatareequaltoonefromalistofvalues.Thelistofvaluesmustbeinparentheses,witheachcharactervalueinquotationmarksandseparatedbyeitheracommaorblank.
Forexample,supposeyouwantallsitesthatareinNorthCarolinaorTexas.Youcouldspecify:
wherestate='NC'orstate='TX';
However,theeasierwaywouldbetousetheINoperator,whichsaysyouwantanystateinthelist:
wherestatein('NC','TX');
Inaddition,youcanusetheNOTlogicaloperatortoexcludealist.Forexample,
wherestatenotin('CA','TN','MA');
Example:
datain_ex;
inputstore$vcr_pricevcd_pricecd_player_price;
datalines;
future_shop169.9969.9979.99
sony_store179.9964.9984.99
radio_shack159.9964.9969.99
three_d174.9967.4974.99
electron174.9965.9969.99
;
datain_ex_2;
setin_ex;
*where/ifvcr_pricenotin(159.99,169.99);
run;
2.3.2Fully-BoundedRangeCondition
Afully-boundedrangeconditionconsistsofavariablebetweentwocomparisonoperators,specifyingbothanupperandlowerlimit.Forexample,thefollowingexpressionreturnstheemployeenumbersthatfallwithintherangeof500to1000(inclusive):
where500<=empnum<=1000;
YoucancombinetheNOTlogicaloperatorwithafully-boundedrangeconditiontoselectobservationsthatfalloutsidetherange.Notethatparenthesesarerequired:
wherenot(500<=empnum<=1000);
2.3.3BETWEEN-ANDOperator
TheBETWEEN-ANDoperatorisalsoconsideredafully-boundedrangeconditionthatselectsobservationsinwhichthevalueofavariablefallswithinaninclusiverangeofvalues.
Youcanspecifythelimitsoftherangeasconstantsorexpressions.Anyrangeyouspecifyisaninclusiverange,sothatavalueequaltooneofthelimitsoftherangeiswithintherange.ThegeneralsyntaxforusingBETWEEN-ANDis:
WHEREvariableBETWEENvalueANDvalue;
Forexample:
whereempnumbetween500and1000;
wheretaxesbetweensalary*0.30andsalary*0.50;
2.4Booleanoperators:
AlsocalledLogicaloperators
&orandand|ororor^ornotnegation
TwocomparisonswithacommonvariablelinkedbyAND(operator)canbecondensedwithanimpliedAND.Forexample,thefollowingtwosubsettingIFstatementsproducethesameresult:
∙if16<=ageand/orage<=65;
∙if16<=age<=65;
2.5MINandMAXOperators
UsetheMINandMAXoperatorstofindtheminimumormaximumvalueoftwoquantities.
2.6ConcatenationOperator
Theconcatenationoperatorconcatenatescharactervalues.Youindicatetheconcatenationoperatorasfollows:
||
Forexample,inthisDATAstep,thevaluethatresultsfromtheconcatenationcontainsblanksbecausethelengthoftheCOLORvariableiseight:
datanamegame;
lengthcolorname$8game$12;
color='black';
name='jack';
*game=color||name;
game=trim(color)||name;
*putgame=;
run;
ThevalueofGAMEis'blackjack'.Tocorrectthisproblem,usetheTRIMfunctionintheconcatenationoperationasfollows:
game=trim(color)||name;Thisstatementproducesavalueof'blackjack'forthevariableGAME
data_null_;
month='sep';year=99;
date=trim(month)||left(put(year,8.));
/*PUTfunctiontoconvertanumericvaluetoacharactervalue*/
putdate=;run;
3.SASexpressionandConditionalexpression(WHEREandIF)
IFexpression:
TheIFcommandcanonlybeusedinaDATAstep.Thismeansyouarecreatinganewdatasetthatincludesonlytheselectedobservations.OnlyobservationsthatsatisfytheIFconditionaresenttotheoutputdataset.
WHEREexpression:
isatypeofSASexpressionthatdefinesaconditionforselectingobservations.TheWHEREcommandcanbeaddedtoanyprocedure.OnlyobservationsthatsatisfytheWHEREconditionareusedbytheprocedure.UsingtheWHEREstatementmayimprovetheefficiencyofyourSASprogramsbecauseSASisnotrequiredtoreadallobservationsfromtheinputdatasetanddoesnotchangethedatasetinanyway.
4.Numericfunctions
4.1Mathematical:
ThefollowingisabriefsummaryofSASfunctionsusefulfordefiningmodels.
ABS(x)theabsolutevalueofx
COS(x)thecosineofx.xisinradians.
EXP(x)ex
LOG(x)thenaturallogarithmofx
LOG10(x)thelogbasetenofx
LOG2(x)thelogbasetwoofx
SIN(x)thesineofx.xisinradians.
SQRT(x)thesquarerootofx
TAN(x)thetangentofx.xisinradiansandisnotanoddmultipleof2.
4.2Statistical
Kurtosis:
describesthe“heavinessofthetails”ofadistribution
Skewness:
measureofthetendencyforthedistributionvaluestobemorespreadoutononesidethatother
MaxandMin:
Mean:
averageisthetotalofsumdividedbythenumberofscores
Median:
thepointthatcorrespondstothevaluethatliesinthemiddleofthedistribution
orthevaluethatdividesadistributionexactlyinhalf(3,5,8,10,11).Itservesasavaluablealternativetothemeaninthespecificsituations:
a)thereareafewextremescoresinthedistribution,b)somevalueshaveundeterminedvalues,c)whenthedataaremeasuredonanordinalscale(seeexamplebelow)
Range:
maxtomin
Sum:
Variance:
measureofvariability
Std:
standarddeviation,thesquarerootofthevariance
Stderr:
standarderrorofthemean,stddev/thesquarerootofsamplesize
Example:
dataincome_1;
inputregion$1-8income;
cards;
Edison99800
Edison109800
Edison120000
Edison96500
Edison90550
Edison115000
Edison142500
Edison73000
Edison79850
Edison55890
Edison23000
Edison19800
Edison82000
Edison76800
Edison39800
Edison22800
Edison58650
;
run;
procunivariatedata=income_1plotnormal;
varincome;
run;
procchartdata=income_1;
vbarincome/levels=8;
run;
dataincome_2;
inputregion$1-8income;
cards;
Edison11800
Edison29800
Edison15000
Edison26500
Edison39550
Edison22000
Edison62500
Edison83000
Edison29850
Edison35890
Edison53000
Edison19800
Edison72000
Edison36800
Edison39800
Edison22800
Edison58650
;
run;
procunivariatedata=income_2plotnormal;
varincome;
run;
procchartdata=income_2;
vbarincome/levels=10;
run;
5.SASTimeandDate:
5.1SASDate,Time,andDatetimeValues
KeyConcepts:
SASdatevalue:
isavaluethatrepresentsthenumberofdaysbetweenJanuary1,1960,andaspecifieddate.SAScanperformcalculationsondatesrangingfromA.D.1582toA.D.19,900.DatesbeforeJanuary1,1960,arenegativenumbers;datesafterarepositivenumbers.SASdatevaluesaccountforallleapyeardays,includingtheleapyeardayintheyear2000.VariousSASlanguageelementshandleSASdatevalues:
functions,formatsandinformats.
SAStimevalue:
isavaluerepresentingthenumberofsecondssincemidnightofthecurrentday.
SASdatetimevalue:
isaspecialvaluethatcombinesbothdateandtimeinformation.ASASdatetimevalueisstoredasthenumberofsecondsbetweenmidnightonJanuary1,1960,andagivendateandtime.
Example:
optionsnodatepageno=1linesize=80pagesize=60;
datatest;
Time1=86399;/*datetimevalue*/;
formatTime1datetime.;
Date1=86399;/*datevalue*/;
formatDate1date.;
Time2=86399;/*timevalue*/;
formatTime2timeampm.;
run;
procprintdata=test;
title'SameNumber,DifferentSASValues';
run;
Output:
ObsTime1Date1Time2
101JAN60:
23:
59:
5920JUL9611:
59:
59PM
HowSASConvertsCalendarDatestoSASDateValues
5.2Two-DigitandFour-DigitYears
IfdatesinyourexternaldatasourcesorSASprogramstatementscontaintwo-digityears,youcandeterminewhichcenturyprefixshouldbeassignedtothembyusingtheYEARCUTOFF=systemoption.TheYEARCUTOFF=systemoptionspecifiesthefirstyearofthe100-yearspan
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SAS Expression and Functions