Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

Removing string from another string

Accepted Solution Solved
Reply
N/A
Posts: 0
Accepted Solution

Removing string from another string

Hi,

I have two variable whose values are strings,
I want to remove substring from big string.
for example,

A=adc sgk ghhj
B= sgk

I want to create a new variable as C, So that it should be c=adc ghhj.
Can any one give the solution on that?


Thanks in advance,
Pabitra

Accepted Solutions
Solution
‎07-06-2017 01:15 PM
SAS Employee
Posts: 35

Re: Removing string from another string

Posted in reply to deleted_user
Base SAS code is always fun. What if you don't know the length of the strings but you do know that space is the separator and that you want the first chunk from A and the third chunk from B. Use the scan function.

length c $32 ;
c= scan(a,1,' ') !! ' ' !! scan(b,3,' ');


Now, what if you don't know how many chunks there are in B, but you know you want the last? Use the reverse function.

length c $32 ;
c= scan(a,1,' ') !! ' ' !! reverse(scan(reverse(b),1,' '));


When using EM 5.3 or 6.1, you can enter the expression directly into the SCORE code editor in the CODE node or the TRANSFORM Node. You do not need to enter DATA, SET, or RUN statements. The code will be inserted into the diagram's score code.

What if you want to use the variable C in a model but you want to reject A and B? You can do this in your CODE node as well. The CODE node has editors for TRAIN, SCORE, and REPORT code. Enter this code into the TRAIN code.

%EM_METACHANGE(name=A, role=rejected) ;
%EM_METACHANGE(name=B, role=rejected) ;


The new variable C will have the role of input by default. If you want to set that explicitly, you can do this:

%EM_METACHANGE(name=C, role=input, level=nominal) ;

You do NOT want to drop A and B from the data since that will cause the score code to fail when the expression that creates C is evaluated after a temporary data set has been created.

In general, EM does not drop variables from the data set, and does not change the values of existing variables.

cheers.

View solution in original post


All Replies
Super Contributor
Super Contributor
Posts: 3,174

Re: Removing string from another string

Posted in reply to deleted_user
Explore the use of SAS function TRANWRD to assign a new SAS variable.

Scott Barry
SBBWorks, Inc.
SAS Employee
Posts: 31

Re: Removing string from another string

Posted in reply to deleted_user
> Hi,
>
> I have two variable whose values are strings,
> I want to remove substring from big string.
> for example,
>
> A=adc sgk ghhj
> B= sgk
>
> I want to create a new variable as C, So that it
> should be c=adc ghhj.
> Can any one give the solution on that?
>
>
> Thanks in advance,
> Pabitra

Hi Pabitra,

One way to accomplish this is to use the BASE SAS substring function in an EM SAS code node.

Substr Returns a portion of the variable value based on a starting position
and number of characters.

cpart1 = SUBSTR(a,1,3);
cpart2 =SUBSTR(a,9,3);

You could runt the function twice as above and the use || to cancatenate back the two extract strings into a single varaible c =adc ghhj.

c=cpart1 ||(left)cpart2;
drop cpart1 cpart2;
Others may have an easier step.
N/A
Posts: 0

Re: Removing string from another string

Posted in reply to deleted_user
You may also be interested by the Regular Expressions.
I do not know how to handle them in SAS specifically.
But it is a very powerfull way to detect string within string, test them, and handle them.
Solution
‎07-06-2017 01:15 PM
SAS Employee
Posts: 35

Re: Removing string from another string

Posted in reply to deleted_user
Base SAS code is always fun. What if you don't know the length of the strings but you do know that space is the separator and that you want the first chunk from A and the third chunk from B. Use the scan function.

length c $32 ;
c= scan(a,1,' ') !! ' ' !! scan(b,3,' ');


Now, what if you don't know how many chunks there are in B, but you know you want the last? Use the reverse function.

length c $32 ;
c= scan(a,1,' ') !! ' ' !! reverse(scan(reverse(b),1,' '));


When using EM 5.3 or 6.1, you can enter the expression directly into the SCORE code editor in the CODE node or the TRANSFORM Node. You do not need to enter DATA, SET, or RUN statements. The code will be inserted into the diagram's score code.

What if you want to use the variable C in a model but you want to reject A and B? You can do this in your CODE node as well. The CODE node has editors for TRAIN, SCORE, and REPORT code. Enter this code into the TRAIN code.

%EM_METACHANGE(name=A, role=rejected) ;
%EM_METACHANGE(name=B, role=rejected) ;


The new variable C will have the role of input by default. If you want to set that explicitly, you can do this:

%EM_METACHANGE(name=C, role=input, level=nominal) ;

You do NOT want to drop A and B from the data since that will cause the score code to fail when the expression that creates C is evaluated after a temporary data set has been created.

In general, EM does not drop variables from the data set, and does not change the values of existing variables.

cheers.
N/A
Posts: 0

Re: Removing string from another string

Posted in reply to David_Duling
Thanks for the ans....
But i need it for general...i mean i want to apply for two columns whose values are the strings...

can some one give me any suggestion...

Thanks in advance......

pabitra
Super Contributor
Super Contributor
Posts: 3,174

Re: Removing string from another string

Posted in reply to deleted_user
Did you look at the DOC and possibly using the TRANWRD function in a DATA step, as suggested? The function can be used with SAS variables as well as constants. Here's a Google advanced search argument to help you find related topic info on the SAS support http://support.sas.com/ website:

tranwrd function site:sas.com


Scott Barry
SBBWorks, Inc.
SAS Employee
Posts: 35

Re: Removing string from another string

ok, how about this - you can use the string that should be removed as the delimiter in the scan function. Works both with and without spaces.

data _null_ ;
a= 'asdf yyy sdfs' ;
b= 'yyy' ;
c= scan(a,1,b) !! ' ' !! scan(a,2,b) ;
put c ;
run ;
Super Contributor
Super Contributor
Posts: 3,174

Re: Removing string from another string

Posted in reply to David_Duling
Honestly, is there some aversion with using a function for its defined purpose, or am I missing something with this elongated thread?

Scott Barry
SBBWorks, Inc.
SAS Employee
Posts: 35

Re: Removing string from another string

For simply removing one string form another, that is the way to go.

TRANWRD

http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a000215027.htm

rc= tranwrd(a,b,' ');
Valued Guide
Posts: 2,177

Re: Removing string from another string

Posted in reply to David_Duling
new_string= compress(tranwrd(original_string, trim(unwanted_string),'01'x ), x'01' );

thus ensuring all occurrences of what is in "unwanted_string" are removed, without leaving a blank for each occurrence.
The trim() function is used to ensure those cases not fully using the width of the variable named unwanted_string will match only on the non-blank (left-hand) part.
The compress() function will remove the substitutions for the unwanted_string.
Frequent Contributor
Posts: 104

Re: Removing string from another string

Posted in reply to deleted_user
data _null_;
a='abc def kkk xyz kkk qwe';
b='kkk';
c=prxchange('s/'||b||'//', -1, a);
put a= b= c=;
run;

will get you this: a=abc def kkk xyz kkk qwe b=kkk c=abc def xyz qwe

Note that it doesn't deal with the resultant two-blank. And it will replace portions of a word containing kkk. With suitable changes to the regular expression, you can deal with all of those situations.
🔒 This topic is solved and locked.

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

Discussion stats
  • 11 replies
  • 9966 views
  • 1 like
  • 6 in conversation