Replace first blank in sentence with special character

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

Replace first blank in sentence with special character

Hi, guys. I am newbie in sas for about a month. I have a problem with compress function as I am not very familiar with it. I was asked to replace the first blank of company name with $, by not replace the existing variable. P.S Only the first blank.

company name

Oasis

Amp North

Aeon Big

AET small APRA

BT wrap.

Really hope can provide answer using compress function, thank you very much.


Accepted Solutions
Solution
‎08-10-2015 11:47 AM
Trusted Advisor
Posts: 1,128

Re: Replace first blank in sentence with special character

i believe the below code will help you

Data A;

  Input company_name $20.;

  Datalines;

Oasis

Amp North

Aeon Big

AET small APRA

BT wrap

;

Run;

Data B;

  Set A;

new=prxchange('s/\s/\$/i',1,company_name) ;

Run;

This code will replace the first space with $, however for the first record is places $, which i believe is incorrect. it has to place the $ in the first space between two words. I would like the experts to correct my regular expression.

Thanks,

Jag

Thanks,
Jag

View solution in original post


All Replies
Super Contributor
Posts: 336

Re: Replace first blank in sentence with special character

Try:

Data A;
  Input company_name $20.;
  Datalines;
Oasis
Amp North
Aeon Big
AET small APRA
BT wrap
;
Run;

Data B;
  Set A;
  Substr(company_name,Find(Trim(company_name),' '),1)='$';
Run;

.. why is the compress function important?

Super User
Posts: 10,500

Re: Replace first blank in sentence with special character

The SAS Compress function removes not replaces.

Translate will replace but all occurences.

The Perl function PRXCHANGE could do this as well

Occasional Contributor
Posts: 16

Re: Replace first blank in sentence with special character

Hi, could I get the pattern in prxparse function for my dataset? Thank you.

Occasional Contributor
Posts: 16

Re: Replace first blank in sentence with special character

posn=index(trim(company_id),' ');

  company_alt=company_id;

  if posn ne 0 then substr(company_alt,posn,1)='$';

  else company_alt= " ";

Solution
‎08-10-2015 11:47 AM
Trusted Advisor
Posts: 1,128

Re: Replace first blank in sentence with special character

i believe the below code will help you

Data A;

  Input company_name $20.;

  Datalines;

Oasis

Amp North

Aeon Big

AET small APRA

BT wrap

;

Run;

Data B;

  Set A;

new=prxchange('s/\s/\$/i',1,company_name) ;

Run;

This code will replace the first space with $, however for the first record is places $, which i believe is incorrect. it has to place the $ in the first space between two words. I would like the experts to correct my regular expression.

Thanks,

Jag

Thanks,
Jag
Occasional Contributor
Posts: 16

Re: Replace first blank in sentence with special character

Hye, Jag.

Thanks for the prxchange example, I think just add trim() in company_name can solve your problem.

New Contributor
Posts: 3

Re: Replace first blank in sentence with special character

You can try below steps.


Data Want;

Input name $20.;

Cards;

Oasis

Amp North

Aeon Big

AET small APRA

BT wrap

;

Run;

Data want;

  Set want;

  posn = index(name,' ');

  Substr(name,posn,1) = '$';

Run;

Occasional Contributor
Posts: 16

Re: Replace first blank in sentence with special character

Hi, the answer for company_alt will be number.

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 370 views
  • 6 likes
  • 5 in conversation