Syntax, SCAN function:
SCAN(argument,n<,<delimiters>>)
|
LastName=scan(name,1);
LastName=scan(name,1,' ,');
LastName=scan(name,1);
data work.newnames(drop=name); set cert.staff; LastName=scan(name,1); FirstName=scan(name,2); run;
209 RADCLIFFE ROAD, CENTER CITY, NY, 92716
blank . < ( + | & ! $ * ) ; ^ - / , %
data work.newnames(drop=name);
set cert.staff;
length LastName FirstName $ 12;
LastName=scan(name,1);
FirstName=scan(name,2);
MiddleInitial=scan(name,3);
run;
proc print data=newnames;
run;
Syntax, SUBSTR function:
SUBSTR(argument,
position <,n>)
|
substr(middlename,1,1)
data work.agencyemp(drop=middlename);
set cert.agencyemp;
length MiddleInitial $ 1;
MiddleInitial=substr(middlename,1,1);
run;
proc print data=work.agencyemp;
run;
622
was
replaced by the exchange 433
. You need
to update the character variable Phone in Cert.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. This assignment statement specifies that the
new exchange 433
should be placed in
the variable Phone, starting at character position 1 and replacing
three characters.
data work.temp2;
set cert.temp;
substr(phone,1,3)='433';
run;
proc print data=work.temp2;
run;
433
into
all values of Phone.
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 work.temp2(drop=exchange);
set cert.temp;
Exchange=substr(phone,1,3);
substr(phone,1,3)='433';
run;
proc print data=work.temp2;
run;
622
, the assignment
statement executes to replace the value of Phone.
data work.temp2(drop=exchange);
set cert.temp;
Exchange=substr(phone,1,3);
if exchange='622' then substr(phone,1,3)='433';
run;
proc print data=work.temp2;
run;
MiddleInitial=substr(middlename,1,1);
substr(region,1,3)='NNW';
Syntax, LEFT and RIGHT
function:
LEFT(argument)
RIGHT(argument)
|
data _null_; a='DUE DATE'; b=' DUE DATE'; c=left(a); d=left(b); put c $8.; put d $12.; run;
DUE DATE DUE DATE
data _null_; a='DUE DATE'; b='DUE DATE '; c=right(a); d=right(b); put c $8.; put d $12.; run;
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 work.newaddress(drop=address city state zip); set cert.temp; NewAddress=trim(address)||', '||trim(city)||', '||zip; run;
Syntax, CATX function:
CATX(separator,string-1 <,...string-n>)
|
data work.newaddress(drop=address city state zip);
set cert.temp;
NewAddress=catx(', ',address,city,zip);
run;
proc print data=work.newaddress;
run;
Syntax, INDEX function:
INDEX(source,
excerpt)
|
index(job,'word processing')
data work.datapool; set cert.temp; where index(job,'word processing') > 0; run; proc print data=work.datapool; 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 work.datapool; set cert.temp; where find(job,'word processing') > 0; run; proc print data=work.datapool; run;
Syntax, UPCASE function:
UPCASE(argument)
|
data work.upcasejob; set cert.temp; Job=upcase(job); run; proc print data=work.upcasejob; run;
Syntax, LOWCASE function:
LOWCASE(argument)
|
data work.lowcasecontact;
set cert.temp;
Contact=lowcase(contact);
run;
proc print data=work.lowcasecontact;
run;
Syntax, PROPCASE function:
PROPCASE(argument<,delimiter(s)>)
Note: If you specify delimiter(s),
then the default delimiters are no longer in effect.
|
data work.propcasecontact;
set cert.temp;
Contact=propcase(contact);
run;
proc print data=work.propcasecontact;
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 work.after; set cert.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-- |
data _null_;
string='Hey
Diddle Diddle';
string=compbl(string);
put string;
run; |
Hey Diddle Diddle |
data _null_;
string='125 E Main St';
length address $10;
address=compbl(string);
put address;
run; |
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 is 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;
b=ABCD
data _null_;
x='919-000-000 nc 610-000-000 pa 719-000-000 CO 419-000-000 Oh';
y=compress(x, 'ACHONP', 'i');
put y=;
run;
y=919-000-000 610-000-000 719-000-000 419-000-000