BookmarkSubscribeRSS Feed
Alexxxxxxx
Pyrite | Level 9

Dear all,

 

How can I convert the relevant code to 'GB'?

for  

NAME
PPP G.B.N.
PPP G. B. N.
PPP G.B. N.
PPP G.B. 
PPP D.G.B. 
PPP D. G. B. 
PPP D. G.B. 
PPP D. G.B. N.
PPP D. G. B. N.

,

I expect to get 

NAMEName_new
PPP G.B.N.PPP G.B.N.
PPP G. B. N.PPP G. B. N.
PPP G.B. N.PPP GB N.
PPP G.B. PPP GB
PPP D.G.B. PPP D.G.B. 
PPP D. G. B. PPP D. G. B. 
PPP D. G.B. PPP D. GB 
PPP D. G.B. N.PPP D. GB N.
PPP D. G. B. N.PPP D. G. B. N.

 

for example,

for 'G.B.N.' and 'G. B. N.' which looks like a complete word, should not be converted to 'GB N.'

But for 'G.B. N.', the 'G.B.' is separated with 'N.' by a blank, and then it should be converted to 'GB N.'.

 

I have tried to use the following code but failed. Could you please give me some suggestion about this? thanks in advance.

 

data HAVE;
input NO NAME &:$100.;
infile datalines  missover;
datalines;
10 PPP G.B.N.
10 PPP G. B. N.
10 PPP G.B. N.
10 PPP G.B. 
10 PPP D.G.B. 
10 PPP D. G. B. 
10 PPP D. G.B. 
10 PPP D. G.B. N.
10 PPP D. G. B. N.
run;

data WANT;
  set HAVE;
 NAME_new=prxchange("s/\bG\b[. ]+\bB\b[. ]+|[. ]+\bGB\b[. ]+/ GB /i",-1, NAME);
run;

 

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

Like either of these?

data  T;
input NAME $20.;
NAME1=tranwrd(NAME,' G.B. ',' GB ');
NAME2=prxchange('s/ G\.B\. / GB /',1,NAME);
cards;
PPP G.B.N.
PPP G. B. N.
PPP G.B. N.
PPP G.B. 
PPP D.G.B. 
PPP D. G. B. 
PPP D. G.B. 
PPP D. G.B. N.
PPP D. G. B. N.
run;

NAME NAME1 NAME2
PPP G.B.N. PPP G.B.N. PPP G.B.N.
PPP G. B. N. PPP G. B. N. PPP G. B. N.
PPP G.B. N. PPP GB N. PPP GB N.
PPP G.B. PPP GB PPP GB
PPP D.G.B. PPP D.G.B. PPP D.G.B.
PPP D. G. B. PPP D. G. B. PPP D. G. B.
PPP D. G.B. PPP D. GB PPP D. GB
PPP D. G.B. N. PPP D. GB N. PPP D. GB N.
PPP D. G. B. N. PPP D. G. B. N. PPP D. G. B. N.
Alexxxxxxx
Pyrite | Level 9

Dear @ChrisNZ ,

 

I really appreciate your advice. May I ask one more questions, please? How can I also convert 'G. B.' to 'GB' during this process?

data  T;
input NAME $20.;
NAME1=tranwrd(NAME,' G.B. ',' GB ');
NAME2=prxchange('s/ G\.B\. / GB /',1,NAME);
cards;
PPP G.B.N.
PPP G. B. N.
PPP G.B. N.
PPP G.B. 
PPP D.G.B. 
PPP D. G. B. 
PPP D. G.B. 
PPP D. G.B. N.
PPP D. G. B. N.
/*one more question*/
PPP G. B. 

run;

 

ChrisNZ
Tourmaline | Level 20

Your 6th line of data was explicitly mentioned as unchanged.

What's different here?

Alexxxxxxx
Pyrite | Level 9

 for value 'PPP D. G. B.', 'D. G. B.' seems like one word, (for example, 'L. T. D.' is 'LTD', limited), -- I do not want to process it during this step.

But if the value is 'PPP G. B.', it should be 'GB', it is the value I expect to process during the step.

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand in the Innovate Hub.

Watch 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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2161 views
  • 0 likes
  • 2 in conversation