SAS, Ch5, Data Transformations 7.29.18
The ROUND Function example
NewVar=ROUND(argument<,round-off-unit>); NewVar6=round(42.65,.5); argument=is a number or numeric expression round-off-unit=is numeric and positive. If round-off-unit is not provided, argument is rounded to the nearest integer
The SCAN Function example
NewVar=SCAN(string,n<,charlist>); Second=scan(Phrase,2,', '); string=can be a character constant, variable, or expression n=specifies the nth word to extract from string charlist=lists the characters that delimit words
Automatic Numeric to Character Conversion
!! example Phone= '(' !! Area_Code !! ') ' !! Mobile; uses the best 12. format right aligns the resulting character value (fixed using the PUT function)
The CATX Function example
NewVar=CATX(separator, string-1,...string-n) separator=character string that is inserted between the concatenated string-1,...string-n arguments string-1...string-n=can be a character constant, variable, or expression. leading and trailing blanks are removed from each argument
The CEIL Function example
NewVar=CEIL(argument); x=ceil(4.4);
The COMPRESS Function example
NewVar=COMPRESS(source<,chars>); New_ID1=compress (ID);
The FLOOR Funciton example
NewVar=FLOOR(argument); y=floor(3.6);
The INT Function example
NewVar=INT(argument); z=int(3.9);
LENGTH Function example
NewVar=LENGTH(argument); ID=substr(Acct_Code,1,length(Acc_Code)-1);
the PROPCASE Function example
NewVar=PROPCASE(argument<,delimiter(s)>); name = propcase(name);
SAS Functions
a routine that performs a computation and returns a value. use arguments supplied by the user or by the operating environment an argument can be a constant, variable or any sas expression when you use a sas variable list in a sas function, use the keyword OF in front of the first variable name in the list
Concatenation Operator
an operator that joins character strings
Data Conversion
can be done in two ways: 1.automatically by allowing SAS to do it for you (add an arithmetic expression) 2.explicitly with these functions 2a.INPUT - character to numeric conversion 2b.PUT-numeric to character conversion
LOWCASE(string)
converts all letters in an argument to lowercase
UPCASE(string)
converts all letters in an argument to uppercase
the PROPCASE Funciton
converts all words in an argument to poper case, in which the first letter is uppercase and the remaining letters are lowercase argument=character constant, variable or expression delimiter=characters which separate words if omitted, the default delimiters are the blank, /, -,(,., and tab characters NewVar=is a new variable, it is created with the same length as argument
CAT Functions
converts any numeric argument to a character string by using the BEST12. format and then removing any leading blanks. No note is written to the log.
NMISS
count of missing numeric arguments
CMISS
count of missing numeric or character arguments
converting a variable example
data work.hrdata (drop=CharGross); set orion.convert(rename=(GrossPay=CharGross)); GrossPay=input(CharGross, comma6.);
CAT(string-1,...string-n)
does not remove leading or traililng blanks from the arguments before concatenating them
SAS Functions example
function-name(argument-1, argument-2...) Total=sum(qtr1, qtr2, qtr3, qtr4);
The SUBSTR Function (Right Side) example
NewVar=SUBSTR(string,start<,length>; item_type=substr(item-code,1,3); string-character constant, variable or expression start-specifies the starting position length-specifies the number of characters to extract. the substring consists of the remainder of string newVar-if a new variable, it will be created with the same length as string. to set a different length for newVar , use a LENGTH statement prior to the assignment statement
The CATX Function
joins or concatenates character strings
The TRANWRD Function example
NewVar=TRANWRD(source,target,replacement); product=Tranwrd(Product, 'Luci ', 'Lucky '); source=specifies the source string that you want to change target=specifies the string searched for in source replacement=specifies the string that replaces target
Concatenation Operator example
NewVar=string1 !! string2; Phone = '('!!area!!') '!!Number; operator can also be written as two vertical or two broken vertical bars
The INPUT Function example
NumVar=INPUT(source,informat); NVar1=input(CVar1,5.); source=contains a sas character expression informat=is the sas informat to apply to the source
CAT Functions example
Phone='(' !! put(area_code, 3.) !! ') ' !! Mobile;
The FIND Function example
Position=FIND(string,substring<,modifiers,startpos>); pos1=find(text, 'US');
The SUBSTR Function (Left Side) example
SUBSTR(string,start<,length>)=value; substr(Location,11,2)='OH'; string=specifies a character variable start=specifies the starting position to replace characters with the value length=specifies the number of characters to replace in string.
LEFT(string)
left aligns a character expression
VARNUM option in PROC CONTENTS
prints a list of the variables by their logical position in the data set
STRIP(string)
removes all leading and trailing blanks from a character string
CATS(string-1,...,string-n)
removes leading and trailing blanks from the arguments
COMPBL(string)
removes multiple blanks from a character string by translating each occurrence of two or more consecutive blanks into a single blank
The COMPRESS Function
removes the characters listed in the chars argument form the source if no chars are specified the function removes all blanks from the source
TRIM(string)
removes trailing blanks from a character string
CATT(string-1,...string-n)
removes trailing blanks from the arguments
The SUBSTR Function (Left Side)
replaces characters in a character variable
The TRANWRD Function
replaces or removes all occurrences of a given word (or a pattern of characters) within a character string. when using this function these details apply: 1. does not remove trailing blanks from target or replacement 2. if NewVar was not previously defined, it is given a length of 200 3. if the target string is not found in the source, then no replacement occurs
CHAR(string.position)
returns a single character from a specified position in a character string
The ROUND Function
returns a value rounded to the nearest multiple of the round off unit
The FLOOR Funciton
returns the greatest integer less than or equal to the argument
The INT Function
returns the integer portion of the argument
LENGTH Function
returns the length of a non-blank character string, excluding trailing blanks
The SCAN Function
returns the nth word of a character value a missing value is returned if there are fewer than n words in the string if n is negative, the function selects the word in the character string starting from the end of the string delimiters before hte first word have no effect any character or set of characters can serve as delimiters two or more contiguous delimiters are treated as a single delimiter the length of the created variable is 200 bytes a good practice is to explicitly define the length of any created variable with a LENGTH statement
The CEIL Function
returns the smallest integer greater than or equal to the argument
The INPUT Function
returns the value produced when the source is read with a specified informat used to explicitly convert character values to numeric
RIGHT(string)
right aligns a character expression
PUT Function example
CharVar=PUT(source,format); CVar1=put(NVar1,3.); source=identifies the SAS variable or constant whose value you want to reformat. This argument can be character or numeric format=contains the sas format that you want applied to the variabe or constant that is specified in the source. It msut agree with the surce in type.
Automatic Character to Numeric Conversion
sas automatically converts a character value to a numeric value when the character value is used in a numeric context, such as the following: assignment to a numeric variable an arithmetic operation logical comparison with a numeric value a function that takes numeric arguments
rename example
sas-data-set(RENAME=(od-name=new-name))
The FIND Function
searches a target string for a specified substring returns a numeric value that is: 1. the starting position of the first occurrence of substring within string, if substring is found 2. 0, if substring is not found modifiers can be: 1. I to indicate a case insensitive search 2. T to indicate to ignore trailing blanks in the string and substring values. startpos indicates where in the string to start searching for the substring.
Steps to converting a variable to another data type
steps: 1. use the rename= data set option to rename the variable that you want to convert sas-data-set(RENAME=(od-name=new-name)) 2. Use the INPUT function in an assignment statement to create a new variable with the original name of the variable that you renamed GrossPay=input(CharGross,comma6.); 3. use a drop= data set option in the data statement to exclude the original variable from the output sas data set. data work.hardata (drop=CharGross);
The SUBSTR Function (Right Side)
used to extract characters for example, can be used to create a new variable from some existing variable
PUT Function
writes values with a specific format returns the value produced when source is written with format always returns a character string