We have some more SAS character functions for data cleaning. We can call this group of SAS Functions as "ANY" function group.
We can use given functions to search digits, alphanumeric (upper and lower cases, letters and digits), alpha (completely character), punctuation characters and space characters (tab, blanks, line feeds).
For these SAS Character functions you must be familiar with LOCALE and TRANTAB SAS System options.
ANYALNUM Function: It gives the first position of alphanumeric character from a variable after searching for a alphanumeric character string in a character variable.
Syntax: Any_Alnum = ANYALNUM (String,Start_Position);
ANYALPHA Function: It gives the first position of an alphabetic character from a variable after searching for a alphabetic character string in an character variable.
Syntax: Any_Alpha=ANYALPHA (String,Start_Position);
ANYDIGIT Function: It gives the first position of a digit from a character string after searching for any digit in character variable.
Syntax: Any_Digit=ANYDIGIT (String,Start_Position);
ANYPUNCT Function: It gives the first position of a punctuation character from a character string after searching for punctuation in character variable.
Syntax: Any_Punct=ANYPUNCT (String,Start_Position);
ANYSPACE Function: It gives the first position of a white – space character (Horizontal and Vertical tab, Blank, Carriage, line feed and form feed) from a character string after searching for a white-space in character variable.
Syntax: Any_Space=ANYSPACE (String,Start_Position);
These functions would return a value zero if any following is true:
Combined code for all above functions in one shot:
Data DSN;
Input Practice $26.;
Alpha_Num=ANYALNUM (Practice);
Alpha_Num_L=ANYALNUM (Practice,-999);
Alpha=ANYALPHA (Practice);
Alpha_L=ANYALPHA (Practice,-999);
Digit=ANYDIGIT (Practice);
Digit_L=ANYDIGIT (Practice,-999);
Punct=ANYPUNCT (Practice);
Space=ANYSPACE (Practice);
Cards;
My Employee Id is 5001710
Brawvo ! well done
1357924680
;
Run;
Example: How could we use these functions for data cleaning
Data Alpha_only Mixed Punct;
Input Practice $26.;
If Anydigit (Practice) then output mixed;
If Anypunct (Practice) then output Punct;
Else output Alpha_only;
Cards;
My Employee Id is 5001710
Brawvo ! well done
1357924680
;
Run;
We can use given functions to search digits, alphanumeric (upper and lower cases, letters and digits), alpha (completely character), punctuation characters and space characters (tab, blanks, line feeds).
For these SAS Character functions you must be familiar with LOCALE and TRANTAB SAS System options.
ANYALNUM Function: It gives the first position of alphanumeric character from a variable after searching for a alphanumeric character string in a character variable.
Syntax: Any_Alnum = ANYALNUM (String,Start_Position);
ANYALPHA Function: It gives the first position of an alphabetic character from a variable after searching for a alphabetic character string in an character variable.
Syntax: Any_Alpha=ANYALPHA (String,Start_Position);
ANYDIGIT Function: It gives the first position of a digit from a character string after searching for any digit in character variable.
Syntax: Any_Digit=ANYDIGIT (String,Start_Position);
ANYPUNCT Function: It gives the first position of a punctuation character from a character string after searching for punctuation in character variable.
Syntax: Any_Punct=ANYPUNCT (String,Start_Position);
ANYSPACE Function: It gives the first position of a white – space character (Horizontal and Vertical tab, Blank, Carriage, line feed and form feed) from a character string after searching for a white-space in character variable.
Syntax: Any_Space=ANYSPACE (String,Start_Position);
- String: Could be any variable, any expression to search and any character constant
- Start_position: Any optional integer which tells about the position from which the search should start and some time the direction to start
- If you want that search should be right aligned, then Start_Position value should be positive
- In the same way if Start_Position value is negative, then search would be proceeds to the left
These functions would return a value zero if any following is true:
- If searching character not found
- If the value of Start_Position is more than the length of the String
- If value of Start_Position is equals to zero
Combined code for all above functions in one shot:
Data DSN;
Input Practice $26.;
Alpha_Num=ANYALNUM (Practice);
Alpha_Num_L=ANYALNUM (Practice,-999);
Alpha=ANYALPHA (Practice);
Alpha_L=ANYALPHA (Practice,-999);
Digit=ANYDIGIT (Practice);
Digit_L=ANYDIGIT (Practice,-999);
Punct=ANYPUNCT (Practice);
Space=ANYSPACE (Practice);
Cards;
My Employee Id is 5001710
Brawvo ! well done
1357924680
;
Run;
- DSN is the name of the Dataset
Example: How could we use these functions for data cleaning
Data Alpha_only Mixed Punct;
Input Practice $26.;
If Anydigit (Practice) then output mixed;
If Anypunct (Practice) then output Punct;
Else output Alpha_only;
Cards;
My Employee Id is 5001710
Brawvo ! well done
1357924680
;
Run;
- Alpha_only, Mixed and Punct are newly created data set
Hello Uma,
ReplyDeleteI just want to thank you for your time and efforts while you are putting
such a useful sas stuff.
I appreciate your dedication.
Please keep it up.
Jagdish
Thank you for sharing it helps me to understand the ANY function. Could you please share more SAS function with detail examples.
ReplyDelete