Help using Base SAS procedures

Length Statement

Accepted Solution Solved
Reply
Occasional Contributor bgs
Occasional Contributor
Posts: 15
Accepted Solution

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)?


Length Statement Doubt.PNG

Accepted Solutions
Solution
‎01-18-2016 05:25 AM
Super Contributor
Posts: 490

Re: Length Statement

[ Edited ]

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.

 

View solution in original post


All Replies
Solution
‎01-18-2016 05:25 AM
Super Contributor
Posts: 490

Re: Length Statement

[ Edited ]

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.

 

Trusted Advisor
Posts: 1,118

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
Super User
Posts: 7,077

Re: Length Statement

Posted in reply to FreelanceReinhard

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
Posts: 10,046

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;

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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