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)?
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.
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.
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.
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.
data state1;
input name : $5. rank status : $50. ;
datalines;
mekelele 1 nohope
torres 1 depress
valdes 1 sotkaal
bhutia 1 fulltension
;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.