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

Here is a pretty basic question for which the answer doesn't seem to exist on this site:

Column name is "Combo."  Type of column is text.

First few observations in the column:

20101_21603

20102_21603

20103_21603

20104_21603

20105_21603

I'd like to do a Search and Replace for "21603."  I would like to replace that with nothing (in other words, delete it).

(In a second step I'll do the same with "_"  Objective being to get the first part, i.e., "20101," alone by itself.)

Any help greatly appreciated.

Nicholas Kormanik

1 ACCEPTED SOLUTION

Accepted Solutions
Fraktalnisse
SAS Employee

Hi!

You have to write a complete data step. For example:

data NewData;

  set InputData;

  combo=transtrn(combo,'21603',trimn(''));

run;

If you would like to remove certain characters from a text string, the compress function is easier than transtrn. But compress doesn't work for longer substrings, if you would like to remove 21603 you can't use compress.

View solution in original post

7 REPLIES 7
Fraktalnisse
SAS Employee

Hi!

If you would like to take out the first 5 characters, you can use combo=substr(combo,1,5);

If you would like to take out everything before _, you can use combo=scan(combo,1,'_');

or

if you would like to search for 21603 and replace that with nothing, use combo=transtrn(combo,'21603',trimn(''));

if you would like to remove all _, use combo=compress(combo,'_');

NKormanik
Barite | Level 11

This will be done on an existing data file.  Is what you've written above part of a larger set of statements?  Or is it a stand-alone run-type line?

Why treat the '_' differently than the '21603'?

Fraktalnisse
SAS Employee

Hi!

You have to write a complete data step. For example:

data NewData;

  set InputData;

  combo=transtrn(combo,'21603',trimn(''));

run;

If you would like to remove certain characters from a text string, the compress function is easier than transtrn. But compress doesn't work for longer substrings, if you would like to remove 21603 you can't use compress.

Linlin
Lapis Lazuli | Level 10

data have;

input combo $11.;

cards;

20101_21603

20102_21603

20103_21603

20104_21603

20105_21603

;

data want;

length newvar $5;

set have;

newvar=scan(combo,1,'_');

proc print;run;

                                  Obs    newvar       combo

                                    1     20101     20101_21603
                                    2     20102     20102_21603
                                    3     20103     20103_21603
                                    4     20104     20104_21603
                                    5     20105     20105_21603

Linlin
Lapis Lazuli | Level 10

or

data have;
input combo $11.;
cards;
20101_21603
20102_21603
20103_21603
20104_21603
20105_21603
20101_88888
20102_88888
20103_88888
;
data want;
length newvar $11;
set have;
newvar=ifc(find(combo,'21603'),scan(combo,1,'_'),combo);
proc print;run;
                                 Obs    newvar            combo

                                 1     20101                20101_21603
                                 2     20102                20102_21603
                                 3     20103                20103_21603
                                 4     20104                20104_21603
                                 5     20105                20105_21603
                                 6     20101_88888    20101_88888
                                 7     20102_88888    20102_88888
                                 8     20103_88888    20103_88888

Haikuo
Onyx | Level 15

If you don't mind some PRX functions:

data have;

input combo $11.;

cards;

20101_21603

20102_21603

20103_21603

20104_21603

20105_21603

;

data want;

set have;

  combo=prxchange('s/_21603//',-1,combo);

run;

proc print;run;

Haikuo

NKormanik
Barite | Level 11

Remarkable.  Thanks so much folks.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 7 replies
  • 1001 views
  • 6 likes
  • 4 in conversation