Beruflich Dokumente
Kultur Dokumente
storage_length=7
length=3
display=:abc :
Moving the LENGTH Statement
data chars2;
string = 'abc';
length string $ 7;
storage_length = lengthc(string);
length = length(string);
display = ":" || string || ":";
put storage_length= /
length= /
display=;
run;
SAS Log
1 data chars2;
2 string = 'abc';
3 length string $ 7;
WARNING: Length of character variable string has already been set.
Use the LENGTH statement as the very first statement in the DATA
STEP to declare the length of a character variable.
4 storage_length = lengthc(string);
5 length = length(string);
6 display = ":" || string || ":";
7 put storage_length= /
8 length= /
9 display=;
10 run;
storage_length=3
length=3
display=:abc:
The INPUT Function
data special;
***INPUT is a special function often used
for character to numeric conversion;
length c_date $ 10 numeral $ 3;
input c_date numeral;
sas_date = input(c_date,mmddyy10.);
number = input(numeral,3.);
datalines;
11/12/1950 123 Listing of Data Set SPECIAL
9-15-2004 99
c_date numeral sas_date number
ss = 123456789 (numeric)
ss_easy = 123456789 (numeric)
Compress Function (SAS® 9 changes)
COMPRESS (char_value <, comp_string> <,modifiers>)
phone number
(908)235-4490 9082354490
(201) 555-77 99 2015557799
The VERIFY Function
data verify;
input @1 id $3.
@5 answer $5.;
position = verify(answer,'abcde');
datalines;
Verify
001 acbed
002 abxde id answer position
003 12cce 001 acbed 0
004 abc e 002 abxde 3
; 003 12cce 1
004 abc e 4
Watch Out for Trailing Blanks
data trailing;
length string $ 10;
string = 'abc';
position = verify(string,'abcde');
run;
Position = 0
Using VERIFY for Data Cleaning
data clean;
input id $;
***Valid ID's contain letters X,Y, or Z
and digits;
if verify(trim(id),'XYZ0123456789') eq 0
then valid = 'Yes';
else valid = 'No';
datalines; Listing of Data Set CLEAN
12X67YZ
67WXYZ id valid
;
12X67YZ Yes
67WXYZ No
Substring Example
data pieces_parts;
input Id $9.;
length State $ 2;
state = substr(Id,3,2);
Num = input(substr(Id,5),4.);
datalines; Listing of Data Set PIECES_PARTS
XYNY123
XYNJ1234 Id State Num
; XYNY123 NY 123
XYNJ1234 NJ 1234
The SUBSTR Function on the Left-
Hand Side of the Equal Sign
data pressure;
input sbp dbp @@;
length sbp_chk dbp_chk $ 4;
sbp_chk = put(sbp,3.);
dbp_chk = put(dbp,3.);
if sbp gt 160 then
substr(sbp_chk,4,1) = '*';
if dbp gt 90 then
substr(dbp_chk,4,1) = '*';
datalines;
120 80 180 92 200 110
;
The SUBSTR Function on the Left-
Hand Side of the Equal Sign
120 80 120 80
180 92 180* 92*
200 110 200* 110*
Parsing a String
data take_apart;
input @1 Cost $10.;
Integer = input(scan(Cost,1,' /'),8.);
Num = input(scan(Cost,2,' /'),8.);
Den = input(scan(Cost,3,' /'),8.);
if missing(Num) then Amount = Integer;
else Amount = Integer + Num/Den;
datalines;
1 3/4 Listing of Data Set TAKE_APART
Without the length statement, cats and catx would have a length of
200
Some LENGTH Functions
data how_long;
one = 'ABC ';
miss = ' '; /* char missing value */
3 length_one = length(one);
3 lengthn_one = lengthn(one);
6 lengthc_one = lengthc(one);
1
0 length_two = length(miss);
1 lengthn_two = lengthn(miss);
lengthc_two = lengthc(miss);
run;
The COMPARE Function
COMPARE(string1, string2 <,'modifiers'>)
I ignore case
L remove leading blanks
: truncate the longer string to the length of the shorter
string. The default is to pad the shorter string with blanks
before a comparison.
(Note: similar to the =: comparison operator)
xxabcxABCxxbbbb 1 7 2
cbacba 0 6 0
Contact Information
Author: Ron Cody
You may download copies of the Powerpoint
presentation from:
www2.umdnj.edu/codyweb/biocomputing