New Functions in SAS 9
New Functions in SAS 9
||
ADDBLANKS=SUBPAD(FIELD);
NEW=FIND(FIELD,HI);
NEW=STRIP(FIELD);
Y=A/B;
How could something so simple cause
problems? Easy, if you did not now you had
records with a value of 0 for B.
A=abcedfghijhih;
B= ' xy ';
Statement
Old1=index(a,hi);
Old2=index(a,HI);
Old3=index(upcase(a),HI);
New1=find(a,'hi');
New2=find(a,'HI');
New3=find(a,'HI','i');
New4=find(b, ' xy ','t');
NEW=NOTALPHA(FIELD);
Returns
8
0
8
8
0
8
2
Returns
11
8
11
11
SPOTTING TYPOS
Do you need to check your data to make
sure the values are legitimate. For example,
you need to check that only the letters A-Z
appear in a field.
OLDWAY=VERIFY(FIELD,
ABCDEFGHJKLMNOPQRSTUVWXYZ)
;
While it looks simple, you had to type every
letter from A-Z so it might be easy to miss
one! (Did you notice if a letter was missing
in the above statment?)
The new method is much easier:
Result
3
0
5
2
1
3
2
1
1
9
7
3
A= '$_ab12A: 5';
Statement
X1=notalnum(a);
X2=notcntrl(a);
X3=notdigit(a);
X4=notfirst(a);
X5=notgraph(a);
X6=notlower(a);
X7=notname(a);
X8=notprint(a);
X9=notpunct(a);
X10=notspace(a);
X11=notupper(a);
X12=notxdigit(a);
Result
1
1
1
1
9
1
1
0
3
1
1
1
A=;
B=LONGER;
C=LONG WITH BLANKS
Length D $200;
D=EXTRA LONG;
STATEMENT
A0=LENGTH(A);
A1=LENGTHN(A);
A2=LENGTHC(A);
A3=LENGTHM(A);
RETURNS
1
0
1
1
B="LONG";
B0=LENGTH(B);
B1=LENGTHN(B);
B2=LENGTHC(B);
B3=LENGTHM(B);
4
4
4
4
C0=LENGTH(C);
C1=LENGTHN(C);
C2=LENGTHC(C);
C3=LENGTHM(C);
16
16
24
24
D0=LENGTH(D);
D1=LENGTHN(D);
D2=LENGTHC(D);
D3=LENGTHM(D);
10
10
200
200
SAME OR NOT?
Do you need to check if two fields are the
same? Then COMPARE is for you (do not
confuse it with the PROC which works on
datasets).
X=name;
Y=naem;
Z=COMPARE(X,Y);
Z=COMPARE(Y,X);
then COMPARE returns -3. Why?
The sign of the result tells you which field
comes first based on your sort sequence. It
is negative if the first field listed would sort
before the second field listed. Otherwise, it
is positive. In this case, on a Windows
system, naem would sort before name
so the result was negative. Remember, sort
order can vary by operating system and you
can overrice the sort order with options.
You have some other options. For example,
you can ignore case in doing your
comparison, remove leading blanks or
quotation marks, and truncate fields to the
shorter length.
Here are a few more examples.
X
Y
Comparison
Name naem (x,y)
Name naem (y,x)
Name naem (x,y,i)
X
X
(x,y)
X
X
(x,y,l)
ab
AB (x,y)
ab
AB (x,y,n)
ABC AB
(x,y)
ABC AB
(x,y,:)
Result
-1
1
3
-1
0
2
0
3
0
(123.45,.1)
(123.55,.1)
(3.156,.003)
(3.157,.003)
ROUND
123.5
123.6
3.156
3.156
ROUNDE
123.4
123.6
3.156
3.156
ROUNDZ
123.4
123.5
3.156
3.156
Regular
1
1
1
1
Z
1
0
0
1
http://www.listserv.uga.edu/archives/sas-l.html
CONCLUSION
There are lots of new functions in SAS 9.
Unless you want to be considered out-ofdate, youll want to review the functions and
see which ones can improve your work. As
I pointed out in some of the examples,
though, make sure you understand the
function, what it does and how to use the
parameters correctly or you could end up
with wrong results.
CONTACT INFORMATION
Deb Cassidy
[email protected]
614-757-7136