Sentry Page Protection
Variable Attributes [7-17]
Attribute #4: Variable Length
Variable Length is the number of bytes assigned to a variable.
One byte generally contains 8 bits, which is the minimum storage capable of holding a single character on your computer.
If you aren't familiar with the concept of byte, please review the details here.
Variable Length is the memory allocated to the variable.
Insufficient length will cause data loss, which is a huge problem that you must avoid.
Numeric Variable
The default length assigned to a numeric variable is 8.
The length of 8 is sufficient to capture up to approximately 15 decimal digits with full precision.
The largest number you can capture is 9,007,199,254,740,992.
In general, having the length of 8 will be sufficient to handle the majority of your numeric data.
Character Variable
Character variable is another story.
The default length is 8 when the variable is created using the Input statement.
Example
Data List;
Input Name $;
Datalines;
Mary
John
Sam
;
Run;
However, the length of 8 can only capture up to 8 characters.
Data will be truncated if the value is more than 8-character long.
Example
Data Char;
Input Name $ Gender $;
Datalines;
Christopher M
Elizabeth F
Kanjirathinkal M
;
Run;
All the names with more than 8 characters got cut off.
This is a huge problem because you would potentially lose tons of data due to insufficient length!
Increase the Variable Length
When data truncation happens, simply use the Length statement to assign a new length to the variable.
Example
Data Char;
Length Name $15;
Input Name $ Gender $;
Datalines;
Christopher M
Elizabeth F
Kanjirathinkal M
;
Run;
Note:
Length Name $15
1. A dollar sign ($) is added before the number 15. This is needed when adjusting the length of a character variable.
Final Note on Variable Length
The initial length assigned to a character variable varies based on how the variable is created.
The length could be different than 8 when the variable is created by functions instead of the Input statement.
Ensure sufficient length for character variable is absolutely crucial when managing data in SAS.
Length Name $15
1. A dollar sign ($) is added before the number 15. This is needed when adjusting the length of a character variable.
Final Note on Variable Length
The initial length assigned to a character variable varies based on how the variable is created.
The length could be different than 8 when the variable is created by functions instead of the Input statement.
Ensure sufficient length for character variable is absolutely crucial when managing data in SAS.
Exercise
Use the Input and Datalines method to create a data set that contains the data above.
Ensure sufficient length is assigned to the variables.
Use the Input and Datalines method to create a data set that contains the data above.
Ensure sufficient length is assigned to the variables.
Need some help?
HINT:
The default length for character variable is 8. Any variable that has values more than 8-character must have a new length assigned to them.
SOLUTION:
Data Prof;
Length PhoneNo $12;
Input Name $ PhoneNo $ Gender $;
Datalines;
Jason 123-456-7890 Male
Benjamin 987-645-3210 Female
;
Run;
Fill out my online form.