BookmarkSubscribeRSS Feed
mnew
Calcite | Level 5
Experts:
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!
4 REPLIES 4
SAS83
Fluorite | Level 6
Example 1: It shows default length of the char


data name;
input subject$;
cards;
Social
Physics
Chemistry
nuclearphysics
;
run;

OUTPUT:

This is default output:

Social
Physics
Chemistr
nuclearp

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

output:

Class
H
H

( 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.
mnew
Calcite | Level 5
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.
MichelleHomes
Meteorite | Level 14
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 http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695113.htm
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.

Cheers,
Michelle
http://twitter.com/homesatmetacoda
//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com
JVarghese
Obsidian | Level 7

Hi;

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*/

Cheers!

Jojan.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2607 views
  • 1 like
  • 4 in conversation