SAS : Read Character Variable of Varying Length

This tutorial demonstrates how we can read or import data with a character variable of varying length. We generally encounter this situation when we have company names or both first and last names of a person in our dataset.

Example I

In the following example, the variable "Name" has varying length i.e. not all observations of this variable has similar length.

Example Dataset
Read Messy Data

Method I : Use COLON Modifier

We can use colon modifier : to tell SAS to read variable "Name" until there is a space or other delimiter. The  $30. defines the variable as a character variable having max length 30.
data example1;
input ID Name :$30. Score;
cards;
1 DeepanshuBhalla 22
2 AttaPat 21
3 XonxiangnamSamnuelnarayan 33
;
proc print noobs;
run;
The colon modifier is also used to read numeric data that contains special characters such as comma For example 1,000.


Method II : Use LENGTH statement prior to INPUT Statement

In the following program, we use a length statement prior to input statement to adjust varying length of a variable. In this case, the variable Name would be read first. Use only $ instead of $30. after "Name" in INPUT statement.
data example2;
length Name $30.;
input ID Name $ Score;
cards;
1 DeepanshuBhalla 22
2 AttaPat 21
3 XonxiangnamSamnuelnarayan 33
;
proc print noobs;
run;
Output
It changes the order of variables as the variable Name would be read first. 

Method III : Use Ampersand (&) and Put Extra Space

We can use ampersand (&) to tell SAS to read the variable until there are two or more spaces as a delimeter. This technique is very useful when the variable contains two or more words. For example, if we have observation like "Deepanshu Bhalla" rather than "DeepanshuBhalla".

Note : 2 spaces before 22, 21 and 33
data example1;
input ID Name & $30. Score;
cards;
1 DeepanshuBhalla  22
2 AttaPat  21
3 XonxiangnamSamnuelnarayan  33
;
proc print noobs;
run;

Example II : When a variable contains more than 1 word

In this case, we have a space between First Name and Last Name and we want to store both the first and last names in a single variable.

Example 2 : Read Messy Data

In this case, the following methods do not work.

  1. Colon modifier (:) does not work for a variable having multiple words
  2.  LENGTH Statement prior to INPUT Statement does not work here.

Use Ampersand (&) and add ADDITIONAL space works.
data example1;
input ID Name & $30. Score;
cards;
1 Deepanshu Bhalla  22
2 Atta Pat  21
3 Xonxiangnam Samnuelnarayan  33
;
proc print noobs;
run;

This trick works in reading data from external file.
data temp;
infile "C:\Users\Deepanshu\Desktop\file1.txt";
input ID Name & $30. Score;
proc print noobs;
run;

SAS Tutorials : 100 Free SAS Tutorials

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

8 Responses to "SAS : Read Character Variable of Varying Length"

  1. Nice tips to resolve time consuming issues for SAS beginners

    ReplyDelete
  2. Hi,

    It it possible to get all sas tuturials as an PDF file ? :)

    ReplyDelete
    Replies
    1. ctrl+p , you can save this into pdf

      Delete
  3. really learnt a new thing here.....
    appreciating your efforts
    thanx

    ReplyDelete
  4. I appreciate your efforts in explainnig this...
    Thanks.

    ReplyDelete

Next → ← Prev