- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!