Skip to main Content

The Length Statement and Character Variables

Suppose we run the following code, PRODGRP being a new variable not present in SASHELP.SHOES:

data shoes; 
 set sashelp.shoes (keep=product); 
 if product=:'Men' then prodgrp='Men'; 
 else if product=:'Women' then prodgrp='Women';
 else prodgrp='Unspecified';
run;

 

Then the length of variable PRODGRP is 3, and its value for products that begin with 'Women' is 'Wom', as the Proc FREQ output shows:

prodgrp    Frequency
Men              95
Uns             204
Wom              96

 

The length of the variable is determined by the first reference to it - in this case when it is set to 'Men'.

We can make sure the length is as we want it by using a LENGTH statement at the start of the data step.

data shoes; 
 set sashelp.shoes (keep=product); 
 length prodgrp $ 11;
 if product=:'Men' then prodgrp='Men'; 
 else if product=:'Women' then prodgrp='Women';
 else prodgrp='Unspecified';
run;

 

The Proc FREQ output now shows the variable PRODGRP is set long enough to display all the information needed:

prodgrp        Frequency
Men                  95
Unspecified         204
Women                96


A LENGTH statement when used before the SET statement is one way of putting variables into the order you want. The following code will mean the variables in the dataset 'shoes' are stored in the order PRODGRP and then PRODUCT. Without the LENGTH statement, or if it was placed after the SET statement, the order would be as PRODUCT then PRODGRP.

data shoes; 
 length prodgrp $ 11 product $ 14;
 set sashelp.shoes (keep=product); 
 if product=:'Men' then prodgrp='Men'; 
 else if product=:'Women' then prodgrp='Women';
 else prodgrp='Unspecified';
run;