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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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