SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 3242 views
  • 2 likes
  • 3 in conversation