DATA Step, Macro, Functions and more

sas add substring to an existing string

Accepted Solution Solved
Reply
Super Contributor
Posts: 413
Accepted Solution

sas add substring to an existing string

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!


Accepted Solutions
Solution
‎10-14-2016 10:21 PM
Super User
Posts: 17,963

Re: sas add substring to an existing string

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


All Replies
Super User
Posts: 5,099

Re: sas add substring to an existing string

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.

Super Contributor
Posts: 413

Re: sas add substring to an existing string

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! 

Solution
‎10-14-2016 10:21 PM
Super User
Posts: 17,963

Re: sas add substring to an existing string

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;
Super User
Posts: 6,977

Re: sas add substring to an existing string


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
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,099

Re: sas add substring to an existing string

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.

Super Contributor
Posts: 413

Re: sas add substring to an existing string

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.);

 

 

Super User
Posts: 5,099

Re: sas add substring to an existing string

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.

Super Contributor
Posts: 413

Re: sas add substring to an existing string

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!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 1814 views
  • 10 likes
  • 4 in conversation