Dear,
I need suggestion. In my sample data below, i have variable a with different lengths ad variable b with same length value. I need to concatenate both variables with space between them and have final values of same length for all rows.
I need to insert blank spaces based length of variable a. Ex: For the first row i need to insert more blank spaces than last record. Please suggest. Thank you
output needed
aa bbbbbb
aaa bbbbbb
aaaa bbbbbb
aaaaa bbbbbb
data hav;
input a $ b $;
datalines;
aa bbbbbb
aaa bbbbbb
aaaa bbbbbb
aaaaa bbbbbb
;
data hav1;
set hav;
c=catx('',a,b);
run;
Strange requirement. Both variables must have a defined maximum length, that has to be used during data import. The variable to hold the combined values must be sum of the lengths +1. The function substr (left of) can be used to place the value of "b" at the right position.
data have;
length a $ 5 b $ 5; /* <- must have, when working with strings */
input a $ b $;
datalines;
aa bbbbbb
aaa bbbbbb
aaaa bbbbbb
aaaaa bbbbbb
;
data want;
set have;
length combined $ 11;
combined = a;
substr(combined, 5+2) = b;
run;
If you know the length of variable a, you can use the repeat function to concatenate space behind it, and then use the substr function to cut off the required length and concatenate it with variable b.
data hav1;
set hav;
c=substr(a||repeat(' ',4),1,6)||b;
run;
SAS v9 character variables are of type CHAR with fixed lengths (padded with blanks up to full length). For this reason "old fashioned" concatenation syntax as proposed by @Astounding will return the desired result. You even don't need to define a length statement for SAS to create the target variable with the appropriate length (sum of lengths of source variables plus 1 for the blank in the concatenation syntax).
No need to go for anything more complicated.
Do you need a variable?
Your current code is defining A with a length of $8 if you want it to only have a maximum of 5 bytes then change the length. Or use SUBSTR() function to only pull the first 5 characters.
c = substr(a,1,5)||' '||b;
Or are you writing to a file?
data _null_;
set hav;
file 'myfile.txt'
put @1 a @7 b;
run;
Now for the complicated part. Are you using UTF-8 (or other multi-byte encoding)? And if so do you need the variable to contain 5 characters from A instead of 5 bytes? If so use the KSUBSTR() function instead.
c = ksubstr(a,1,5)||' '||b;
If this is in preparation for use by some external program then I would likely look at creating a Text file to write a and b using ancient fixed column put @ instructions and not worry about concatenating at 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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: