Character Functions.docx

上传人:b****8 文档编号:9776024 上传时间:2023-02-06 格式:DOCX 页数:19 大小:21.55KB
下载 相关 举报
Character Functions.docx_第1页
第1页 / 共19页
Character Functions.docx_第2页
第2页 / 共19页
Character Functions.docx_第3页
第3页 / 共19页
Character Functions.docx_第4页
第4页 / 共19页
Character Functions.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

Character Functions.docx

《Character Functions.docx》由会员分享,可在线阅读,更多相关《Character Functions.docx(19页珍藏版)》请在冰豆网上搜索。

Character Functions.docx

CharacterFunctions

Chapter1

CharacterFunctions

Chapter1:

CharacterFunctions29

Explanation

Thisstraightforwardprogramdemonstrateseachofthe"NOT"characterfunctions.Aswithmostcharacterfunctions,becarefulwithtrailingblanks.Noticethatthelastobservation("ABC")containsonlythreecharactersbutsinceSTRINGisreadwitha$5.informat,therearetwotrailingblanksfollowingtheletters'ABC'.Thatisthereasonyouobtainavalueof4forallthefunctionsexceptNOTDIGIT,whichreturnsa1(thefirstcharacterisnotadigit).

AlistingofthedatasetNEGATIVEisshownnext:

ListingofDataSetNEGATIVE

NOT_ALPHA_NOT_NOT_NOT_

STRINGNUMERICALPHADIGITUPPER

ABCDE0010

abcde0011

abcDE0011

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

Chapter1:

CharacterFunctions31

Function:

FINDC

Purpose:

Tolocateacharacterthatappearsordoesnotappearwithinastring.Withoptionalarguments,youcandefinethestartingpointforthesearch,thedirectionofthesearch,toignorecaseortrailingblanks,ortolookforcharactersexcepttheoneslisted.

Syntax:

FINDC(character-value,find-characters

<,'modifiers'><,start>)

character-valueisanySAScharacterexpression.

find-charactersisalistofoneormorecharactersthatyouwantto

searchfor.

Thefunctionreturnsthefirstpositioninthecharacter-valuethat

containsoneofthefind-characters.Ifnoneofthecharactersare

found,thefunctionreturnsa0.Withanoptionalargument,youcanhave

thefunctionreturnthepositioninacharacterstringofacharacterthatisnotinthefind-characterslist.

modifiers(inupper-orlowercase),placedinsingleordoublequotationmarks,maybeusedwithFINDCasfollows:

iignorecase.

tignoretrailingblanksinboththecharactervariableandthe

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.

Examples

FortheseexamplesSTRING1="ApplesandBooks"andSTRING2="abcde"

FunctionReturns

FINDC(STRING1,STRING2)5

FINDC(STRING1,STRING2,'i')1

FINDC(STRING1,"aple",'vi')6

FINDC("abcxyzabc","abc",4)7

Program1.14:

UsingtheFINDandFINDCfunctionstosearchforstrings

andcharacters

***Primaryfunctions:

FINDandFINDC;

DATAFIND_VOWEL;

INPUT@1STRING$20.;

PEAR=FIND(STRING,"Pear");

POS_VOWEL=FINDC(STRING,"aeiou",'I');

UPPER_VOWEL=FINDC(STRING,"aeiou");

NOT_VOWEL=FINDC(STRING,"AEIOU",'IV');

DATALINES;

XYZABCabc

XYZ

AppleandPear

;

PROCPRINTDATA=FIND_VOWELNOOBS;

TITLE"ListingofDataSetFIND_VOWEL";

RUN;

Chapter1:

CharacterFunctions33

Explanation

TheFINDfunctionreturnsthepositionofthecharacters"Pear"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');

DATALINES;

CapitalAhereA

LowerahereX

AppleB

;

PROCPRINTDATA=O_MODIFIERNOOBSHEADING=H;

TITLE"ListingofDataSetO_MODIFIER";

RUN;

Explanation

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

Chapter1:

CharacterFunctions43

FunctionsThatExtractPartsofStrings

Thefunctionsdescribedinthissectioncanextractpartsofstrings.Whenusedonthelefthandsideoftheequalsign,theSUBSTRfunctioncanalsobeusedtoinsertcharactersintospecificpositionsofanexistingstring.

Function:

SUBSTR

Purpose:

Toextractpartofastring.WhentheSUBSTRfunctionisusedontheleftsideoftheequalsign,itcanplacespecifiedcharactersintoanexistingstring.

Syntax:

SUBSTR(character-value,start<,length>)

character-valueisanySAScharacterexpression.

startisthestartingpositionwithinthestring.

lengthifspecified,isthenumberofcharacterstoincludeinthe

substring.Ifthisargumentisomitted,theSUBSTRfunctionwillreturnallthecharactersfromthestartpositiontotheendofthestring.

Ifalengthhasnotbeenpreviouslyassigned,thelengthoftheresulting

variablewillbethelengthofthecharacter-value.

Examples

Fortheseexamples,letSTRING="ABC123XYZ"

FunctionReturns

SUBSTR(STRING,4,2)"12"

SUBSTR(STRING,4)"123XYZ"

SUBSTR(STRING,LENGTH(STRING))"Z"(lastcharacterinthestring)

44SASFunctionsbyExample

Program1.21:

Extractingportionsofacharactervalueandcreatingacharactervariableandanumericvalue

***Primaryfunction:

SUBSTR

***Otherfunction:

INPUT;

DATASUBSTRING;

INPUTID$1-9;

LENGTHSTATE$2;

STATE=SUBSTR(ID,1,2);

NUM=INPUT(SUBSTR(ID,7,3),3.);

DATALINES;

NYXXXX123

NJ1234567

;

PROCPRINTDATA=SUBSTRINGNOOBS;

TITLE'ListingofDataSetSUBSTRING';

RUN;

Explanation

Inthisexample,theIDcontainsbothstateandnumberinformation.ThefirsttwocharactersoftheIDvariablecontainthestateabbreviationsandthelastthreecharactersrepresentnumeralsthatyouwanttousetocreateanumericvariable.Extractingthestatecodesisstraightforward.Toobtainanumericvaluefromthelast3bytesoftheIDvariable,itisnecessarytofirstusetheSUBSTRfunctiontoextractthethreecharactersofinterestandtothenusetheINPUTfunctiontodothecharactertonumericconversion.AlistingofdatasetSUBSTRINGisshownnext:

ListingofDataSetSUBSTRING

IDSTATENUM

NYXXXX123NY123

NJ1234567NJ567

Chapter1:

CharacterFunctions45

Program1.22:

Extractingthelasttwocharactersfromastring,regardless

ofthelength

***Primaryfunctions:

LENGTH,SUBSTR;

DATAEXTRACT;

INPUT@1STRING

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 冶金矿山地质

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1