Help using Base SAS procedures

length of a new variable

Frequent Contributor
Posts: 100

length of a new variable

Got this question from my big SAS book but could not understand the answer.
Which of the following can determine the length of a new variable?
a. the length of the variable's first value
b. the assignment statement
c. the Length statement
d. all of the above.
The answer is D. I could not figure out why A is also correct. I thought length is determined during compilation stage so before SAS runs into the first value of any variable?
Thank you!
Posts: 32

Re: length of a new variable

Example 1: It shows default length of the char

data name;
input subject$;


This is default output:


Example 2: Length of the first variables value
data name1;
set name;
if subject ='Social' then Class ='H';
else if subject ='Physics' then Class ='H plus';



( But the second row in the output should be H Plus instead of H because it takes the length of the first assigment variable)

and as you know you can use length subject $ 15. ;
for the final option.

So, all three are possible hence D is correct.
Frequent Contributor
Posts: 100

Re: length of a new variable

Thanks for the help. I'm still confused. The example one seems to reflect the default character var length is 8 rule. I'm not sure how the length is related to the first value.
Trusted Advisor
Posts: 1,403

Re: length of a new variable

The length can be related to the variable's first value when an assignment is made in the data step (with a constant). Your understanding of compilation and execution is correct and the data is not read until execution...

The following is taken from the SAS 9.2 Language Reference manual
In a DATA step, you can create a new variable and assign it a value by using it for the first time on the left side of an assignment statement. SAS determines the length of a variable from its first occurrence in the DATA step. The new variable gets the same type and length as the expression on the right side of the assignment statement.

I think the wording of the response (a.) in your "big SAS book" is a bit vague as it implies data that you are reading in rather than through an assignment statement.

Posts: 43

Re: length of a new variable


Just imagine, if you use the same variable 3 times conditionally say for eg in if-then-else construct.

if a>100 then length='Long';

else if 50<=a<=100 then length='Normal';

else if a<50 then length='short';

During compilation, the first instance the variable 'length' above is coupled with the value 'Long' which is only 4 bytes long. This is the case which you are confused of.

In the second case, even if the same variable is assigned with value 'Normal', which is 6 bytes long, it cannot be accommodated, means its get truncated to 4 char length.

So, the first value  which is 4 char is determined as the variable's length for later use!

To avoid such truncations, SAS have LENGTH  statement which you should use well before you actually use the variables in your statements like a declaration. You need to find the space needed for the longest value of the variable  and then assign this number to that. So that there is no truncation happens like I said before. Hope this helps!

LENGTH length $ 6;

if a>100 then length='Long';

else if 50<=a<=100 then length='Normal';

else if a<50 then length='short'; /* Now its perfect*/



Ask a Question
Discussion stats
  • 4 replies
  • 4 in conversation