BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NKormanik
Barite | Level 11

I'll start searching for the answer, but thought I'd put the question here first:

 

How would one add a character string to every value of some variable?

 

i.e., Variable is Item...

apple

banana

grape

 

becomes:

apple_fruit

banana_fruit

grape_fruit

 

Solution greatly appreciated!

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Or

newvar = cats(item,'_fruit');

or

newvar = strip(item)||'_fruit';

 

The || operator may leave some spaces you don't want in the result.

 

You likely need to create a new variable as the length of the variable item is already set and adding additional characters may exceed that length, or play some games with a Length statement if you want to use the same variable name.

This would look like (assumes making the variable length 25 will hold existing text plus the text you intend to add)

data want; 
   length item $ 25;
   set have;
   item= cats(item,'_fruit');
run;

The above would have a possibly unwanted behavior; item becomes the first column in the data set which some people have issues with column order;

View solution in original post

2 REPLIES 2
tarheel13
Rhodochrosite | Level 12

just put newvar=item||'_fruit' in a data step.

ballardw
Super User

Or

newvar = cats(item,'_fruit');

or

newvar = strip(item)||'_fruit';

 

The || operator may leave some spaces you don't want in the result.

 

You likely need to create a new variable as the length of the variable item is already set and adding additional characters may exceed that length, or play some games with a Length statement if you want to use the same variable name.

This would look like (assumes making the variable length 25 will hold existing text plus the text you intend to add)

data want; 
   length item $ 25;
   set have;
   item= cats(item,'_fruit');
run;

The above would have a possibly unwanted behavior; item becomes the first column in the data set which some people have issues with column order;

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2168 views
  • 2 likes
  • 3 in conversation