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.
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.
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.