Character FunctionsWord格式.docx
- 文档编号:5251454
- 上传时间:2023-05-04
- 格式:DOCX
- 页数:19
- 大小:21.55KB
Character FunctionsWord格式.docx
《Character FunctionsWord格式.docx》由会员分享,可在线阅读,更多相关《Character FunctionsWord格式.docx(19页珍藏版)》请在冰点文库上搜索。
123450101
:
#$%&
1111
ABC4414
FINDandFINDC
ThispairoffunctionssharessomesimilaritiestotheINDEXandINDEXCfunctions.FINDandINDEXbothsearchastringforagivensubstring.FINDCandINDEXCbothsearchforindividualcharacters.However,bothFINDandFINDChavesomeadditionalcapabilityovertheircounterparts.Forexample,thispairoffunctionshastheabilitytodeclareastartingpositionforthesearch,thedirectionofthesearch,andtoignorecaseortrailingblanks.
Function:
FIND
Purpose:
Tolocateasubstringwithinastring.Withoptionalarguments,youcandefinethestartingpointforthesearch,thedirectionofthesearch,andignorecaseortrailingblanks.
Syntax:
FIND(character-value,find-string<
'
modifiers'
>
<
start>
)
30SASFunctionsbyExample
character-valueisanySAScharacterexpression.
find-stringisacharactervariableorstringliteralthatcontainsoneormorecharactersthatyouwanttosearchfor.Thefunctionreturnsthefirstpositioninthecharacter-valuethatcontainsthefind-string.Ifthefind-stringisnotfound,thefunctionreturnsa0.
Thefollowingmodifiers(inupper-orlowercase),placedinsingleordoublequotationmarks,maybeusedwithFIND:
iignorecase.
tignoretrailingblanksinboththecharactervariableandthe
findstring.startisanoptionalparameterthatspecifiesthepositioninthestringtobeginthesearch.Ifitisomitted,thesearchstartsatthebeginningofthestring.Ifitisnon-zero,thesearchbeginsatthepositioninthestringoftheabsolutevalueofthenumber.Ifthevalueispositive,thesearchgoesfromlefttoright;
ifthevalueisnegative,thesearchgoesfromrighttoleft.Anegativevaluelargerthanthelengthofthestringresultsinascanfromrighttoleft,startingattheendofthestring.Ifthevalueofstartisapositivenumberlongerthanthelengthofthestring,orifitis0,thefunctionreturnsa0.
Examples
FortheseexamplesSTRING1="
Hellohellogoodbye"
andSTRING2="
hello"
FunctionReturns
FIND(STRING1,STRING2)7
FIND(STRING1,STRING2,'
I'
)1
FIND(STRING1,"
bye"
)17
FIND("
abcxyzabc"
"
abc"
4)7
FIND(STRING1,STRING2,"
i"
-99)7
CharacterFunctions31
FINDC
Tolocateacharacterthatappearsordoesnotappearwithinastring.Withoptionalarguments,youcandefinethestartingpointforthesearch,thedirectionofthesearch,toignorecaseortrailingblanks,ortolookforcharactersexcepttheoneslisted.
FINDC(character-value,find-characters
<
find-charactersisalistofoneormorecharactersthatyouwantto
searchfor.
Thefunctionreturnsthefirstpositioninthecharacter-valuethat
containsoneofthefind-characters.Ifnoneofthecharactersare
found,thefunctionreturnsa0.Withanoptionalargument,youcanhave
thefunctionreturnthepositioninacharacterstringofacharacterthatisnotinthefind-characterslist.
modifiers(inupper-orlowercase),placedinsingleordoublequotationmarks,maybeusedwithFINDCasfollows:
find-characters.vcountonlycharactersthatarenotinthelistoffindcharacters.Oprocessthemodifiersandfindcharactersonlyoncetoaspecificcalltothefunction.Insubsequentcalls,changestotheseargumentswillhavenoeffect.
startisanoptionalparameterthatspecifiesthepositioninthestringto
beginthesearch.Ifitisomitted,thesearchstartsatthebeginningofthestring.Ifitisnon-zero,thesearchbeginsatthepositioninthestringoftheabsolutevalueofthenumber.Ifthevalueispositive,thesearchgoesfrom
32SASFunctionsbyExample
lefttoright;
ifthevalueisnegative,thesearchgoesfromrighttoleft.A
negativevaluelargerthanthelengthofthestringresultsinascanfromrighttoleft,startingattheendofthestring.Ifthevalueofstartisapositivenumberlongerthanthelengthofthestring,orifitis0,thefunctionreturnsa0.
Note:
Youcanswitchthepositionsofstartandmodifiersandthefunctionwillworkthesame.
ApplesandBooks"
andSTRING2="
abcde"
FINDC(STRING1,STRING2)5
FINDC(STRING1,STRING2,'
i'
FINDC(STRING1,"
aple"
vi'
)6
FINDC("
Program1.14:
UsingtheFINDandFINDCfunctionstosearchforstrings
andcharacters
***Primaryfunctions:
FINDandFINDC;
DATAFIND_VOWEL;
INPUT@1STRING$20.;
PEAR=FIND(STRING,"
Pear"
);
POS_VOWEL=FINDC(STRING,"
aeiou"
UPPER_VOWEL=FINDC(STRING,"
NOT_VOWEL=FINDC(STRING,"
AEIOU"
IV'
DATALINES;
XYZABCabc
XYZ
AppleandPear
;
PROCPRINTDATA=FIND_VOWELNOOBS;
TITLE"
ListingofDataSetFIND_VOWEL"
RUN;
CharacterFunctions33
TheFINDfunctionreturnsthepositionofthecharacters"
inthevariableSTRING.Sincetheimodifierisnotused,thesearchiscase-sensitive.ThefirstuseoftheFINDCfunctionlooksforanyupper-orlowercasevowelinthestring(becauseoftheimodifier).Thenextstatement,withouttheimodifier,locatesonlylowercasevowels.Finally,thevmodifierinthelastFINDCfunctionreversesthesearchtolookforthefirstcharacterthatisnotavowel(upper-orlowercasebecauseoftheimodifier).
Program1.15:
DemonstratingtheomodifierwithFINDC
***Primaryfunction:
FINDC;
DATAO_MODIFIER;
INPUTSTRING$15.
@16LOOK_FOR$1.;
POSITION=FINDC(STRING,LOOK_FOR,'
IO'
CapitalAhereA
LowerahereX
AppleB
PROCPRINTDATA=O_MODIFIERNOOBSHEADING=H;
ListingofDataSetO_MODIFIER"
InthefirstcalltoFINDC,thevalueofLOOK_FORisanuppercaseA.Sincetheomodifierwasused,changingthevalueofLOOK_FORinthenexttwoobservationshasnoeffect—thefunctioncontinuestolookfortheletterA.NotethatanotheruseofFINDCinthisDATAstepwouldnotbeaffectedbytheprevioususeoftheomodifier,evenifthenameof
thevariable(inthiscasePOSITION)werethesame.Theomodifierismostlikelyusefulinreducingprocessingtimewhenloopingthroughmultiplestrings,lookingforthesamestringwiththesamemodifiers.ThelistingofdatasetO_MODIFIERbelowshowsthat,eventhoughtheLOOK_FORvaluewaschangedtoXinthesecondobservationandBinthethirdobservation,thefunctioncontinuestosearchfortheletterA.
34SASFunctionsbyExample
ListingofDataSetO_MODIFIER
STRINGLOOK_FORPOSITION
CapitalAhereA2
LowerahereX7
AppleB1
CharacterFunctions43
FunctionsThatExtractPartsofStrings
Thefunctionsdescribedinthissectioncanextractpartsofstrings.Whenusedonthelefthandsideoftheequalsign,theSUBSTRfunctioncanalsobeusedtoinsertcharactersintospecificpositionsofanexistingstring.
SUBSTR
Toextractpartofastring.WhentheSUBSTRfunctionisusedontheleftsideoftheequalsign,itcanplacespecifiedcharactersintoanexistingstring.
SUBSTR(character-value,start<
length>
startisthestartingpositionwithinthestring.
lengthifspecified,isthenumberofcharacterstoincludeinthe
substring.Ifthisargumentisomitted,theSUBSTRfunctionwillreturnallthecharactersfromthestartpositiontotheendofthestring.
Ifalengthhasnotbeenpreviouslyassigned,thelengthoftheresulting
variablewillbethelengthofthecharacter-value.
Fortheseexamples,letSTRING="
ABC123XYZ"
SUBSTR(STRING,4,2)"
12"
SUBSTR(STRING,4)"
123XYZ"
SUBSTR(STRING,LENGTH(STRING))"
Z"
(lastcharacterinthestring)
44SASFunctionsbyExample
Program1.21:
Extractingportionsofacharactervalueandcreatingacharactervariableandanumericvalue
***Otherfunction:
INPUT;
DATASUBSTRING;
INPUTID$1-9;
LENGTHSTATE$2;
STATE=SUBSTR(ID,1,2);
NUM=INPUT(SUBSTR(ID,7,3),3.);
NYXXXX123
NJ1234567
PROCPRINTDATA=SUBSTRINGNOOBS;
TITLE'
ListingofDataSetSUBSTRING'
Inthisexample,theIDcontainsbothstateandnumberinformation.ThefirsttwocharactersoftheIDvariablecontainthestateabbreviationsandthelastthreecharactersrepresentnumeralsthatyouwanttousetocreateanumericvariable.Extractingthestatecodesisstraightforward.Toobtainanumericvaluefromthelast3bytesoftheIDvariable,itisnecessarytofirstusetheSUBSTRfunctiontoextractthethreecharactersofinterestandtothenusetheINPUTfunctiontodothecharactertonumericconversion.AlistingofdatasetSUBSTRINGisshownnext:
ListingofDataSetSUBSTRING
IDSTATENUM
NYXXXX123NY123
NJ1234567NJ567
CharacterFunctions45
Program1.22:
Extractingthelasttwocharactersfromastring,regardless
ofthelength
LENGTH,SUBSTR;
DATAEXTRACT;
INPUT@1STRING
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Character Functions
![提示](https://static.bingdoc.com/images/bang_tan.gif)