BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

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;
6 REPLIES 6
andreas_lds
Jade | Level 19

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;

 

japelin
Rhodochrosite | Level 12

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;
Patrick
Opal | Level 21

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. 

Tom
Super User Tom
Super User

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;
ballardw
Super User

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 2513 views
  • 6 likes
  • 7 in conversation