Reading Free-Format Data | |
Changing the Length of Character Values |
The LENGTH Statement
You can assign the length of a character variable by using the LENGTH statement. |
General form, LENGTH statement using list
input:
where
|
You can assign the same length to more than one variable in a single LENGTH statement. |
length LastName FirstName $ 20; |
Or, you can assign different lengths to variables in a single LENGTH statement. |
length LastName $ 20 FirstName $ 15 |
In the example below, a length of 12 has been assigned to
accommodate PHILADELPHIA, which is the longest value for
City . |
length City $ 12; |
ANCHORAGE 48081 174431 |
ATLANTA 495039 425022 |
BOSTON 641071 562994 |
CHARLOTTE 241420 314447 |
CHICAGO 3369357 3005072 |
DALLAS 844401 904078 |
DENVER 514678 492365 |
DETROIT 1514063 1203339 |
MIAMI 334859 346865 |
PHILADELPHIA 1949996 1688210 |
SACRAMENTO 257105 275741 |
Remember, variable attributes are defined when the variable
is first encountered in the DATA step. In the program below,
the LENGTH statement precedes the INPUT statement and defines both the length
and type of the variable City . |
data perm.growth; infile citydata; length City $ 12; input city $ Pop70 Pop80; run; proc print data=perm.growth; run; |
Using this method, it is not necessary to specify
City 's type in the INPUT statement. However, leaving the $ in
the INPUT statement will not produce an error. Your output should now display
the complete values for City . |
Obs | City | Pop70 | Pop80 |
1 | ANCHORAGE | 48081 | 174431 |
2 | ATLANTA | 495039 | 425022 |
3 | BOSTON | 641071 | 562994 |
4 | CHARLOTTE | 241420 | 314447 |
5 | CHICAGO | 3369357 | 3005072 |
6 | DALLAS | 844401 | 904078 |
7 |
DENVER | 514678 |
492365 |
8 |
DETROIT | 1514063 |
1203339 |
9 |
MIAMI | 334859 |
346865 |
10 |
PHILADELPHIA | 1949996 |
1688210 |
11 | SACRAMENTO | 257105 | 275741 |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.