BookmarkSubscribeRSS Feed
gurpreetkaur
Fluorite | Level 6

Hi Experts,

 

I am concatenating 2 strings using  || operator. Below is the question:

 

data sample;

length string1 $20.;

string1 = "Gurpreet";

string2 = "Kaur";

string1 = string1 || string2;

run;

 

My question is: The value of string1 in the dataset should be "GurpreetKaur". But the value is being shown as "Gurpreet" only. Please explain.

 

Thanks,

Gurpreet

2 REPLIES 2
Jagadishkatam
Amethyst | Level 16

Please try with cats function, may the additional spaces are inhibiting from displaying the full name

 

data sample;
length string1 $20.;
string1 = "Gurpreet";
string2 = "Kaur";
string1 = cats(string1,string2);
run;

proc print;
run;
Thanks,
Jag
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, do remember that a string is an array of characters.  Where you define string1 to be $20 this means 20 boxes each with its own character.  So when you concatenate "Gurpreet            " with "Kaur", and put those 24 characters back into a variable which can only hold 20 characters the last four are dropped.

There are pletny of options to resolve this, for instance strip() each value:

string1=strip(string1)||strip(string2);

Or as @Jagadishkatam has given you, the cats() function would work well, note however you might want a space between so catx would be a better choice:

string1=catx(' ',string1,string2);

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 26329 views
  • 8 likes
  • 3 in conversation