1、Character FunctionsC h a p t e r 1Character FunctionsChapter 1: Character Functions 29ExplanationThis straightforward program demonstrates each of the NOT character functions. As with most character functions, be careful with trailing blanks. Notice that the last observation (ABC) contains only thre
2、e characters but since STRING is read with a $5. informat, there are two trailing blanks following the letters ABC. That is the reason you obtain a value of 4 for all the functions except NOTDIGIT, which returns a 1 (the first character is not a digit).A listing of the data set NEGATIVE is shown nex
3、t:Listing of Data Set NEGATIVENOT_ ALPHA_ NOT_ NOT_ NOT_STRING NUMERIC ALPHA DIGIT UPPERABCDE 0 0 1 0abcde 0 0 1 1abcDE 0 0 1 112345 0 1 0 1:#$%& 1 1 1 1ABC 4 4 1 4FIND and FINDCThis pair of functions shares some similarities to the INDEX and INDEXC functions. FIND and INDEX both search a string for
4、 a given substring. FINDC and INDEXC both search for individual characters. However, both FIND and FINDC have some additional capability over their counterparts. For example, this pair of functions has the ability to declare a starting position for the search, the direction of the search, and to ign
5、ore case or trailing blanks.Function: FINDPurpose: To locate a substring within a string. With optional arguments, you can define the starting point for the search, the direction of the search, and ignore case or trailing blanks.Syntax: FIND(character-value, find-string )30 SAS Functions by Examplec
6、haracter-value is any SAS character expression.find-string is a character variable or string literal that contains one or more characters that you want to search for. The function returns the first position in the character-value that contains the find-string. If the find-string is not found, the fu
7、nction returns a 0.The following modifiers (in upper- or lowercase), placed in single or double quotation marks, may be used with FIND:i ignore case.t ignore trailing blanks in both the character variable and thefindstring.start is an optional parameter that specifies the position in the string to b
8、egin the search. If it is omitted, the search starts at the beginning of the string. If it is non-zero, the search begins at the position in the string of the absolute value of the number. If the value is positive, the search goes from left to right; if the value is negative, the search goes from ri
9、ght to left. A negative value larger than the length of the string results in a scan from right to left, starting at the end of the string. If the value of start is a positive number longer than the length of the string, or if it is 0, the function returns a 0.ExamplesFor these examples STRING1 = He
10、llo hello goodbye and STRING2 =helloFunction ReturnsFIND(STRING1, STRING2) 7FIND(STRING1, STRING2, I) 1FIND(STRING1,bye) 17FIND(abcxyzabc,abc,4) 7FIND(STRING1, STRING2, i, -99) 7Chapter 1: Character Functions 31Function: FINDCPurpose: To locate a character that appears or does not appear within a st
11、ring. With optional arguments, you can define the starting point for the search, the direction of the search, to ignore case or trailing blanks, or to look for characters except the ones listed.Syntax: FINDC(character-value, find-characters )character-value is any SAS character expression.find-chara
12、cters is a list of one or more characters that you want tosearch for.The function returns the first position in the character-value thatcontains one of the find-characters. If none of the characters arefound, the function returns a 0. With an optional argument, you can havethe function return the po
13、sition in a character string of a character that is not in the find-characters list.modifiers (in upper- or lowercase), placed in single or double quotation marks, may be used with FINDC as follows:i ignore case.t ignore trailing blanks in both the character variable and thefind-characters.v count o
14、nly characters that are not in the list of find characters. O process the modifiers and find characters only once to a specific call to the function. In subsequent calls, changes to these arguments will have no effect.start is an optional parameter that specifies the position in the string tobegin t
15、he search. If it is omitted, the search starts at the beginning of the string. If it is non-zero, the search begins at the position in the string of the absolute value of the number. If the value is positive, the search goes from32 SAS Functions by Exampleleft to right; if the value is negative, the
16、 search goes from right to left. Anegative value larger than the length of the string results in a scan from right to left, starting at the end of the string. If the value of start is a positive number longer than the length of the string, or if it is 0, the function returns a 0.Note: You can switch
17、 the positions of start and modifiers and the function will work the same.ExamplesFor these examples STRING1 = Apples and Books and STRING2 = abcdeFunction ReturnsFINDC(STRING1, STRING2) 5FINDC(STRING1, STRING2, i) 1FINDC(STRING1,aple,vi) 6FINDC(abcxyzabc,abc,4) 7Program 1.14: Using the FIND and FIN
18、DC functions to search for stringsand characters*Primary functions: FIND and FINDC;DATA FIND_VOWEL;INPUT 1 STRING $20.;PEAR = FIND(STRING,Pear);POS_VOWEL = FINDC(STRING,aeiou,I);UPPER_VOWEL = FINDC(STRING,aeiou);NOT_VOWEL = FINDC(STRING,AEIOU,IV);DATALINES;XYZABCabcXYZApple and Pear;PROC PRINT DATA=
19、FIND_VOWEL NOOBS;TITLE Listing of Data Set FIND_VOWEL;RUN;Chapter 1: Character Functions 33ExplanationThe FIND function returns the position of the characters Pear in the variable STRING. Since the i modifier is not used, the search is case-sensitive. The first use of the FINDC function looks for an
20、y upper- or lowercase vowel in the string (because of the i modifier). The next statement, without the i modifier, locates only lowercase vowels. Finally, the v modifier in the last FINDC function reverses the search to look for the first character that is not a vowel (upper- or lowercase because of
21、 the i modifier).Program 1.15: Demonstrating the o modifier with FINDC*Primary function: FINDC;DATA O_MODIFIER;INPUT STRING $15.16 LOOK_FOR $1.;POSITION = FINDC(STRING,LOOK_FOR,IO);DATALINES;Capital A here ALower a here XApple B;PROC PRINT DATA=O_MODIFIER NOOBS HEADING=H;TITLE Listing of Data Set O_
22、MODIFIER;RUN;ExplanationIn the first call to FINDC, the value of LOOK_FOR is an uppercase A. Since the o modifier was used, changing the value of LOOK_FOR in the next two observations has no effectthe function continues to look for the letter A. Note that another use of FINDC in this DATA step would
23、 not be affected by the previous use of the o modifier, even if the name ofthe variable (in this case POSITION) were the same. The o modifier is most likely useful in reducing processing time when looping through multiple strings, looking for the same string with the same modifiers. The listing of d
24、ata set O_MODIFIER below shows that, even though the LOOK_FOR value was changed to X in the second observation and B in the third observation, the function continues to search for the letter A.34 SAS Functions by ExampleListing of Data Set O_MODIFIERSTRING LOOK_FOR POSITIONCapital A here A 2Lower a
25、here X 7Apple B 1Chapter 1: Character Functions 43Functions That Extract Parts of StringsThe functions described in this section can extract parts of strings. When used on the left hand side of the equal sign, the SUBSTR function can also be used to insert characters into specific positions of an ex
26、isting string.Function: SUBSTRPurpose: To extract part of a string. When the SUBSTR function is used on the left side of the equal sign, it can place specified characters into an existing string.Syntax: SUBSTR(character-value, start )character-value is any SAS character expression.start is the start
27、ing position within the string.length if specified, is the number of characters to include in thesubstring. If this argument is omitted, the SUBSTR function will return all the characters from the start position to the end of the string.If a length has not been previously assigned, the length of the
28、 resultingvariable will be the length of the character-value.ExamplesFor these examples, let STRING = ABC123XYZFunction ReturnsSUBSTR(STRING,4,2) 12SUBSTR(STRING,4) 123XYZSUBSTR(STRING,LENGTH(STRING) Z (last character in the string)44 SAS Functions by ExampleProgram 1.21: Extracting portions of a ch
29、aracter value and creating a character variable and a numeric value*Primary function: SUBSTR*Other function: INPUT;DATA SUBSTRING;INPUT ID $ 1-9;LENGTH STATE $ 2;STATE = SUBSTR(ID,1,2);NUM = INPUT(SUBSTR(ID,7,3),3.);DATALINES;NYXXXX123NJ1234567;PROC PRINT DATA=SUBSTRING NOOBS;TITLE Listing of Data S
30、et SUBSTRING;RUN;ExplanationIn this example, the ID contains both state and number information. The first two characters of the ID variable contain the state abbreviations and the last three characters represent numerals that you want to use to create a numeric variable. Extracting the state codes i
31、s straightforward. To obtain a numeric value from the last 3 bytes of the ID variable, it is necessary to first use the SUBSTR function to extract the three characters of interest and to then use the INPUT function to do the character to numeric conversion. A listing of data set SUBSTRING is shown next:Listing of Data Set SUBSTRINGID STATE NUMNYXXXX123 NY 123NJ1234567 NJ 567Chapter 1: Character Functions 45Program 1.22: Extracting the last two characters from a string, regardlessof the length*Primary functions: LENGTH, SUBSTR;DATA EXTRACT;INPUT 1 STRING
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1