Hi:
I am trying to set a length based on a calculated value.
For example, if I read in 36 variables of 6 characters/variable and, concatenate those to a new variable that has a total length of 216. Now the default is 200 characters, so I will get a truncation message. Easily solved by setting a length
length concatvar $ 216;
However, if change the input to just 30 variables of 6 characters each, and concatenate, the new variable is 180 characters. If the length had previously been set at 216, that would leave blanks. And, if I ran it a third time concatenating 50 variables, that is a length of 300.
Is it possible to avoid manually calculating and setting the length; making it dynamic, based on a calculated number (i.e., length concatvar $ (calculated number)?
You can try the code below ad then use the macrovariable &total_length
in a length statement in a data step.
%let list_var = /* list your variables. E.g. var1 var2 var3 var4 */; data _Null_; set sample; /*specify your dataset*/ array variables(*) $ &list_var ; do i=1 to dim(variables); total_lenght + length(variables(i)); end; call symputx("total_length",total_lenght); run; data want; set sample; length new_var $ &total_length; ....; run;
You can get the defined length of a variable from dictionary.columns in proc sql.
So you can run this
proc sql noprint;
select sum(length) into :concat_length
from dictionary.columns
where libname = 'XXX' and memname = 'YYY' and /* insert condition to identify your character columns here */;
quit;
to later use &concat_length in your code.
I solved it using eval. Thanks all
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.