BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
KC_16
Fluorite | Level 6

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Sathish_jammy
Lapis Lazuli | Level 10

data want;
set have;
want=scan(Comment_variable,-1,',');
run;

View solution in original post

4 REPLIES 4
Sathish_jammy
Lapis Lazuli | Level 10

data want;
set have;
want=scan(Comment_variable,-1,',');
run;

PeterClemmensen
Tourmaline | Level 20
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
Tom
Super User Tom
Super User

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.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 4059 views
  • 4 likes
  • 4 in conversation