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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.