SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Find and Remove Multiple String

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Find and Remove Multiple String

Hi, 

 

I have a variable as 'pr_ins_name' and example :

 

 

FASHION CO (M) BHD AS OWNER, LOOP
FASHION CO (M) BHD AS OWNER, LOOP INTER
ABC TECHONLOGY SDN BHD AS PRINCIPAL
ABD TECHONLOGY SDN BHD AS PRINCIPAL (AS MENTION
FLORA LIMITED

FLORA LTD
XYZ123 SDN BHD ON THE LIFE OF LKC
XYZ123 SDN BHD ON THE LIFE OF M
CAPTAIN APPAREL AS HIRER AND CONTRACTOR
YS ENTERPRISE AS HIRER AND HP DEVELOPMENT

 

I would like to remove those words (in Bold) :

1) contain "AS OWNER" , "AS PRINCIPAL" , "AS HIRER" , "ON THE LIFE OF" , "AS HIRER"

2) replace the "LIMITED" with "LTD"

 

How to combine above into one statement ?

 

 

data want;
set sasuser.name;


ins_name = substr (pr_ins_Name,1,find(pr_ins_name, ' AS OWNER')) ;
ins_name = substr (pr_ins_Name,1,find(pr_ins_name, ' AS HIRER')) ;
ins_name = substr (pr_ins_Name,1,find(pr_ins_name, ' AS PRINCIPAL')) ;
ins_name = substr (pr_ins_Name,1,find(pr_ins_name, ' ON THE LIFE OF')) ;

 


run;

 

 

Thank you.


Accepted Solutions
Solution
‎09-07-2017 09:42 PM
New Contributor
Posts: 3

Re: Find and Remove Multiple String

Thank you for your assistant. The program is worked and solved my problem. Have a nice day.

View solution in original post


All Replies
Super User
Super User
Posts: 7,695

Re: Find and Remove Multiple String

Something like:

data want;
  set sasuser.name;
  do i="AS_OWNER","AS PRINCIPAL"...;
    ins_name=tranwrd(ins_name,i,"");
  end;
run;

You could also put your strings in a dataset and use that.

New Contributor
Posts: 3

Re: Find and Remove Multiple String

 

I am trying below statement:

 

data want;
set sasuser.name;
do i=' AS OWNER',' AS PRINCIPAL','ON THE LIFE OF',' AS HIRER';
ins_Name = substr (pr_ins_Name,1,find(pr_ins_name, i)) ;
end;
run;

 

Only observation contains 'AS HIRER' has executed. 

 

Message shown : 

NOTE: Invalid third argument to function SUBSTR at line 31 column 12.

 

Output :

 

FASHION CO (M) BHD AS OWNER, LOOP
FASHION CO (M) BHD AS OWNER, LOOP INTER
ABC TECHONLOGY SDN BHD AS PRINCIPAL
ABD TECHONLOGY SDN BHD AS PRINCIPAL (AS MENTION
FLORA LIMITED
FLORA LTD
XYZ123 SDN BHD ON THE LIFE OF LKC
XYZ123 SDN BHD ON THE LIFE OF M
CAPTAIN APPAREL 
YS ENTERPRISE

 

Thank you.

Respected Advisor
Posts: 4,131

Re: Find and Remove Multiple String

@pooiwan

Your approach as such seems more or less to work.

 

data sample;
  infile datalines truncover;
  input pr_ins_name $100.;
  pr_ins_name_orig=pr_ins_name;
  do search_str=' AS OWNER',' AS PRINCIPAL','ON THE LIFE OF',' AS HIRER';
    pr_ins_name = substr (pr_ins_name,1,find(pr_ins_name,search_str,'i'));
  end;

  datalines;
FASHION CO (M) BHD AS OWNER, LOOP
FASHION CO (M) BHD AS OWNER, LOOP INTER
ABC TECHONLOGY SDN BHD AS PRINCIPAL
ABD TECHONLOGY SDN BHD AS PRINCIPAL (AS MENTION
FLORA LIMITED
FLORA LTD
XYZ123 SDN BHD ON THE LIFE OF LKC
XYZ123 SDN BHD ON THE LIFE OF M
CAPTAIN APPAREL AS HIRER AND CONTRACTOR
YS ENTERPRISE AS HIRER AND HP DEVELOPMENT
;
run;

Capture.JPG

 

 

Solution
‎09-07-2017 09:42 PM
New Contributor
Posts: 3

Re: Find and Remove Multiple String

Thank you for your assistant. The program is worked and solved my problem. Have a nice day.

Super User
Super User
Posts: 7,695

Re: Find and Remove Multiple String

In future please mark the post which answered the question as the correct answer, not your thank you post.  Otherwise the answer post (which appears under the question) does not actually answer the question.

Respected Advisor
Posts: 4,131

Re: Find and Remove Multiple String

@pooiwan

Then mark the answer which you've used as the solution and not your answer to it.

Super User
Super User
Posts: 7,695

Re: Find and Remove Multiple String

You do however notice that your code is different to mine yes?

    ins_name=tranwrd(ins_name,i,"");

And yours:

ins_Name = substr (pr_ins_Name,1,find(pr_ins_name, i)) ;

 

Mine removes strings, yours does a substring.

 

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 311 views
  • 2 likes
  • 3 in conversation