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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1372 views
  • 1 like
  • 3 in conversation