Search the site...

SASCRUNCH TRAINING
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
Sentry Page Protection
Please Wait...
Variable Attributes [7-17]


Attribute #4: Variable Length
Picture

​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.
Picture

Variable Length is the memory allocated to the variable.

Insufficient length will cause data loss, which is a huge problem that you must avoid.
Picture

Numeric Variable

The default length assigned to a numeric variable is 8.
Picture

The length of 8 is sufficient to capture up to approximately 15 decimal digits with full precision.
Picture

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;
Picture

​However, the length of 8 can only capture up to 8 characters.
Picture

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;
Picture

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!
Picture

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;
Picture
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.

Exercise
Picture
​
Use the Input and Datalines method to create a data set that contains the data above. 

Ensure sufficient length is assigned to the variables.
Next

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.

Already a member? Go to member's area.