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

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
1 ACCEPTED SOLUTION

Accepted Solutions
mohamed_zaki
Barite | Level 11

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

4 REPLIES 4
mohamed_zaki
Barite | Level 11

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.

 

FreelanceReinh
Jade | Level 19

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.

Tom
Super User Tom
Super User

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.

Ksharp
Super User

 

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

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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