DATA Step, Macro, Functions and more

Replace a string value with previous string in a group if first 10 characters are same

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Replace a string value with previous string in a group if first 10 characters are same

[ Edited ]

Hi

 

I have data which have merchant names but mispelled texts. which is becoming hard while joining this table with other table on basis of merchant name. im trying to replace the text with first row in the group. if first 10 characters are same.

 

Data Have;
input merchant_name $20.;
cards;
future retail pvt. ltd.
future retail limited
future retail ltd
osborne corporation
osborne corp ltd
;


run;

 

desired output should be like

future retail pvt. ltd.
future retail pvt. ltd.
future retail pvt. ltd.
osborne corporation
osborne corporation

thanks

 


Accepted Solutions
Solution
‎10-16-2017 02:38 AM
PROC Star
Posts: 2,230

Re: Replace a string value with previous string in a group if first 10 characters are same

Posted in reply to mohammedali0

Like this?

data HAVE;
input MERCHANT_NAME $40.;
cards;
future retail pvt. ltd.
future retail limited
future retail ltd
osborne corporation
osborne corp ltd
run;
data WANT;
  set HAVE;
  length PREV $40;
  retain PREV;
  if MERCHANT_NAME =: substr(PREV,1,10) then MERCHANT_NAME=PREV;
  else PREV=MERCHANT_NAME;
run;

 

MERCHANT_NAME
future retail pvt. ltd.
future retail pvt. ltd.
future retail pvt. ltd.
osborne corporation
osborne corporation

View solution in original post


All Replies
Solution
‎10-16-2017 02:38 AM
PROC Star
Posts: 2,230

Re: Replace a string value with previous string in a group if first 10 characters are same

Posted in reply to mohammedali0

Like this?

data HAVE;
input MERCHANT_NAME $40.;
cards;
future retail pvt. ltd.
future retail limited
future retail ltd
osborne corporation
osborne corp ltd
run;
data WANT;
  set HAVE;
  length PREV $40;
  retain PREV;
  if MERCHANT_NAME =: substr(PREV,1,10) then MERCHANT_NAME=PREV;
  else PREV=MERCHANT_NAME;
run;

 

MERCHANT_NAME
future retail pvt. ltd.
future retail pvt. ltd.
future retail pvt. ltd.
osborne corporation
osborne corporation
New Contributor
Posts: 2

Re: Replace a string value with previous string in a group if first 10 characters are same

Thanks a ton Sir.

Occasional Contributor
Posts: 12

Re: Replace a string value with previous string in a group if first 10 characters are same

Posted in reply to mohammedali0
hi .


DATA NEW_HAVE1 (DROP=merchant_name RENAME=(PREV_VALUE=NEW_VALUE));
SET HAVE;
LENGTH PREV_VALUE $50;
RETAIN PREV_VALUE;
IF merchant_name =: SUBSTR(PREV_VALUE,1,10) THEN merchant_name=PREV_VALUE;
ELSE PREV_VALUE=merchant_name;
RUN;
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 164 views
  • 0 likes
  • 3 in conversation