🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Obsidian | Level 7

Length Statement

data state1;
input name \$ rank status \$ ;
length name \$5 status \$50;
datalines;
mekelele 1 nohope
torres 1 depress
valdes 1 sotkaal
bhutia 1 fulltension
;
run;

In the above statement - I have particularly set the length of variable name to \$5 and status to \$50

By that logic I must get:

for mekelele - makel(only 5 characters) and for fulltension - fulltension (since length is \$50 the whole word must come)

However, i am  not gettign the desired outcome (see screenshot attached)?

1 ACCEPTED SOLUTION

Accepted Solutions
Barite | Level 11

Re: Length Statement

Because the INPUT statment will figure out variables length based on some observation automatically. If you want the LENGTH statment to have effect let it be before the INPUT statment.

As

``````data state1;
length name \$5 status \$50;
input name \$ rank status \$ ;
datalines;
mekelele 1 nohope
torres 1 depress
valdes 1 sotkaal
bhutia 1 fulltension
;
run;``````

If you checked your log you will figure out that easily by reading the WARNINGs you got

``````WARNING: Length of character variable name 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.
WARNING: Length of character variable status 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 REPLIES 4
Barite | Level 11

Re: Length Statement

Because the INPUT statment will figure out variables length based on some observation automatically. If you want the LENGTH statment to have effect let it be before the INPUT statment.

As

``````data state1;
length name \$5 status \$50;
input name \$ rank status \$ ;
datalines;
mekelele 1 nohope
torres 1 depress
valdes 1 sotkaal
bhutia 1 fulltension
;
run;``````

If you checked your log you will figure out that easily by reading the WARNINGs you got

``````WARNING: Length of character variable name 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.
WARNING: Length of character variable status 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.``````

Jade | Level 19

Re: Length Statement

I support @mohamed_zaki's advice to put the LENGTH statement before the INPUT statement. However, I don't think that SAS looks at the data values when it determines the lengths. Apparently, it assigns the default length to all character variables (marked with the dollar sign) found in the INPUT statement, i.e. \$8, regardless of the actual lengths of the data values. Interestingly, SAS is flexible enough to adjust the length of numeric variables from the default 8 to, say, 4 (bytes) if this length is specified in the LENGTH statement after the INPUT statement, without complaints in the log.

Super User

Re: Length Statement

SAS will define the variable type and length at the first place that it sees the variable. So if that is when you reference an existing data set then the variable will take the attributes it has in that data set.  If the first place is in an INPUT statement then it will attempt to use the format used to define the type and length. It you do not give it enough information it will default to length 8 for numbers and length \$8 for character variables.

Super User

Re: Length Statement

``````data state1;
input name : \$5. rank status : \$50. ;
datalines;
mekelele 1 nohope
torres 1 depress
valdes 1 sotkaal
bhutia 1 fulltension
;
run;``````

Discussion stats
• 4 replies
• 8961 views
• 4 likes
• 5 in conversation