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.
ilikesas
Barite | Level 11

Hi,

 

suppsose I have a variable string with value name1.

 

I would like to add to this string ***, like this the string becomes name1***.

 

I tried to do:

string = string ||"***";

 

but it didn't work, I had to create a dummy variable and do dummy = string ||"***"; and delete the original string and rename the dummy string. But is there a direct way to add a substring to an existing string without having recourse to dummy variables?

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Post your exact code and log.  Note that your code above doesn't use the TRIM function.

 

Following the suggestions of assigning a long enough LENGTH and use of the TRIM function I can't replicate your issue. In general, I prefer the CAT family of functions since they trim automatically. I also find it easier to read.

 

data test;
	length a b $20.;
	input a $ b $;
	datalines;
14.793 15.697
;
run;

data test2;
	set test;
	a=trim(a)||"***";
	b=catt(b, "****");
run;

proc print data=test2;
run;

View solution in original post

8 REPLIES 8
Astounding
PROC Star

You can do it directly:

 

string= trim(string) || '***';

 

You're running into trouble because STRING is already defined with a fixed length.  If you just concatenate without adding the TRIM function, the trailing blanks remain in place and there isn't room to add any more characters.

ilikesas
Barite | Level 11

I am still getting errors, namely in the cases where I have strings which are made entirely of numbers, so if I have something like this: 14.793 and want to get 14.793*** I can't.

 

But when I do a = string||'***'; I get 14.793*** even without trimming.

 

Thank you! 

Reeza
Super User

Post your exact code and log.  Note that your code above doesn't use the TRIM function.

 

Following the suggestions of assigning a long enough LENGTH and use of the TRIM function I can't replicate your issue. In general, I prefer the CAT family of functions since they trim automatically. I also find it easier to read.

 

data test;
	length a b $20.;
	input a $ b $;
	datalines;
14.793 15.697
;
run;

data test2;
	set test;
	a=trim(a)||"***";
	b=catt(b, "****");
run;

proc print data=test2;
run;
Kurt_Bremser
Super User

@ilikesas wrote:

I am still getting errors, namely in the cases where I have strings which are made entirely of numbers, so if I have something like this: 14.793 and want to get 14.793*** I can't.

 

But when I do a = string||'***'; I get 14.793*** even without trimming.

 

Thank you! 


If your original variable is defined as type character with a length of 6, and contains the value '14.793', then it cannot accept any further characters.

Since your new variable a is automatically defined by SAS with sufficient length (because you concatenate with a string literal, so SAS knows exactly how many characters are needed), it can take the whole new string.

Example:

data have;
string = '14.793';
run;

proc contents;run;

data want;
set have;
a = string !! '***';
run;

proc contents;run;

Partial output from the second proc contents:

Alphabetic List of Variables and Attributes

       #    Variable    Type    Len

       2    a           Char      9
       1    string      Char      6
Astounding
PROC Star

That result is misleading.  It would occur when STRING is actually a numeric variable, not a character variable.  There is no way to add "***" to the value fo a numeric variable (although a format might be able to get it to print with "***" at the end).  When your original variable is character, you won't get any errors.

ilikesas
Barite | Level 11

That is my case - I get errors that my variable is numeric, although I did try to convert it to character in the following way:

 

estimate = put(estimate,12.3.);

 

 

Astounding
PROC Star

Once ESTIMATE is defined as numeric, it remains numeric.  Forever.  Yes, the PUT function generates a character string, but SAS then has to store those characters in a numeric variable, so SAS converts the character string to numeric.

ilikesas
Barite | Level 11

So yes, the only way to make ESTIMATE character is to create a new character DUMMY variable, make it equal ESTIMATE, delete the original ESTIMATE and then rename DUMMY to ESTIMATE. That is what I did in the very beginning, but I just thought if there was another way to make it directly. I accepeted as a solution the post where the string and catt functions are shown, making it more useful for future readers.

 

Thank you!

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
  • 8 replies
  • 38977 views
  • 12 likes
  • 4 in conversation