BookmarkSubscribeRSS Feed
texasmfp
Lapis Lazuli | Level 10

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)?

 

 

 

 

 

 

3 REPLIES 3
ed_sas_member
Meteorite | Level 14

 

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;


Kurt_Bremser
Super User

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.

texasmfp
Lapis Lazuli | Level 10

I solved it using eval.  Thanks all

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1563 views
  • 1 like
  • 3 in conversation