Syntax, SCAN function:
SCAN(argument,n<,<delimiters>>)
|
LastName=scan(name,1);
LastName=scan(name,1,' ,');
LastName=scan(name,1);
data hrd.newtemp(drop=name); set hrd.temp; LastName=scan(name,1); FirstName=scan(name,2); MiddleName=scan(name,3); run;
209 RADCLIFFE ROAD, CENTER CITY, NY, 92716
blank . < ( + | & ! $ * ) ; ^ - / , %
data hrd.newtemp(drop=name);
set hrd.temp;
length LastName FirstName MiddleName $ 10;
lastname=scan(name,1);
firstname=scan(name,2);
middlename=scan(name,3);
run;
Syntax, SUBSTR function:
SUBSTR(argument,
position <,n>)
|
substr(middlename,1,1)
data work.newtemp(drop=middlename); set hrd.newtemp; length MiddleInitial $ 1; MiddleInitial=substr(middlename,1,1); run;
622
was
replaced by the exchange 433
. You need
to update the character variable Phone in Hrd.Temp to reflect this
change.
SUBSTR(argument,position,n)
MiddleInitial=substr(middlename,1,1);
substr(region,1,3)='NNW';
92
, you write the following
assignment statement: substr(test,4,2)='92'; Test Test S7381K2 → S7392K2 S7381K7 → S7392K7
622
exchange
in the variable Phone. First, write this assignment statement: data hrd.temp2;
set hrd.temp;
substr(phone,1,3)='433';
run;
433
into
all values of Phone. You need to replace only the values of Phone
that contain the 622 exchange. To extract the exchange from Phone,
add an assignment statement to the DATA step. Notice that the SUBSTR
function is used on the right side of the assignment statement. data hrd.temp2(drop=exchange);
set hrd.temp;
Exchange=substr(phone,1,3);
substr(phone,1,3)='433';
run;
data hrd.temp2(drop=exchange);
set hrd.temp;
Exchange=substr(phone,1,3);
if exchange='622' then substr(phone,1,3)='433';
run;
MiddleInitial=substr(middlename,1,1);
substr(region,1,3)='NNW';
Syntax, LEFT and RIGHT
function:
LEFT(argument)
RIGHT(argument)
|
SAS Statement
|
Result
|
---|---|
|
----+----1----+ |
a=' DUE DATE'; b=left(a); put b; |
DUE DATE |
SAS Statement
|
Result
|
---|---|
|
----+----1----+ |
a='Due Date '; b=right(a); put a $10.; put b $10.; |
Due Date Due Date |
FullName = First || Middle || Last;
FullName = trim(First) || trim(Middle) || Last;
Syntax, TRIM function:
TRIM(argument)
|
data work.nametrim; length Name $ 20 First Middle Last $ 10; Name= 'Jones, Mary Ann, Sue'; First = left(scan(Name, 2, ',')); Middle = left(scan(Name, 3, ',')); Last = scan(name, 1, ','); FullName = trim(First) || trim(Middle) ||Last; drop Name; run; proc print data=work.nametrim; run;
data hrd.newtemp(drop=address city state zip); set hrd.temp; NewAddress=trim(address)||', '||trim(city)||', '||zip; run;
Syntax, CATX function:
CATX(separator,string-1 <,...string-n>)
|
data hrd.newtemp(drop=address city state zip);
set hrd.temp;
NewAddress=catx(', ',address,city,zip);
run;
Syntax, INDEX function:
INDEX(source,excerpt)
|
index(job,'word processing')
data hrd.datapool; set hrd.temp; where index(job,'word processing') > 0; run;
index(job,'WORD PROCESSING')
index(upcase(job),'WORD PROCESSING') index(lowcase(job),'word processing')
Syntax, FIND function:
FIND(string,substring<,modifiers><,startpos> )
Note: If string or substring is
a character literal, you must enclose it in quotation marks.
|
find(job,'word processing')
data hrd.datapool; set hrd.temp; where find(job,'word processing') > 0; run; proc print data=hrd.datapool; run;
Syntax, UPCASE function:
UPCASE(argument)
|
data hrd.newtemp; set hrd.temp; Job=upcase(job); run; proc print data=hrd.newtemp; run;
Syntax, LOWCASE function:
LOWCASE(argument)
|
data hrd.newtemp;
set hrd.temp;
Contact=lowcase(contact);
run;
proc print data=hrd.newtemp;
run;
Syntax, PROPCASE function:
PROPCASE(argument<,delimiter(s)>)
Note: If you specify delimiter(s),
then the default delimiters are no longer in effect.
|
data hrd.newtemp; set hrd.temp; Contact=propcase(contact); run; proc print data=hrd.newtemp; run;
Syntax, TRANWRD function:
TRANWRD(source,target,replacement)
Note: target and replacement can
be specified as variables or as character strings. If you specify
character strings, be sure to enclose the strings in quotation marks
(' ' or " ").
|
name=tranwrd(name,'Monroe','Manson')
data hrd.after; set hrd.before; name=tranwrd(name,'Miss','Ms.'); name=tranwrd(name,'Mrs.','Ms.'); run; proc print data=work.after; run;
Syntax, COMPBL function:
COMPBL(source)
|
SAS Statement
|
Result
|
---|---|
|
----+----1----+-—-2-- |
string='Hey Diddle Diddle'; string=compbl(string); put string; |
Hey Diddle Diddle |
string='125 E Main St'; length address $10; address=compbl(string); put address; |
125 E Main |
Syntax, COMPRESS function:
COMPRESS(source<, characters> <, modifier(s)>)
|
Number of Arguments
|
Result
|
---|---|
only the first argument, source
|
All blanks have been
removed from the argument. If the argument is completely blank, then
the result is a string with a length of zero. If you assign the result
to a character variable with a fixed length, then the value of that
variable will be padded with blanks to fill its defined length.
|
the first two arguments, source and chars
|
All characters that
appear in the second argument are removed from the result.
|
three arguments, source, chars,
and modifier(s)
|
The K modifier (specified
in the third argument) determines whether the characters in the second
argument are kept or removed from the result.
|
compress(source, "1234567890"); compress(source, , "d");
compress(source, "1234567890+-"); compress(source, "+-", "d");
data _null_; a='A B C D'; b=compress(a); put b=; run;
SAS Statement
|
Result
|
---|---|
|
----+----1 |
a='A B C D '; b=compress(a); put b; |
ABCD |