Hi,
I have a variable (Comments_Line) where by it's prefixed with text that I don't need and it always follows the same structure.
I need to remove any text before the first comma and if possible, remove the comma too!
An example of this would be:
Kevin, welcome to your new account
Abi, we miss you
ideally what i would like to see on the examples used above would be the following output:
welcome to your new account
we miss you
Any idea is this is possible please and if it is, how I would go about doing this?
data want;
set have;
want=scan(Comment_variable,-1,',');
run;
data want;
set have;
want=scan(Comment_variable,-1,',');
run;
Thank you Sathish_jammy
data have;
input Comments_Line $ 1-100;
infile datalines truncover;
datalines;
Kevin, welcome to your new account
Abi, we miss you
;
data want;
set have;
NewVar=scan(Comments_Line,-1,',');
run;
Result:
Comments_Line NewVar Kevin, welcome to your new account welcome to your new account Abi, we miss you we miss you
There are some boundary conditions your example test data don't cover. What do you want when there are no commas? Comma is the FIRST character. There are two or more commas?
Here is your requirment.
I need to remove any text before the first comma and if possible, remove the comma too!
Here is code to do that for the variable named STRING in the dataset named HAVE.
data want;
set have;
string=left(substrn(string,indexc(string,',')+1));
run;
This will handle strings that start with comma, have no comma, and have multiple commas.
The SUBSTRN() will handle the case if the first comma is at the end of the variable so that adding one would be trying to index past the end of STRING.
The LEFT() function will remove space(s) that follow the comma. Note this means that for strings without any commas it will remove leading spaces.
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!
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.