Amadeus cookies policy - you'll see this message only once.

Amadeus use cookies on this website. They help us to know a little bit about you and how you use our website, which improves the browsing experience and marketing - both for you and for others. They are stored locally on your computer or mobile device. To accept cookies, continue browsing as normal. Or, go to the privacy policy for more information.

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