Proc Print Data = One; Where Char = 23; Run;

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.

Suppose you want to read a variable which holds numeric values with comma in thousands place (or thousand separator).

data ex2; input ID Name:$30. Score fee:$10.; cards; 1 DeepanshuBhalla 22 1,000 2 AttaPat 21 2,000 3 XonxiangnamSamnuelnarayan 33 3,000 ; run;                  

In the above program, we have used colon modifier to load "fee" variable and used $ sign to read this variable. It is stored as a character variable.If you would not use $ sign for the same, it will return missing values. See the program below how to store it as a numeric variable.

data ex2; input ID Name:$30. Score fee comma5. ; cards; 1 DeepanshuBhalla 22 1,000 2 AttaPat 21 2,000 3 XonxiangnamSamnuelnarayan 33 3,000 ; run;                  

comma5. informat removes comma and store it as a numeric variable. 5 refers to width of the input field. To read bigger number like 3,000,000, you can use comma10.

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

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

Next → ← Prev

Proc Print Data = One; Where Char = 23; Run;

Source: https://www.listendata.com/2016/06/sas-read-character-variable-of-varying.html

0 Response to "Proc Print Data = One; Where Char = 23; Run;"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel